Page 1 of 1

Add All option to jump menu

Posted: Thu Aug 12, 2010 9:23 am
by mrlayance
I have this code work well, but a "ALL" option would be nice. I have seen this done before but not sure how.

here is my code.

Code: Select all

<?php

// Connect to your MySQL database
require "dbconnect.php";

// Select the data
$select = mysql_query( "SELECT `agency` FROM csv WHERE `agencytype` = 'something' GROUP BY `agency`") or die("SELECT Error: ".mysql_error());

?>

<select onChange="window.open(this.options[this.selectedIndex].value,'_top')">

<?

// Begin grabbing the rows from your database
while ($row = mysql_fetch_array($select)) {

?>

<option value="queryws.php?agency=<? echo $row['agency']; ?>">

<? echo $row['agency']; ?>

</option>

<?

} 

?>

</select>
The best example I can find is as follows, but I can seem to wrap my head around it. Might just need some more coffee...

Code: Select all

if ((!empty($_GET['agency']))&&($_GET['agency'] != 'all')) 
  { 
    $sql .= " and agency like '". addslashes($_GET['agency'])."%' "; 
  } 

Re: Add All option to jump menu

Posted: Thu Aug 12, 2010 10:23 am
by yacahuma
What do you mean by all option? I dont understand your question

Re: Add All option to jump menu

Posted: Thu Aug 12, 2010 10:33 am
by mrlayance
yacahuma wrote:What do you mean by all option? I dont understand your question
Yes, All option.

Re: Add All option to jump menu

Posted: Thu Aug 12, 2010 11:39 am
by yacahuma

Code: Select all


$agency = mysql_real_escape_string($_GET['agency']);

$sql = 'select * from table where 1=1';
if ($agency  == 'all')
  $sql .= '';
else
    $sql .= " and agency like '%{$agency}%'";

Re: Add All option to jump menu

Posted: Thu Aug 12, 2010 12:35 pm
by mrlayance
I keep getting "mysql_fetch_array(): supplied argument is not a valid MySQL result resource" in line:

while($row = mysql_fetch_array($result)) {

If possible could you show me how this fits in the code above?
Thanks again...

Re: Add All option to jump menu

Posted: Thu Aug 12, 2010 1:42 pm
by yacahuma
do an echo on your query, to see what exactly are you running

Code: Select all

echo $sql;

Re: Add All option to jump menu

Posted: Fri Aug 13, 2010 7:55 am
by mrlayance
Not sure if the sample code will work. The form with the jump box is the form pass the variable to the next page that querys all the info.

Code: Select all

$agency = mysql_real_escape_string($_GET['agency']);
Not sure how I would get the agency.

Does that sound correct?

Re: Add All option to jump menu

Posted: Sat Aug 14, 2010 6:53 am
by yacahuma
mysql_real_escape_string is just protecting your input. You will get the value that you are passing. You should never use the input without some filtering.