...

View Full Version : sort by dropdown?



markman641
08-28-2011, 11:51 PM
how would i get it so i can display a dropdown, and the user can select a value in the dropdown and clicks a button, and after that it displays only values in the database that match what the user selected? Something like this..


Sort By: <select name="category">
<option value="$1ClickFamily">1Click Family</option>
<option value="$1stPage/EmailSubmits">1st Page/Email Submits</option>
<option value="$AbsoluteRewards">Absolute Rewards</option>
<option value="$BlazingRewards">Blazing/Nextday/WayRewards</option>
<option value="$DailySurveys">Daily Surveys</option>
<option value="$Downloads">Downloads</option>
<option value="$Eversave">Eversave</option>
<option value="$HealthOffers">Health Offers</option>
<option value="$InsuranceQuotes">Insurance Quotes</option>
<option value="$QuizJungles">Quiz Jungles</option>
<option value="$Registration">Registration</option>
<option value="$Downloads">Downloads</option>
<option value="$SurveyPanels">Survey Panels</option>
<option value="$ThinkFast">Think Fast</option>
<option value="$WinningSurveys">Winning Surveys</option>
<option value="$ZZZQuiz">ZZZ Quiz</option>
</select>

$get_name = mysql_query("SELECT * from `offers` WHERE `category` = '$selectedvalue' & ORDER BY id DESC");

I KNOW thats completely wrong i just didnt know how to explain it :P

Old Pedant
08-29-2011, 12:04 AM
It's not really wrong. Just missing the important step:


$selectedvalue = $_GET["category"];
or
$selectedvalue = $_POST["category"];

depending on whether your <form> is coded as method="get" or method="post"

Though I don't understand the point of the $ characters in front of all the category values, unless you really do have $ characters in your database in that category field.

markman641
08-29-2011, 12:25 AM
yeah, but what would i make the <form> action?

wait, so on the page i want the dropdown thing, what would i put?

and would i make another file that the form uses? XD

markman641
08-29-2011, 02:02 AM
this is what i got so far, its not working, its searching for "category"


<P ALIGN="RIGHT"><select name="category">
<option value="1Click Family">1Click Family</option>
<option value="1st Page/Email Submits">1st Page/Email Submits</option>
<option value="Absolute Rewards">Absolute Rewards</option>
<option value="BlazingRewards">Blazing/Nextday/WayRewards</option>
<option value="Daily Surveys">Daily Surveys</option>
<option value="Downloads">Downloads</option>
<option value="Eversave">Eversave</option>
<option value="Health Offers">Health Offers</option>
<option value="Insurance Quotes">Insurance Quotes</option>
<option value="Quiz Jungles">Quiz Jungles</option>
<option value="Registration">Registration</option>
<option value="Downloads">Downloads</option>
<option value="Survey Panels">Survey Panels</option>
<option value="Think Fast">Think Fast</option>
<option value="Winning Surveys">Winning Surveys</option>
<option value="ZZZ Quiz">ZZZ Quiz</option>
</select>
<?
$selectedvalue = $_GET["category"];
?>
<form name="form" action="searchcategory.php" method="get">
<input name = "q" type="submit" name="Submit" value="Category" />
</form></P>

Old Pedant
08-29-2011, 02:30 AM
The <select> *MUST* be between the <form> and the </form> !!!

Also, you need to provide for the case when the page first appears and no category has yet been chosen.



<form ...>
<select ...>
</select>
<input type="submit" ...>
</form>
<?php
$selectedvalue = $_GET["category"];
if ( isset($selectedvalue) )
{
// ** NO AMPERSAND IN THE SQL!!! **
$sql = "SELECT * from `offers` WHERE `category` = '$selectedvalue' ORDER BY id DESC";

$get_name = mysql_query($sql) ...
... dump out results ...

}
?>

Or, if you want to show *all* categories when no <option> is yet chosen, maybe


<form ...>
<select ...>
</select>
<input type="submit" ...>
</form>
<?php
$selectedvalue = $_GET["category"];
if ( isset($selectedvalue) )
{
$sql = "SELECT * from offers WHERE category = '$selectedvalue' ORDER BY id DESC";
} else {
$sql = "SELECT * from offers ORDER BY id DESC";
}
$get_name = mysql_query($sql) ...
... dump out results ...

?>

Up to you.

But in any case, no <form> fields are ever posted back to server if they don't appear in the HTML between <form> and </form>. Or if they don't have a name=

