Jump to content

Need help


chokies12

Recommended Posts

hi need some help i dont know how to do this. i have list that shows my category items what i want to do know is to have and indent if that item is a sub category can someone help me..thx

 

here's part of my code

<table  width="80%" cellpadding="2" cellspacing="2">	
  <tr>    
    <td class="listhd" width="25">#</td>	
    <td class="listhd" width="50">Actions</td>    
    <td class="listhd">Category Name</td>	
    <td class="listhd" width="50" align="center">Order 	</td>	
  </tr>
<?php
$n_record_counter=1;
foreach($result_fields as $v){
$s_bgColor=($n_record_counter%2) ? '#FFFFFF' : '#EFEFEF';
  ?>	
  <tr bgcolor="<?=$s_bgColor?>">    
    <td >
      <?=$n_record_counter++;?>.</td>	
    <td >
      <a href="?a=categories&action=edit&id=<?=$v['id']?>">
        <img src="images/icon_edit16x16.gif" /></a>  
      <a href="?a=categories&action=delete&id=<?=$v['id']?>">
        <img src="images/icon_delete16x16.gif" /></a>	</td>    	
    <td class="rowGreen">
      <?=$v['name']?>	</td>    
    <td align="center">
      <?=$v['ordseqn']?></td>	
  </tr>
<?php
}
  ?>
</table>

 

Link to comment
Share on other sites

You need recursion to do so:

function category_list(array $category, $indent = "\t") {
    //process
    if (is_array($value)) {
        category_list($value, $indent . "\t");
    }
}

 

If you have 2 tables: category and subcategory. Then first retrieve all categories and just roll them out if a user clicked a category and the current category id in the loop matches the clicked category, then perform a new query which retrieves the subcategories for the active category. I strongly suggest using something differently than a table to display the indentation like a definition list (dl) a category is then (dt) and the subcategories (dd)

Link to comment
Share on other sites

You need recursion to do so:

function category_list(array $category, $indent = "\t") {
    //process
    if (is_array($value)) {
        category_list($value, $indent . "\t");
    }
}

 

If you have 2 tables: category and subcategory. Then first retrieve all categories and just roll them out if a user clicked a category and the current category id in the loop matches the clicked category, then perform a new query which retrieves the subcategories for the active category. I strongly suggest using something differently than a table to display the indentation like a definition list (dl) a category is then (dt) and the subcategories (dd)

 

 

Hi i only have 1 table for category and the problem is this is already as is i just have to make indention for sub categories. Im just doing some updates and i not familiar with its structures im still a newbie

Link to comment
Share on other sites

Ok so you have one category table? Do you have a field which identifies the parent? Something like:

 

category
----------------
id   parent_id  
1    0 // not a child
2    1 // child of 1
3    1 // child of 1
4    2 // child of 2
5    3 // child of 3

 

$currentCat = (int) $_GET['category'];
while (list($id, $parent_id, ..) = mysql_fetch_array($result, MYSQL_NUM)) {
    if ($id === $currentCat) {
        $query = "SELECT * FROM categories WHERE parent_id = '$id'";
        $result = mysql_query($query, $db);
        while (list(..
}

 

If you are in for some complexity then take a look at the composite pattern as this will do here just nicely ;)

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.