Visually Comparing Rows of a MySQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shekki25
Forum Newbie
Posts: 2
Joined: Mon Apr 04, 2011 9:28 pm

Visually Comparing Rows of a MySQL database

Post 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.
Apocalypz
Forum Newbie
Posts: 6
Joined: Fri Nov 02, 2007 2:54 pm

Re: Visually Comparing Rows of a MySQL database

Post 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.
shekki25
Forum Newbie
Posts: 2
Joined: Mon Apr 04, 2011 9:28 pm

Re: Visually Comparing Rows of a MySQL database

Post by shekki25 »

Awesome, worked like a charm. Thanks a ton!
Post Reply