markman641
08-29-2011, 02:50 AM
i thought it would bring me to another page.. i dunno im really confused.

webdev1958
08-29-2011, 02:55 AM
It will take you to whatever page you have specified in the form's action attribute.

markman641
08-29-2011, 02:57 AM
but... then why is the SELECT on that page? *confused*

webdev1958
08-29-2011, 03:06 AM
It depends on what you want to do.

Consider this scenario which is what I am assuming you have.

1 - You have a php page containing a <form> with a <select> inside the form.

2 - the user makes a selection from the <select> and clicks a submit button

3 - the form then sends the user's selection to the php script specified in the form's action attribute. This php script can be the same php script that contains the form or it could be a separate php script.

4 - in either case, the script processing the form data extracts the data from the database for the user's selection and then outputs the records in the browser.

So the first thing you need to decide is whether the form data processing script in 3 is to be the same script containing the original <form> or a separate script all together.

markman641
08-29-2011, 03:11 AM
1 - You have a php page containing a <form> with a <select> inside the form.

2 - the user makes a selection from the <select> and clicks a submit button

3 - the form then sends the user's selection to the php script specified inthe form's action attribute. This php script can be the same php script that contains the form or it could be a separate php script.

4 - in either case, the script processing the form data extracts the data from the database for the user's selection and then outputs the records in the browser.

that is exactly right. i have a search script called categorysearch.php, but when it tries to search it searches for the term "Category" instead of the users selected value.

webdev1958
08-29-2011, 03:20 AM
You'll have to post the actual query being run to debug it, but in theory if the user selects the Absolute Rewards optin then your query should evaluate to


SELECT * from `offers` WHERE `category` = 'Absolute Rewards' & ORDER BY id DESCTo help debug your code, do the following


$query = "SELECT * from `offers` WHERE `category` = '$selectedvalue' ORDER BY id DESC";

echo $query; //this will display the actual query about to be run

die(); //stop further execution


and post the output from echo statement.

btw - I removed the ampersand that was in your original code. I don't know why you had it there.

markman641
08-29-2011, 03:43 AM
Okay, i give up this is more complicated then i thought. I'm just going to make a new page for each category then just have the dropdown link to it.

It sounds so simple. select something from dropdown then display offers that match the category on a new page.

webdev1958
08-29-2011, 04:00 AM
Okay, i give up this is more complicated then i thought.

It doesn't have to be.

If you put this code in a file called searchCategory.php and insert your code to connect to the db where I have commented to do so, it should work when you substitute your own table field names for col1, col2 etc


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
</head>
<body>
<form action="searchCategory.php" method="post">
<select name="category">
<option value="1Click Family">1Click Family</option>
<option value="1st Page/Email Submits">1st Page/Email Submits</option>
<option value="Absolute Rewards">Absolute Rewards</option>
<option value="BlazingRewards">Blazing/Nextday/WayRewards</option>
<option value="Daily Surveys">Daily Surveys</option>
<option value="Downloads">Downloads</option>
<option value="Eversave">Eversave</option>
<option value="Health Offers">Health Offers</option>
<option value="Insurance Quotes">Insurance Quotes</option>
<option value="Quiz Jungles">Quiz Jungles</option>
<option value="Registration">Registration</option>
<option value="Downloads">Downloads</option>
<option value="Survey Panels">Survey Panels</option>
<option value="Think Fast">Think Fast</option>
<option value="Winning Surveys">Winning Surveys</option>
<option value="ZZZ Quiz">ZZZ Quiz</option>
</select>
<input type="submit" value="Submit" />
</form>
<div id="results">
<?php
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') { //check if submit button was clicked
//insert your code to connect to your database here

$selectedVal = $_POST['category'];
$query = 'SELECT * from offers WHERE category = "' . $selectedvalue . '" ORDER BY id DESC';
$rs = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($rs)) {
echo $row['col1'] . ' ' . $row['col2'] . '<br />'; //col1, col2 are the columns in category table
}
mysql_free_result($rs);
mysql_close($conn);
}
?>
</div>
</body>
</html>

I haven't bothered to include any data validation/sanitisation at this stage - rather stay with the KISS principle for now.

Old Pedant
08-29-2011, 04:11 AM
See, from your post #4 I just *assumed* that the page with the <select> and the page that *used* the selection were/are one and the same.

YOU are the one who put the mysql_query line of code into the same page of code as the <select>. If you want them to be different pages, then need that mysql_query to be on the page that is specified in the action= of the <form>.

