Forum Moderators: coopster
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
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
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
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 If you implode that you get: Try this little snippet to see how its working: if (isset($_POST['submit'])) echo '<b>Array Values:</b><br><br>'; echo '<br><br><b>Imploded Array:</b><br><br>'; } ?> 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]
(
[0] => 1
=> 3
)
1,3
<?php
{
$course = $_POST['course'];
print_r($course);
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>
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
<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
<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
?>
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
<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"
?>
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>
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