PC Services"From Payroll to Body Scanners" |
Some PHP Tips and 'Tricks'Thinking beyond each page and DESIGNING your web site |
Generating PHP/HTML CheckBox Array in Form.Taking the example sections in the previous section we can now look at how to ctreate PHP scripts to do this simply. Why should we consider this? basically because whether you are producing a datasheet/brochure library, online shopping, or even a recruitment site the basic steps for most of the pages will consist of -
This becomes even easier to generate from PHP, as no doubt some form of search has produced a list of records to be used as the short form list, so the necessary information is already to hand. In the following $result is an array of rows (records), that has been extracted from a database, which is scanned row by row to produce rows of an HTML table consisting of
while( $row = mysql_fetch_object( $result ) )
{
echo " <tr>\n <td>";
echo "<B>$row->title</B></td>\n";
if( empty( $row->reference ) )
$row->reference = " ";
echo "<td> $row->reference </td>\n";
echo "<td align=center><input type=checkbox id=check name='id[]'"
." value='$row->id'></td>\n </tr>\n";
}
As we have done a database search to give a result containing a number of rows it is easy to generate the PHP code using mysql_num_rows() function to say how many rows there should be. Then the buttons can simply be created by PHP lines of
<input type=button value="Select ALL" onclick="select_all( check, true ); return false;"> Similarly the Submit button as in previous example calls upon the Javascript check functions from the form definition as follows - <form method="post" name=listform enctype="multipart/form-data" action="results.php" onsubmit="return checkform( check, 1 );"> Now by adding a GET method to some of the items (Title and Reference Number) as links, we can make this even easier to navigate to that specific item ONLY, by clicking on that link. The GET parameter is id = database index
while( $row = mysql_fetch_object( $result ) )
{
echo " <tr>\n <td>"
."<a href='search.php?id=$row->id'>";
echo "<B>$row->title</B></a></td>\n";
if( empty( $row->reference ) )
$row->reference = " ";
echo "<td> <a href='search.php?id=$row->id'>"
.$row->reference."</a> </td>\n";
echo "<td align=center><input type=checkbox id=check name='id[]'"
." value='$row->id'></td>\n </tr>\n";
}
This way a small section of code generates long tables in the same format, so different formats can easily be incorporated into the same PHP script with choices of format determined by POST and/or GET parameters, from the calling page. Careful script programming will allow search modules to perform various types of search methods (by category, area, price, quantity, list of stock codes, etc..) in one module, which makes maintenance easier. This way functions for common methods will be found quicker as the design WILL change over time! |
| © 2010 onwards by PC Services, Reading UK | Last Updated: 9th January 2010 |
| If you encounter problems with this page please email your comments to webmaster | |