Forum Moderators: coopster

Message Too Old, No Replies

Comparing results from a database such as pricing etc by ticking a box

         

JuiceUK

11:19 am on May 18, 2006 (gmt 0)

10+ Year Member



Hi All,

Thanks for reading - I'm newish to php so please could you give me good keywords to search for or the basic code to understand -

I have a database and i have a page that allows people to search and get a list of titles of courses that we run at the college - these resutls are listed in a table.

I would like to have a feature were I can select 3 or 4 course titles (by clicking a tick box) then press a compare button and it goes to different page and lists the basic info next to each other such as start date, price, entry requirements etc

I've looked up php sessions thinking this could help but not got my head around it and I've looked up the idea of cookies but have been off put by this method - please could anyone point me in the right direction as to how I can do this?

Many thanks -

M

dreamcatcher

11:47 am on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi JuiceUK, a warm welcome to Webmaster World. :)

I don`t think cookies or sessions are what you are looking for. You need to think more in the line of arrays.

So, some syntax to get you started would be:


<input type="checkbox" name="course[]" value="1">
<input type="checkbox" name="course[]" value="2">
<input type="checkbox" name="course[]" value="3">

if (!empty($_POST['course']))
{
mysql_query("SELECT * FROM courses WHERE id IN(".implode(",", $_POST['id'])");

// show course data..
}

So, basically you need to create some data in a database that corresponds to the id numbers. When you run the query, fetch the data for those ids. The rest would involve setting out a design (HTML table?) to incorporate the comparison.

Good luck.

dc

JuiceUK

12:05 pm on May 18, 2006 (gmt 0)

10+ Year Member



Hi dreamcatcher - really appreciate you getting back to me so quick -

Right I kinda get what's happenin but I'm a bit confused - this is the code I have that sets out my list of found courses:

$result = mysql_query($query,$connection);
if (mysql_num_rows($result)) {
?>
<p>We have found <strong><?php $count = mysql_num_rows($result); echo $count;?></strong> courses matching your search criteria <?php if (!empty($sw)) {?>'<strong><?php echo $sw;?></strong>'<?php }?>.</p>
<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence / Duration</th><th>Reference</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr><td><a href="/courses/<?php echo $row['course_id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?> / <?php echo $row['duration'];?></td><td><?php echo $row['reference'];?></td></tr>
<?php
}
?>
</tbody>
</table>

<?php
} else {
?>

Which rusults in a table being made as you can see - ok - so looking at your code I want on the end of ever row in this table a little tick box that says compare - I tick it - lets say 3 or them - I press a button on the screen and it shoots off to a page that lists keys facts.

In your code :

<input type="checkbox" name="course[]" value="1">
<input type="checkbox" name="course[]" value="2">
<input type="checkbox" name="course[]" value="3">

The value - would this be set to my Id of the course?

This bit of the code:

if (!empty($_POST['course']))
{
mysql_query("SELECT * FROM courses WHERE id IN(".implode(",", $_POST['id'])");

// show course data..
}

Would this be on my compare page? I knind of know what I need to do but can't get my head around it - I need to collect all the id's people are interested in send it to a new page and then say right now run a query using these id's and get back on the info for them?

Please can you help further - I've just read a small tutorial on arrays but it's not enlightened me anymore - thanks -

M

dreamcatcher

12:22 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to collect all the id's people are interested in send it to a new page and then say right now run a query using these id's and get back on the info for them?

Yep, you`ve pretty much got the logic.

Whichever page your form action points to is the page you run the query. Might be the same page refreshing or a new page. Basically using the [] operators in the checkbox name creates an array of ids.

You can see the array by doing the following after you process the form with the checkboxes:

$course = $_POST['course'];

print_r($course);

The implode function returns the data as a comma delimited string. So, if someone checked courses 1 & 3, this would be your array output:

Array
(
[0] => 1
=> 3
)

If you implode that you get:
1,3

Try this little snippet to see how its working:


<?php

if (isset($_POST['submit']))
{
$course = $_POST['course'];

echo '<b>Array Values:</b><br><br>';
print_r($course);

echo '<br><br><b>Imploded Array:</b><br><br>';
echo implode(",", $course);

}