Read again what webdev said in his post #9. You *CAN* do it either way. Honest. It's 100% your choice. Webdev's code in post #13 is clean and clear and a way to do it all in one page. If you really want two pages, though, just ask. It's a truly minor mod to that code.

markman641
08-29-2011, 04:12 AM
didnt work... just refreshed the page.

webdev1958
08-29-2011, 04:23 AM
didnt work... just refreshed the page.

ok, but I'm not sure how you expect anyone to advise you on what you have done wrong if you don't post your code.

markman641
08-29-2011, 04:31 AM
ok, but I'm not sure how you expect anyone to advise you on what you have done wrong if you don't post your code.

i didnt change anything besides the columns. i put include config.php which has my database info.

webdev1958
08-29-2011, 04:37 AM
if that's the only changes you made then there's your problem. Have a look at the variable name for $conn your config file uses, or remove $conn from the code.

If you're not game to post your code, there isn't any more I can do to help you fix your errors.

Good luck :)

markman641
08-29-2011, 05:22 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
</head>
<body>
<form action="searchcategory.php" method="post">
<select name="category">
<option value="1Click Family">1Click Family</option>
<option value="1st Page/Email Submits">1st Page/Email Submits</option>
<option value="Absolute Rewards">Absolute Rewards</option>
<option value="BlazingRewards">Blazing/Nextday/WayRewards</option>
<option value="Daily Surveys">Daily Surveys</option>
<option value="Downloads">Downloads</option>
<option value="Eversave">Eversave</option>
<option value="Health Offers">Health Offers</option>
<option value="Insurance Quotes">Insurance Quotes</option>
<option value="Quiz Jungles">Quiz Jungles</option>
<option value="Registration">Registration</option>
<option value="Downloads">Downloads</option>
<option value="Survey Panels">Survey Panels</option>
<option value="Think Fast">Think Fast</option>
<option value="Winning Surveys">Winning Surveys</option>
<option value="ZZZ Quiz">ZZZ Quiz</option>
</select>
<input type="submit" value="Submit" />
</form>
<div id="results">
<?php
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') { //check if submit button was clicked
//insert your code to connect to your database here
$con = mysql_connect("localhost","*******","********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$selectedVal = $_POST['category'];
$query = 'SELECT * from offers WHERE category = "' . $selectedvalue . '" ORDER BY id DESC';
$rs = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($rs)) {
echo $row['name'] . ' ' . $row['link'] . '<br />'; //col1, col2 are the columns in category table
}
mysql_free_result($rs);
mysql_close($conn);
}
?>

</div>
</body>
</html>

btw, i highlighted something i think is an error. should it be $selectedVal not $selectedvalue

webdev1958
08-29-2011, 08:30 AM
1 - change $conn to $con

2 - change $selectedvalue to $selectedVal //this one is my typo

markman641
08-29-2011, 11:58 PM
Still doesn't work. Just refreshes the page still.

Old Pedant
08-30-2011, 02:09 AM
Add in DEBUG DEBUG DEBUG code:


<div id="results">
<?php
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
echo "DEBUG: form submit detected<hr>";
$con = mysql_connect("localhost","*******","********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$selectedVal = $_POST['category'];
echo "DEBUG: category posted from form was ==" . $selectedVal . "==<hr>";
$query = 'SELECT * from offers WHERE category = "' . $selectedvalue . '" ORDER BY id DESC';
echo "DEBUG SQL: " . $query . "<hr>";
$rs = mysql_query($query, $conn) or die("Error in SQL query");
echo "DEBUG: after mysql_query<hr>";
while ($row = mysql_fetch_assoc($rs)) {
echo $row['name'] . ' ' . $row['link'] . '<br />';
}
echo "DEBUG: after while loop on rows<hr>";
mysql_free_result($rs);
mysql_close($conn);
}
?>
</div>

And if that's not enough DEBUG, add more! If you can't debug, you can't program. As much as 90% of many programming projects are planning and debugging and testing time, with only 10% for coding.

And if I made a typo or two, you can fix them.

markman641
08-30-2011, 06:00 AM
lulz, nothing showed up XD

Old Pedant
08-30-2011, 07:58 AM
??? NOTHING???

Are you sure you have PHP running on that server?

markman641
08-31-2011, 02:09 AM
yes, im sure hahahahahaha

Old Pedant
08-31-2011, 02:24 AM
Okay, still more debug...though I would have that by now you would have tried it:


