quasiman Posted November 26, 2007 Share Posted November 26, 2007 I'm using the following code to display the categories available in my shop. It works fine, except when I modify a product, I want it to print "selected" in the <option> where the category is previous used. As it is now, it defaults back to the category root, and I have to reselect the correct category or else it saves as the root. Any help would be greatly appreciated: <?php require_once 'library/config.php'; require_once 'library/category-functions.php'; require_once 'library/product-functions.php'; require_once 'library/cart-functions.php'; $_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI']; $catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0; $pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0; require_once 'include/header.php'; function traverse($root, $depth, $sql) { $row=0; while ($acat = mysql_fetch_array($sql)) { if ($acat['cat_parent_id'] == $root) { print "<option value='" . $acat['cat_id'] . "'>"; $j=0; while ($j<$depth) { print " "; $j++; } if($depth>0) { print "-"; } print $acat['cat_name'] . "</option>\n"; mysql_data_seek($sql,0); traverse($acat['cat_id'], $depth+1,$sql); } $row++; @mysql_data_seek($sql,$row); } } print "<select name='cboCategory' id='cboCategory' class='box'>"; $selcat="SELECT * from tbl_category order by cat_name ASC"; $selcat2=mysql_query($selcat) or die("Could not select category"); traverse(0,0,$selcat2); print "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/ Share on other sites More sharing options...
tippy_102 Posted November 27, 2007 Share Posted November 27, 2007 I had the same problem a while ago and Barand solved it for me. Here's the thread: http://www.phpfreaks.com/forums/index.php/topic,132026.msg554878.html#msg554878 Quote Link to comment https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/#findComment-400056 Share on other sites More sharing options...
quasiman Posted November 27, 2007 Author Share Posted November 27, 2007 Well that looks like it's right, but I only succeeded in making it worse Quote Link to comment https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/#findComment-400087 Share on other sites More sharing options...
tippy_102 Posted November 27, 2007 Share Posted November 27, 2007 If you want help with that you'll have to tell us what exactly is happening that makes it "worse" and also post your updated code. Quote Link to comment https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/#findComment-400490 Share on other sites More sharing options...
quasiman Posted November 27, 2007 Author Share Posted November 27, 2007 I'm still playing with it, hold on a minute lol Quote Link to comment https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/#findComment-400599 Share on other sites More sharing options...
quasiman Posted November 28, 2007 Author Share Posted November 28, 2007 Alright, I still can't get it to work. It's not worse at this point, but this is what I've tried with the same results. function traverse($root, $depth, $sql) { $row=0; while ($acat = mysql_fetch_array($sql)) { if ($acat['cat_parent_id'] == $root) { $query = "SELECT cat_id FROM tbl_product"; $result = mysql_query($query) or die(mysql_error()); If ($acat['cat_id'] == $result) { $selected = "selected";} else {$selected = "";} print "<option value='" . $acat['cat_id'] . "' $selected>"; $j=0; while ($j<$depth) { print " "; $j++; } if($depth>0) { print "-"; } print $acat['cat_name'] . "</option>\n"; mysql_data_seek($sql,0); traverse($acat['cat_id'], $depth+1,$sql); } $row++; @mysql_data_seek($sql,$row); } } Quote Link to comment https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/#findComment-400989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.