Jump to content

Recommended Posts

$query = 'SELECT id, category FROM categories';
$result = mysql_query($query, $db);
if ($result) {
    $htmlSelect = '<select name="category-select">';
    while (list($id, $category) = mysql_fetch_array($result, MYSQL_NUM)) {
        $htmlSelect .= "<option value=\"$id\">$category</option>";
    }
    $htmlSelect .= '</select>';
}

 

<form action="" method="post">
    ...
    <label>Category: <?php print $htmlSelect; ?></label>
    ...
</form>

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874446
Share on other sites

Is this right?

function select_db(){
mysql_connect("localhost","login","pass")or die(mysql_error());
mysql_select_db("dbname")or die(mysql_error());
}
$query = 'SELECT id, Category FROM categories';
$result = mysql_query($query, $db);
if ($result) {
    $htmlSelect = '<select name="category-select">';
    while (list($id, $category) = mysql_fetch_array($result, MYSQL_NUM)) {
        $htmlSelect .= "<option value=\"$id\">$category</option>";
    }
    $htmlSelect .= '</select>';
} 

 

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874462
Share on other sites

No, you never call you function to make the connection, nor do you pass your connection back out to where it can be useful.

 

Should always check your queries actually return records before trying to use them too.

 

function select_db(){
  $db = mysql_connect("localhost","login","pass")or die(mysql_error());
  mysql_select_db("dbname")or die(mysql_error());
  return $db;
}

$db = select_db();
$query = 'SELECT id, Category FROM categories';
$result = mysql_query($query, $db);
if ($result) {
  if (mysql_num_rows($result)) {
    $htmlSelect = '<select name="category-select">';
    while (list($id, $category) = mysql_fetch_array($result, MYSQL_NUM)) {
        $htmlSelect .= "<option value=\"$id\">$category</option>";
    }
    $htmlSelect .= '</select>';
  }
}

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874479
Share on other sites

I'm being a total newbie here, I've tried it like this, but it's obviously still wrong:

<?PHP
mysql_connect("dedi536.nur4.host-h.net","ivif09","ivifpa55")or die(mysql_error());
mysql_select_db("web_cat09")or die(mysql_error());
?>

getting the same mysql error

I appreciate any help you can give me

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874501
Share on other sites

Okay great, I've managed to populate my drop down:

<?PHP
$link = mysql_connect("host","login","pass");
mysql_select_db("dbname", $link);

$query = "select Category FROM categories"; 
$results = mysql_query($query, $link) or die("Error performing query"); 

if(mysql_num_rows($results) > 0){ 
echo("<select name=\"Category\">"); 
while($row = mysql_fetch_object($results)){ 
echo("<option value=\"$row->record_id\">$row->Category</option>"); 
} 
echo("</select>"); 
} 
else{ 
echo("<i>No values found</i>"); 
}?>

 

I now want to populate another drop down from a choice in this drop down using the WHERE query, can anyone offer me guidance?

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874519
Share on other sites

Assuming you want to select a subcategory:

 

$link = mysql_connect("host","login","pass");
mysql_select_db("dbname", $link);

$query = "select Category FROM categories";
$results = mysql_query($query, $link) or die("Error performing query");

if(mysql_num_rows($results) > 0){
    echo("<select name=\"Category\">");
    while($row = mysql_fetch_object($results)){
        echo("<option value=\"$row->record_id\">$row->Category</option>");
    }
    echo("</select>");
}
else{
    echo("<i>No values found</i>");
}

if (!empty($_POST)) {
    if (empty($_POST['subcategory'])) {
        $query = 'SELECT id, subcategory FROM subcategories';
        $result = mysql_query($query, $link);
        if (mysql_num_rows($result)) {// no need for > 0; if mysql_num_rows() == 0 then the if body won't be executed as 0 == false
            echo('<select name="subcategory">');
            while (list($id, $subcategory) = mysql_fetch_array($result, MYSQL_NUM)) {
                echo("<option value=\"$id\">$subcategory</option>");
            }
            echo('</select>');
        }
    } else if (!empty($_POST['category'] && !empty($_POST['subcategory'])) {
        $cat = $_POST['category'];
        $subcat = $_POST['subcategory'];
        ..process..
    }
}

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-874531
Share on other sites

Something is wrong with this piece of code here:

<?PHP
while (list($id, $coName) = mysql_fetch_array($result, MYSQL_NUM)) {
		echo("<option value=\"$id\">$coName</option>");
		}
		echo('</select>');
		}
	} else if (!empty($_POST['category'] && !empty($_POST['coName']))) {
		$cat = $_POST['category'];
		$subcat = $_POST['coName'];
		..process..
		}
	}
?>

I've added another ) after ['coName'], but I'm still receiving this error when I run the script?

Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ')'

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-875023
Share on other sites

Their is alot missing from this code:

 

..something here..
    ..something here..
       ..something here..
            while (list($id, $coName) = mysql_fetch_array($result, MYSQL_NUM)) {
                echo("<option value=\"$id\">$coName</option>");
            }
            echo('</select>');
        }
    } else if (!empty($_POST['category'] && !empty($_POST['coName'])) {
        $cat = $_POST['category'];
        $subcat = $_POST['coName'];
        ..process..
    }
}

Link to comment
https://forums.phpfreaks.com/topic/165774-drop-down/#findComment-875740
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.