<div id="results">
DEBUG: this is just before the php code<hr>
<?php
echo "DEBUG: begin the php code<hr>";
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
echo "DEBUG: form submit detected<hr>";
$con = mysql_connect("localhost","*******","********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$selectedVal = $_POST['category'];
echo "DEBUG: category posted from form was ==" . $selectedVal . "==<hr>";
$query = 'SELECT * from offers WHERE category = "' . $selectedvalue . '" ORDER BY id DESC';
echo "DEBUG SQL: " . $query . "<hr>";
$rs = mysql_query($query, $conn) or die("Error in SQL query");
echo "DEBUG: after mysql_query<hr>";
while ($row = mysql_fetch_assoc($rs)) {
echo $row['name'] . ' ' . $row['link'] . '<br />';
}
echo "DEBUG: after while loop on rows<hr>";
mysql_free_result($rs);
mysql_close($conn);
}
?>
DEBUG: this is just after all the php code
</div>

Which of *those* debug messages do you see, both before you submit the <form> and then after it is submitted?

markman641
08-31-2011, 04:44 AM
I think I'm putting that code in the wrong spot because before i do anything i see
DEBUG: this is just before the php code DEBUG: begin the php code DEBUG: this is just after all the php code



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
</head>
<body>
<form action="searchcategory.php" method="post">
<select name="category">
<option value="1Click Family">1Click Family</option>
<option value="1st Page/Email Submits">1st Page/Email Submits</option>
<option value="Absolute Rewards">Absolute Rewards</option>
<option value="BlazingRewards">Blazing/Nextday/WayRewards</option>
<option value="Daily Surveys">Daily Surveys</option>
<option value="Downloads">Downloads</option>
<option value="Eversave">Eversave</option>
<option value="Health Offers">Health Offers</option>
<option value="Insurance Quotes">Insurance Quotes</option>
<option value="Quiz Jungles">Quiz Jungles</option>
<option value="Registration">Registration</option>
<option value="Downloads">Downloads</option>
<option value="Survey Panels">Survey Panels</option>
<option value="Think Fast">Think Fast</option>
<option value="Winning Surveys">Winning Surveys</option>
<option value="ZZZ Quiz">ZZZ Quiz</option>
</select>
<input type="submit" value="Submit" />
</form>
<div id="results">
DEBUG: this is just before the php code<hr>
<?php
echo "DEBUG: begin the php code<hr>";
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
echo "DEBUG: form submit detected<hr>";
$con = mysql_connect("localhost","*******","********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$selectedVal = $_POST['category'];
echo "DEBUG: category posted from form was ==" . $selectedVal . "==<hr>";
$query = 'SELECT * from offers WHERE category = "' . $selectedvalue . '" ORDER BY id DESC';
echo "DEBUG SQL: " . $query . "<hr>";
$rs = mysql_query($query, $conn) or die("Error in SQL query");
echo "DEBUG: after mysql_query<hr>";
while ($row = mysql_fetch_assoc($rs)) {
echo $row['name'] . ' ' . $row['link'] . '<br />';
}
echo "DEBUG: after while loop on rows<hr>";
mysql_free_result($rs);
mysql_close($conn);
}
?>
DEBUG: this is just after all the php code
</div>
</body>
</html>

Old Pedant
08-31-2011, 08:04 PM
Nope. That's what you wanted to see.

Now, if that is *all* you saw, then that must have been *BEFORE* you hit the SUBMIT button.

What happens when you select a category and hit SUBMIT???

One thing strikes me: What is the *NAME* of the page you are showing us here???

It *SHOULD* be searchcategory.php

That is, we have both been *expecting* that your <form> will be posting back to the *SAME PAGE*. If not, then no wonder nothing has been working.

If that's not the name of your page, then for now just remove the action= from your <form> tag, thus:


<form method="post">

markman641
09-01-2011, 01:27 AM
yeah that's what the page is. after i select and click submit the page reloads.

Old Pedant
09-01-2011, 01:43 AM
Which it should.

But *THEN* it should hit THIS code

if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
echo "DEBUG: form submit detected<hr>";

and so should, at a minimum, display that "DEBUG: form submit detected<hr>".

If it is not doing that, then you have a PHP problem I don't understand. It certainly has nothing to do with MySQL.

markman641
09-01-2011, 05:08 AM
nope it doesn't

Old Pedant
09-01-2011, 05:38 PM
Okay, so you'll have to get a PHP expert to look at it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum