CMS takes the work out of building interactive websites.

Create a website at Omnis Network using Joomla, PostNuke or other popular software.

$5.95 Web Hosting
Installs instantly using the FANTASTICO script installer!!

Your site will have automatic features like member sign up and site search.

Features like message boards and chat are easily added.

PHP MySQL
Interactive Website Design

Parsing Multiple Selections

Very often a form will allow a user to select more than one choice from a list of choices. Checkboxes and selection boxes would provide this option.

We recommend that before you try this tutorial, you download the birthday package and build the database. You may also want to experiment with adding some extra fields to the table.

In the form below the user is given the option of selecting desired fields of the database table. The user may only want to look at names and birthdates without the id field.

This option is generated using a series of checkboxes.

A text input box is also provided to add a condition to the search.


Search for:

ID
Name
Birthday

Where:

Name Is:

When the information is selected and the Submit button is depressed a table like the one below is generated:
Name Jim Jr Jim Sr
Birthday Mar26 Jun22

The Name and Birthday checkboxes would be selected and the name Jim would be entered in the text box. If more than one Jim existed in the database, both would be returned.

Notice also that the Id field doesn't generate because it wasn't selected.

The script that produces the result is shown below. Comments are provided to show the code that is used for some of the operations.

<?php
/*Concat Checkbox Values into One string separated by commas for Select*/
foreach ($searchitem as $value){
$search=$search.",".$value;
}
/* Remove Leading comma*/
$searchit=substr($search,1);
/*Show the selected fields*/
print "$searchit";
$db="mydatabase";
$link = mysql_connect("localhost");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result=mysql_query( "SELECT $searchit FROM birthdays where name LIKE '%$where%'" );

$num=mysql_num_rows($result);

mysql_close();

print "<b><center>Database Output - $searchitem</center></b><br><br>";
print "<table cellpadding=2 cellspacing=0 border=1>";
/*Check for void or unselected field id*/
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
++$i;
}
/*Print if not void*/
if($id <> ""){
print "<tr><td bgcolor=#000080><p>ID</p></td>";
$i=0;
while ($i < $num) {
/*Convert field to string*/
$id=mysql_result($result,$i,"id");
print "<td align=right>$id</td>";
++$i;
}
print "</tr>";
}

/*Check for void field name*/
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
++$i;
}

/*Print if not void*/
if($name <> ""){
print "<tr><td bgcolor=#000080><p>Name</p></td>";
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
print "<td align=center>$name</td>";
++$i;
}
print "</tr>";
}

/*Check for void field birthday*/
$i=0;
while ($i < $num) {
$birthday=mysql_result($result,$i,"birthday");
++$i;
}

/*Print if not void*/
if($birthday <> ""){
print "<tr><td bgcolor=#000080><p>Birthday</p></td>";
$i=0;
while ($i < $num) {
$birthday=mysql_result($result,$i,"birthday");
print "<td align=left>$birthday</td>";
++$i;
}
print "</tr>";
}

print "</table>";
?>

Note: This script would be enclosed in an html page format and saved with php extension.

The Form

The code for the form would be created as shown below:
<h4>Search for:</h4>
<form method="POST" action="the-script.php">
<input type="checkbox" name="searchitem[]" value="id">ID<br>
<input type="checkbox" name="searchitem[]" value="name">Name<br>
<input type="checkbox" name="searchitem[]" value="birthday">Birthday<br>
<br>
<h4>Where:</h4>
Name Is: <input type="text" name="where" size="20">
<input type="submit" value="Submit"><input type="reset">
</form>

Note: The characters that appear as a box following searchitem are square brackets [ and ]. This signifies that the checkbox items are array items.

Download the Scripts

The Birthdays Database management files can be downloaded in a zip file. The package contains an integrated db management system, with a simple login file and interface. To run the scripts on your PC you must have a localhost server installed along with PHP and the MySQL server.
Download birthdays_db.zip

MySQL Tutorial

To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com


Introduction Top Recommended $5.95 Hosting Creating the database
PHP MySQL is a service provided by Net Success 2000 Plus Inc
PO Box 1508
Somerset, KY 42502
Last Modified: July 2, 2007

| HTML TOC | Web Design | Create a Website |

PHP MySQL - Interactive Website Design Copyright © 2007