Hello
I am learning PHP. I need help with the following code:
I have a phpmyadmin database with following fields: id, coursename, coursecode, coursedescription, instructor , courseyear , coursesemester,building, room.
I want to create a php page including a basic drop-down menu to sort the phpmyadmin table.
So far, I have created the drop-down menu form.
I have written the php code that displays all records within the database.
However I couldn`t tie the drop-down menu to the actual table records. Meaning that, when I pick a category from the drop-down menu(lets say I want to list courses in fall2008 or I want to show courses that John Doe teaches) it doesnt work. It always shows all records in the database.
I will really appreciate if you can help me out.
Best,
***************************
database connection part
***************************
<?php
session_start();
include_once("x.php");
$db = new Database("localhost", "xxx", "yyy", "zzz")or trigger_error(mysql_error(),E_USER_ERROR); ?>
***************
form part:
****************
<div class="pulldown">
<form method="GET" action="<?=$self?>" name="courseselect" >
Courses by:
<select id="q" name="q">
<optgroup label="Semesters">
<option value="fall2008" >Fall</option>
<option value="spring2008" >Spring</option>
<option value="summer2008" >Summer</option>
</optgroup>
<optgroup label="Instructors">
<option value="john">John Doe</option>
<option value="mary">Mary White</option>
<option value="tom">Tom Sawyer</option>
</optgroup>
</select>
<input type="submit" value="show »" />
</form>
</div>
***********************
php part:
***********************
<?
$result = mysql_query("SELECT DISTINCT id, coursename, coursecode, coursedescription, instructor, courseyear, coursesemester, building, room FROM coursesnew WHERE crse IN ( '2100', '2102', '2104') ORDER BY crse ASC;");
while( $c = mysql_fetch_object($result)) {
printf( '<div class="info">
<br><h2>%s%s - %s</h2><br>
%s <br />
</div>',
$c->coursename, $c->coursecode, $c->instructor, $c->room);
}
?>
Sorting a Php Table via a DropDown Menu
Moderator: General Moderators
-
postmanager
- Forum Newbie
- Posts: 6
- Joined: Tue Nov 11, 2008 2:21 am
Re: Sorting a Php Table via a DropDown Menu
First of all, phpMyAdmin is not a database, it's a control panel.
If you have successfully generated a drop down select box in a form, then your question seems to be, How do I write another script that uses the selection that the user has made, to filter records in a table, to be returned to a new HTML page. If that is substantially what you are asking, the answer is that when you generate the drop down select box, it should be within an HTML Form, and that Form should have a "method=post" parameter and an "action=....." parameter that determines what script will be executed when the Submit button is clicked. So it's that script we're talking about, OK? So, near the beginning of your script, you will have a line something like: (where 'selection' is whatever name you assigned to your drop down select box)
Now that you have a value for what was selected, you can use it in an SQL query to filter your table.
If you have successfully generated a drop down select box in a form, then your question seems to be, How do I write another script that uses the selection that the user has made, to filter records in a table, to be returned to a new HTML page. If that is substantially what you are asking, the answer is that when you generate the drop down select box, it should be within an HTML Form, and that Form should have a "method=post" parameter and an "action=....." parameter that determines what script will be executed when the Submit button is clicked. So it's that script we're talking about, OK? So, near the beginning of your script, you will have a line something like:
Code: Select all
$selection = mysql_real_escape_string($_POST['selection']);Now that you have a value for what was selected, you can use it in an SQL query to filter your table.
-
postmanager
- Forum Newbie
- Posts: 6
- Joined: Tue Nov 11, 2008 2:21 am
Re: Sorting a Php Table via a DropDown Menu
Thanks Califdon.
I have added $selection = mysql_real_escape_string($_POST['selection']); to the very beginning of the php part and changed "selection'" into "q" because I believe that this is the name for my dropdown box but it didnt work. What am I doing wrong?
I have added $selection = mysql_real_escape_string($_POST['selection']); to the very beginning of the php part and changed "selection'" into "q" because I believe that this is the name for my dropdown box but it didnt work. What am I doing wrong?
Re: Sorting a Php Table via a DropDown Menu
I couldn't begin to guess, since I don't know anything about your code, your database, or your application. I think you will have to find someone who understands PHP and who has the time to thoroughly analyze your application. I'm sorry, but I don't have that much time.postmanager wrote:Thanks Califdon.
I have added $selection = mysql_real_escape_string($_POST['selection']); to the very beginning of the php part and changed "selection'" into "q" because I believe that this is the name for my dropdown box but it didnt work. What am I doing wrong?