Sorry I haven't updated PHPBuilder is
SO long, but I've been a bit busy
helping build a new web site called
SourceForge.
While building the site, I stretched my knowledge of PHP and databases and thought
I would share a few things I learned.
A lot of people ask about passing arrays and using checkboxes/select boxes in PHP,
so that's what I'll cover today.
To pass multiple values, as in a "Multiple Select Box", put [] after the variable name,
like assigned_to[].
<SELECT NAME="assigned_to[]" MULTIPLE SIZE="8">
<OPTION VALUE="100">None</OPTION>
<OPTION VALUE="2">dtype</OPTION>
<OPTION VALUE="4">tim_perdue</OPTION>
<OPTION VALUE="5">fusion94</OPTION>
<OPTION VALUE="3">precision</OPTION>
<OPTION VALUE="18">michael</OPTION>
<OPTION VALUE="157">jbyers</OPTION>
<OPTION VALUE="251">Lectric</OPTION>
<OPTION VALUE="149">baddog</OPTION>
<OPTION VALUE="105">mrzenn</OPTION>
</SELECT>
Then on the receiving page, you simply do a count() on assigned_to, and iterate
through the list of values that was selected.
<?php
$count=count($assigned_to);
for ($i=0; $i<$count; $i++) {
echo $assigned_to[$i];
}
?>
Easy, right? Instead of echoing the values, you can insert them into a database or
do whatever you need to do.
Over the past year, I've created a bunch of utilities that help me code faster. Utilities
that create HTML select boxes, multiple select boxes (as shown above), show database results, etc.
Here's the first. It takes a two-column database result and creates a pop-up box from it.
Notice how I use apostrophes instead of quotes. This helps improve speed of parsing.
<?php
function build_select_box ($result, $name, $checked_val="xzxz") {
/*
Takes a result set, with the first column being the "id" or value
and the second column being the text you want displayed
The second parameter is the name you want assigned to this form element
The third parameter is optional. Pass the value of the item that should be checked
*/
$rows=db_numrows($result);
echo '<SELECT NAME="'.$name.'">';
for ($i=0; $i<$rows; $i++) {
echo '
<OPTION VALUE="'.db_result($result,$i,0).'"';
if (db_result($result,$i,0) == $checked_val) {
echo ' SELECTED';
}
echo '>'.db_result($result,$i,1).'</OPTION>';
}
echo '
</SELECT>';
}
?>
This next one is a bit more complex. It builds the multiple select boxes, and since
multiple rows can be selected, you have to pass in a name with [] at the end, and
pass in an array of checked items.
<?php
function build_multiple_select_box ($result,$name,$checked_array,$size='8') {
/*
Takes a result set, with the first column being the "id" or value
and the second column being the text you want displayed
The second parameter is the name you want assigned to this form element
The third parameter is an array of checked values;
The fourth parameter is optional. Pass the size of this box
*/
$rows=db_numrows($result);
for ($i=0; $i<$rows; $i++) {
echo '
<OPTION VALUE="'.db_result($result,$i,0).'"';
/*
Determine if it's checked
*/
$val=db_result($result,$i,0);
for ($j=0; $j<$checked_count; $j++) {
if ($val == $checked_array[$j]) {
echo ' SELECTED';
}
}
echo '>'.db_result($result,$i,1).'</OPTION>';
}
echo '
</SELECT>';
}
?>
This last function is utilitarian. I built it to quickly take a set of selected
items and turn it into an array that I could pass to build_multiple_select_box().
<?php
function result_column_to_array($result, $col=0) {
/*
Takes a result set and turns the optional column into
an array
*/
$rows=db_numrows($result);
for ($i=0; $i<$rows; $i++) {
$array[]=db_result($result,$i,$col);
}
return $array;
}
?>
So how about some examples of using it? The following code generated the sample
box above directly from the database. To be honest, I ripped this directly out of the
task manager that I wrote for SourceForge. It's OK, because VA Linux is GPLing all of this
code anyway.
<?php
/*
List of possible users that this one could be assigned to
*/
$sql="SELECT user.user_id,user.user_name ".
"FROM user,user_group WHERE user.user_id=user_group.user_id ".
"AND user_group.group_id='$group_id' AND user_group.project_flags IN (1,2)";
$result3=db_query($sql);
/*
Get the list of ids this is assigned to and convert to array
to pass into multiple select box
*/
$result2=db_query("SELECT assigned_to_id FROM project_assigned_to WHERE project_task_id='$project_task_id'");
build_multiple_select_box($result3,'assigned_to[]',result_column_to_array($result2));
?>
Not too difficult, right? Now on to the receiving page...
<?php
$user_count=count($assigned_to);
/*
DELETE THEN Insert the people this task is assigned to
*/
$toss=db_query("DELETE FROM project_assigned_to WHERE project_task_id='$project_task_id'");
for ($i=0; $i<$user_count; $i++) {
$sql="INSERT INTO project_assigned_to VALUES ('','$project_task_id','$assigned_to[$i]')";
$result=db_query($sql);
}
?>
I hope this is all very useful. Please and let me know
if you have created some utilities that others could use.
--Tim