Add All option to jump menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Add All option to jump menu

Post 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'])."%' "; 
  } 
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Add All option to jump menu

Post by yacahuma »

What do you mean by all option? I dont understand your question
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Re: Add All option to jump menu

Post by mrlayance »

yacahuma wrote:What do you mean by all option? I dont understand your question
Yes, All option.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Add All option to jump menu

Post 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}%'";
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Re: Add All option to jump menu

Post 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...
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Add All option to jump menu

Post by yacahuma »

do an echo on your query, to see what exactly are you running

Code: Select all

echo $sql;
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Re: Add All option to jump menu

Post 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?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Add All option to jump menu

Post 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.
Post Reply