Page 1 of 1
Visually Comparing Rows of a MySQL database
Posted: Mon Apr 04, 2011 9:37 pm
by shekki25
Essentially what I need to do is give a user the option to check a box to "compare" the desired items (up to 3), then submit that form and have the options visually represented on a single page. I have the table all set up with the desired values but I'm having a hard time figuring out a script to make it work. Any help would be greatly appreciated.
Re: Visually Comparing Rows of a MySQL database
Posted: Wed Apr 06, 2011 8:50 am
by Apocalypz
Say the user wants rows A, B, and C. Your form looks like this...
Code: Select all
<input type='checkbox' name='items[]' value='A' />
<input type='checkbox' name='items[]' value='B' />
<input type='checkbox' name='items[]' value='C' />
When this is submitted, your PHP page can handle the array like so...
Code: Select all
$sql = "SELECT * FROM `items` WHERE ";
foreach ($_POST['items'] as $key => $val)
{
$sql .= "`id` = '$val' OR ";
}
$sql = substr($sql,0,-4);
$result = mysql_query($sql);
while ($items = mysql_fetch_assoc($result))
{
// looping through your items to display them
}
You can handle the array of data POSTed just as you'd handle any other array.
Re: Visually Comparing Rows of a MySQL database
Posted: Thu Apr 07, 2011 10:57 pm
by shekki25
Awesome, worked like a charm. Thanks a ton!