Jump to content

[SOLVED] Ignore value from mysql


Alexhoward

Recommended Posts

Hi Guys,

 

I have a drop down populated from mysql...

 

(these things have taken over all my time lately!  :D )

 

Some how thou i would like to ignore certain values.

 

e.g. say the table includes - a, b, c, d, e ...

 

I would like to pull everything back apart from d

 

can anyone help?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/109800-solved-ignore-value-from-mysql/
Share on other sites

Hi Guys,

 

Thanks for taking the time to look at this

 

<?php

//Select Category

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());


echo'<form><select name="cat" style="width:160px;">';

$res=mysql_query("select distinct cat from category WHERE cat !='Add_Category..?' order by cat");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
$selected = @$_GET['cat'] == $row['cat'] ? "selected" : "";
echo"<option value=".$row['cat']." $selected>".$row['cat']."</option>";
}
echo'</select>';
echo'<input type="submit" value="select"></form>';

?>

I'd recommend you to change

for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
$selected = @$_GET['cat'] == $row['cat'] ? "selected" : "";
echo"<option value=".$row['cat']." $selected>".$row['cat']."</option>";
}

 

to

while($row = mysql_fetch_assoc($res))
{
    $selected = ((isset($_GET['cat']) && $_GET['cat'] == $row['cat']) ? ' selected="selected"' : null;

    echo '<option value="'.$row['cat'].'"' . $selected .'>'.$row['cat']."</option>\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.