Jump to content

Categories option list - selected


quasiman

Recommended Posts

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

Link to comment
Share on other sites

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

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.