PDA

View Full Version : Can somebody please tell me why my menu only shows one sometimes


nanny
05-05-2010, 01:29 AM
Hi I am using someones code and trying to add a couple of menu lists.
There is code to build a category list with parent/child. The first menu list in the insert form shows all in the database:


$sql2 = "SELECT *
FROM size_w";

$result = dbQuery($sql2);
$row_size = dbFetchAssoc($result);



<select name="size_w" id="size_w">
<option value="">Size Width</option>
<?php do { ?>
<option value="<?php echo $row_size['size_w']; ?>"><?php echo $row_size['size_w']; ?></option>
<?php }while ($row_size = mysql_fetch_array($result)); ?>
</select>
However the next one will only show one in the list and there are two:


$sql3 = "SELECT *
FROM expires";

$result = dbQuery($sql3);
$row_expires = dbFetchAssoc($result);


<select name="expires" id="expires">
<option value="">Years</option>
<?php do { ?>
<option value="<?php echo $row_expires['expires']; ?>"><?php echo $row_expires['expires']; ?></option>
<?php }while ($row_expires = mysql_fetch_array($result)); ?>
</select>

I use the same for the edit page and both will only show the one that is in the database. I need it to show all options.

This is on the update page:


<select name="size_w" id="size_w">
<option value="">Size Width</option>
<?php do { ?>
<option value="<?php echo $row_size['size_w']; ?>"><?php echo $size_w; ?></option>
<?php }while ($row_size = mysql_fetch_array($result)); ?>
</select>

Can you please tell me what I am doing wrong. I even tried to do a similar list build as the categories but it would only print out array:

This is the categories build list:



// get product info
$sql = "SELECT pd.cat_id, pd_name, pd_description, pd_price, pd_qty, size_w, size_d, size_h, doors, expires, pd_image, pd_thumbnail
FROM tbl_product pd, tbl_category cat
WHERE pd.pd_id = $productId AND pd.cat_id = cat.cat_id";
$result = mysql_query($sql) or die('Cannot get product. ' . mysql_error());
$row = mysql_fetch_assoc($result);
extract($row);

// get category list
$sql = "SELECT cat_id, cat_parent_id, cat_name
FROM tbl_category
ORDER BY cat_id";
$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());



$categories = array();
while($row = dbFetchArray($result)) {
list($id, $parentId, $name) = $row;

if ($parentId == 0) {
$categories[$id] = array('name' => $name, 'children' => array());
} else {
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
}
}

//echo '<pre>'; print_r($categories); echo '</pre>'; exit;

// build combo box options
$list = '';
foreach ($categories as $key => $value) {
$name = $value['name'];
$children = $value['children'];

$list .= "<optgroup label=\"$name\">";

foreach ($children as $child) {
$list .= "<option value=\"{$child['id']}\"";

if ($child['id'] == $cat_id) {
$list .= " selected";
}
$list .= ">{$child['name']}</option>";
}

$list .= "</optgroup>";
}
then it echo's the $list. I have only used dreamweaver behaviour previously for menu's as I found it the easiest way to update.

Also if I move one select list above another it gives defined index error....confused...

tux
05-05-2010, 05:50 AM
Maybe this tutorial by Davidj could help you.

http://www.dreamweaverclub.com/vtm/php-dynamic-dropdowns.php

nanny
05-05-2010, 10:00 PM
yes that is how I got the menu list code from.... any other suggestions why it isn't showing more than one from database?

nanny
05-05-2010, 10:37 PM
I worked it out -:


$sql3 = "SELECT *
FROM expires";

$result = dbQuery($sql3);
$row_expires = dbFetchAssoc($result);


I changed the $result to $result3 and for the other select list $result2 and it works well except for the update how to have the one from the database selected:


<option value="">Size Width</option>
<?php do { ?>
<option value="<?php echo $row['size_w']; ?>"><?php echo $row['size_w']; ?></option>
<?php }while ($row = mysql_fetch_array($result2)); ?>
</select>



I know it needs to echo the $size_w but where do I put it? If I put it in the option value that is all I get - thanks....
Hopefully I can get my head around it better when I get rid of this damn flu...

nanny
05-05-2010, 11:16 PM
Finally got it to work - silly me I already knew this stuff but forgot along the way... For anybody else that may come across this:



<select name="size_w" id="size_w">
<option value="">Size Width</option>
<?php do { ?>
<option value="<?php echo $row_size['size_w']; ?>"<?php if ($row_size['size_w'] == $size_w) { echo "selected"; }?>><?php echo $row_size['size_w']; ?></option>
<?php }while ($row_size = mysql_fetch_array($result2)); ?>
</select>



I added the echo selected into the option.