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
https://forums.phpfreaks.com/topic/78901-categories-option-list-selected/
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); 
     } 
} 

Archived

This topic is now archived and is closed to further replies.

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