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
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>';

?>

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.