?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
1. <input type="checkbox" name="course[]" value="1">
2. <input type="checkbox" name="course[]" value="2">
3. <input type="checkbox" name="course[]" value="3">
<input type="submit" name="submit" value="Compare">
</form>

Just copy the following into a text editor, save as a .php file, then run in your browser:

dc

[1][edited by: dreamcatcher at 12:36 pm (utc) on May 18, 2006]

JuiceUK

12:31 pm on May 18, 2006 (gmt 0)

10+ Year Member



ahhhh think I'm understanding this now - right - will be back asap - just trying it out :)

Thanks again

M

dreamcatcher

12:37 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good luck. The reason you need to implode the data is for the sql query. It will fail otherwise.

dc

JuiceUK

1:13 pm on May 18, 2006 (gmt 0)

10+ Year Member



Right - totally confused now :)

I think I have the first page sorted -

$result = mysql_query($query,$connection);
if (mysql_num_rows($result)) {
?>
<p>We have found <strong><?php $count = mysql_num_rows($result); echo $count;?></strong> courses matching your search criteria <?php if (!empty($sw)) {?>'<strong><?php echo $sw;?></strong>'<?php }?>.</p>
<form method="POST" action="compare.php">

<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence / Duration</th><th>Reference</th><th>Compare</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr><td><a href="/courses/<?php echo $row['course_id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?> / <?php echo $row['duration'];?></td><td><?php echo $row['reference'];?></td><td><input type="checkbox" name="course[]" value="<?php echo $row['course_id'];?>"></td></tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" name="submit" value="Compare">
</form>
<?php
} else {
?>

In my compare page I've tried doing this - it's errors for fun -

<?php
// need to pass on $school_id as a variable before calling this file

include('common.php');

$connection = mysql_connect($dbhost,$dbuser,$dbpasswd);
$db = mysql_select_db($dbname,$connection);

if (isset($_POST['submit']))
{
$course = $_POST['course'];

$query = 'SELECT *, c.id as course_id, q.name AS qualification_name, s.name AS school_name, s.url AS school_url FROM courses AS c, qualifications AS q, schools AS s WHERE course_id = IN(".implode(",", $_POST['id'])") AND active = 1 ORDER BY title';
$result = mysql_query($query,$connection) or die(mysql_error());

// close the database
mysql_close();

$total=mysql_num_rows($result);

// error checking: did the query bring up any results?
if ($total<=0) {
?>
<h2>No compare</h2>
<p>no compare</p>
<?php
} else {
?>
<h2>Compare</h2>
<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence / Duration</th><th>Reference</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><a href="/courses/<?php echo $row['course_id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?> / <?php echo $row['duration'];?></td><td><?php echo $row['reference'];?></td>
</tr>
<?php
} // end of while loop
?>
</tbody>
</table>
<?php
} // end of if($total<=0) else
?>

at the moment I know if it did work it would just list the same stuff again but I'm just trying to get it to only show the ones I have picked - then I can work out how to put them side by side etc

dreamcatcher

1:16 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



course_id = IN

should be:

course_id IN

dc

JuiceUK

1:25 pm on May 18, 2006 (gmt 0)

10+ Year Member



Hi again - this is all my code for the compare page -

<h1>Courses</h1>
<?php
// need to pass on $school_id as a variable before calling this file

$connection = mysql_connect($dbhost,$dbuser,$dbpasswd);
$db = mysql_select_db($dbname,$connection);

if (isset($_POST['submit']))
{
$course = $_POST['course'];

$query = 'SELECT *, c.id as course_id, q.name AS qualification_name, s.name AS school_name, s.url AS school_url FROM courses AS c, qualifications AS q, schools AS s WHERE course_id IN(".implode(",", $_POST['id'])") AND active = 1 ORDER BY title';
$result = mysql_query($query,$connection) or die(mysql_error());

// close the database
mysql_close();

$total=mysql_num_rows($result);

// error checking: did the query bring up any results?
if ($total<=0) {
?>
<!--sphider_noindex-->
<h2>No compare</h2>
<p>no compare</p>
<!--/sphider_noindex-->
<?php
} else {
?>
<!--sphider_noindex-->
<h2>Courses</h2>
<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence / Duration</th><th>Reference</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><a href="/courses/<?php echo $row['course_id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?> / <?php echo $row['duration'];?></td><td><?php echo $row['reference'];?></td>
</tr>
<?php
} // end of while loop
?>
</tbody>
</table>
<!--/sphider_noindex-->
<?php
} // end of if($total<=0) else
?>

why is submit used in the below? - I can see why course is used as we are getting the array course from the other pages tick box?:

if (isset($_POST['submit']))
{
$course = $_POST['course'];

In the mysql query - IN(".implode(",", $_POST['id'])") - what does the id refer to?

$query = 'SELECT *, c.id as course_id, q.name AS qualification_name, s.name AS school_name, s.url AS school_url FROM courses AS c, qualifications AS q, schools AS s WHERE course_id IN(".implode(",", $_POST['id'])") AND active = 1 ORDER BY title';

Sorry it doesn't work still - driving me mad :)

M

Thanks for your help

dreamcatcher

1:41 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The submit was checking if the form was processed. Its not needed, just depends on how you are processing.

The $_POST['id'] is in fact incorrect, sorry about that.

Try changing it to:

IN(".implode(",", $custom)")

dc

JuiceUK

1:56 pm on May 18, 2006 (gmt 0)

10+ Year Member



We are getting that little bit closer - it no longer errors - I changed the $custom to $course thinking we need the info set in $course = $_POST['course']; to be put in that place - I now tick my boxes press compare and it goes straight to:

<h2>No compare</h2>
<p>no compare</p>

So from this I gather it thinks there isn't any records with these id's? but there is?

I've put all the code as it stands below for the compare page - eeekkk sorry to keep on asking you to look at it.

<?php

$connection = mysql_connect($dbhost,$dbuser,$dbpasswd);
$db = mysql_select_db($dbname,$connection);

$course = $_POST['course'];

$query = 'SELECT *, q.name AS qualification_name, s.name AS school_name, s.url AS school_url FROM courses AS c, qualifications AS q, schools AS s WHERE c.qualification_id = q.id AND c.school_id = s.id AND c.active = 1 AND c.id IN(".implode(",", $course)") ORDER BY title';
$result = mysql_query($query,$connection) or die(mysql_error());

// close the database
mysql_close();

$total=mysql_num_rows($result);

// error checking: did the query bring up any results?
if ($total<=0) {
?>
<!--sphider_noindex-->
<h2>No compare</h2>
<p>no compare</p>
<!--/sphider_noindex-->
<?php
} else {
?>
<!--sphider_noindex-->
<h2>Courses</h2>
<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence / Duration</th><th>Reference</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><a href="/courses/<?php echo $row['course_id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?> / <?php echo $row['duration'];?></td><td><?php echo $row['reference'];?></td>
</tr>
<?php
} // end of while loop
?>
</tbody>
</table>
<!--/sphider_noindex-->
<?php
} // end of if($total<=0) else
?>

dreamcatcher

3:31 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JuiceUK, I do apologise, I`m working on a project and getting my variables mixed up with yours. $course is correct.

Try changing your syntax to:

WHERE (c.id IN(".implode(",", $course)")) AND c.qualification_id = q.id AND c.school_id = s.id AND c.active = 1

See if that helps.

Failing that you might need to join the tables.

dc

JuiceUK

8:52 am on May 19, 2006 (gmt 0)

10+ Year Member



Sorry still doesn't work - this is my code as it stands now:

<div id="content">
<h1>Compare</h1>
<?php

$connection = mysql_connect($dbhost,$dbuser,$dbpasswd);
$db = mysql_select_db($dbname,$connection);

$course = $_POST['course'];

$query = 'SELECT *, q.name AS qualification_name, s.name AS school_name, s.url AS school_url FROM courses AS c, qualifications AS q, schools AS s WHERE (c.id IN(".implode(",", $course)")) AND c.qualification_id = q.id AND c.school_id = s.id AND c.active = 1 ORDER BY title';
$result = mysql_query($query,$connection) or die(mysql_error());

// close the database
mysql_close();

$total=mysql_num_rows($result);

// error checking: did the query bring up any results?
if ($total<=0) {
?>
<!--sphider_noindex-->
<p>no compare</p>
<!--/sphider_noindex-->
<?php
} else {
?>
<!--sphider_noindex-->
<h2>Courses</h2>
<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence / Duration</th><th>Reference</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><a href="/courses/<?php echo $row['course_id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?> / <?php echo $row['duration'];?></td><td><?php echo $row['reference'];?></td>
</tr>
<?php
} // end of while loop
?>
</tbody>
</table>
<!--/sphider_noindex-->
<?php
} // end of if($total<=0) else
?>

I've found these articles around the web - do you think I should implode the array before I put it in the query?

[phpfreaks.com...]

more example:

handy use of implode in a MySQL query

<?php
$id_nums = array(1,6,12,18,24);

$id_nums = implode(" OR user_id=", $id_nums);

$sqlquery = "Select name,email,phone from usertable where user_id=$id_nums";

// $sqlquery becomes "Select name,email,phone from usertable where user_id=1 OR user_id=6 OR user_id=12 OR user_id=18 OR user_id=24"
?>

dreamcatcher

9:43 am on May 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn`t make any difference. Try breaking the query down little by little to see which part is causing no data to be returned.

dc

JuiceUK

11:39 am on May 19, 2006 (gmt 0)

10+ Year Member



Hi DC - thanks for taking your time over this. I stipped it out and it works - but I have to do it slightly different to how you are explaining as it just doesn't seem to work otherwise. This is my code as it stands now. I've imploded the info and then popped it into place in the query - the only way I could get this to work was to use id = '.$courseid.' to bring it in - I don't know if this is the right way of doing things as I have seen on other examples just using id = $courseid but this just errors? I have to use '. .' to bring it in? Problem with hashing the ode in rather than knowing what it's doing eeek.

I've echod the query and the course id and they look good and the data is showing. Problem I'm getting now is that when I view source in firefox after it's all ran - it looks like this:

<div id="content">
<h1>Compare courses</h1>
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY title' at line 1

The page shows up great everthing I want but the page doesn't validate and the code in view source suggests it's erroring in the mysql. This is why I echoed the query to see what it was doing. I popped the query straight into mysql editor I use and ran it and it pulled up the info with no errors?

I have also noticed that when I go straight to the compare page it trys to run the query and shows the mysql error on the page rather than go to my else section?

Can you help bud?

Thanks so much -

<div id="content">
<h1>Compare courses</h1>
<?php
include('common.php');

$course = $_POST['course'];

$courseid = implode (' OR id = ', $course);

$connection = mysql_connect($dbhost,$dbuser,$dbpasswd);
$db = mysql_select_db($dbname,$connection);

echo $courseid;

$query = 'SELECT * FROM courses WHERE active = 1 AND id = '.$courseid.' ORDER BY title';
$result = mysql_query($query,$connection) or die(mysql_error());

echo $query;

// close the database
mysql_close();

$total=mysql_num_rows($result);

// error checking: did the query bring up any results?
if ($total<=0) {
?>
<!--sphider_noindex-->
<p>no compare</p>
<!--/sphider_noindex-->
<?php
} else {
?>
<!--sphider_noindex-->
<table cellspacing="0" class="stripe">
<thead>
<tr><th>Title / Qualification</th><th>Price</th><th>Attendence</th><th>Points</th></tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><a href="/courses/<?php echo $row['id'];?>"><?php echo $row['title'].' / '.$row['qualification_name'];?></a></td><td><?php echo $row['price'];?></td><td><?php if ($row['mode_of_attendance']==1) {?>Full time<?php }?>
<?php if ($row['mode_of_attendance']==0) {?>Part time<?php }?></td><td><?php echo $row['points'];?></td>
</tr>
<?php
} // end of while loop
?>
</tbody>
</table>
<!--/sphider_noindex-->
<?php
} // end of if($total<=0) else
?>

</div>

dreamcatcher

1:13 pm on May 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mmm..could it be with the apostrophes or something?

Try enclosing the query in parenthesis:

$query = 'SELECT * FROM courses WHERE active = 1 (AND id = '.$courseid.') ORDER BY title';

or now that you have simplified it down, try the original IN option. That should work ok:

$courseid = implode(",",$_POST['course']);

$query = 'SELECT * FROM courses WHERE id IN ($courseid) AND active = '1' ORDER BY title';

dc

JuiceUK

2:18 pm on May 25, 2006 (gmt 0)

10+ Year Member



Thanks DC - this works great - I really appreciate you sticking with me on it.

M