Jump to content
Larry Ullman's Book Forums

Strange Output From An Array Loop And I Can'T Figure Out The Problem


Recommended Posts

HI, again

 

I tried to use the array looping technique from PHP advance book (chapter 1) on my project to output products category into <ul><li> list then apply superfish menu on them. But the output created extra <a>element which slightly messed up the superfish menu. I double check the make_list() function script and I can't tell why the extra <a> tabs are created.

 

the code:

<?php
require ('includes/config.inc.php');
require (MYSQL);
function make_list($parent) {
    global $cat_list;
    echo '<ul>';
    foreach($parent as $catid => $c_list){
            echo "<li>";
            echo "<a href=\"index.php?cid=$catid\">";
            echo $c_list;
        if(isset($cat_list[$catid])){ //$catid = parent_id
            make_list($cat_list[$catid]);
        }        
        echo '</a></li>';
    }
    
    echo '</ul>'; //close the ordered list
} // end of make_list function

$query = "SELECT cc.category_id, cc.parent_id, c.category 
          FROM categories AS c 
          INNER JOIN category_categories AS cc USING (category_id)
          WHERE c.publish = 1";

$result = mysqli_query($dbc, $query);

if(!$result) echo mysqli_error($dbc);

if(mysqli_num_rows($result)>=1){
    
$cat_list = [];    
    
    while (list($c_id, $p_id, $cat) = mysqli_fetch_array($result, MYSQLI_NUM)){
        $cat_list[$p_id][$c_id] = $cat;
    }
}
make_list($cat_list[0]);
?>

the problem output(first few lines)

<ul>
<li>
<a href="index.php?cid=1">Box Collection</a>
<ul>
<a href="index.php?cid=1"></a>
<li>
<a href="index.php?cid=1"></a>
<a href="index.php?cid=32">Classic Preminum Brown Hardwood Boxes</a>
</li>
<li>
<a href="index.php?cid=35">Hat Boxes</a>
</li>
<li>
<a href="index.php?cid=36">Leatherette Boxes</a>
</li>

I expect the output to be: 

<ul>
<li>
<a href="index.php?cid=1">Box Collection</a>
<ul>
<li>
<a href="index.php?cid=32">Classic Preminum Brown Hardwood Boxes</a>
</li>
<li>
<a href="index.php?cid=35">Hat Boxes</a>
</li>
<li>
<a href="index.php?cid=36">Leatherette Boxes</a>
</li>

From the make_list() function, which seems fine, I can't tell why the extra <a> is created. Please anyone can tell where is the problem? 

Link to comment
Share on other sites

I think the following is your problem:

if(isset($cat_list[$catid])){ //$catid = parent_id

You're grabbing the global $cat_list variable, which is a multidimensional array, and you're checking whether the indexes that exist for the subarray $cat_list[0] (i.e., the values set for the $cat_id variable) are also set for the higher-level $cat_list variable.

Actually, I'm not sure why you're doing that.

 

If you could please explain exactly what you want to do, I think we could better help.

My guess is that you want to recursive through a multidimensional array to build up a multilayered unordered list, but please confirm that.

  • Upvote 1
Link to comment
Share on other sites

Hi HartleySan, thanks again for help. 

 

I am trying to do a category menu in the front page that visitor can mouse over the menu and display the category and subcategory. I used Larry's method to sort out the category array by the parent_id, and then apply a JQuery plugin called super fish to create this menu. Everything seems work except the extra yielded <a> cause the blank space in the first space ( the red rectangle highlight).  I still want to solve this problem.. if I can. or leave as it is since the most part of menu is functioning.

 

Screen-Shot.jpg

Link to comment
Share on other sites

Well, like I said before, I think that the problem is the if statement in your function.

You could just correct the if statement, and you can likely resolve your problem, but I think a better fix is to go back and review recursion in Larry's book, and then recode the function to use true recursion, as opposed to half recursion mixed with a foreach loop.

 

I think you can do it without any additional help, but if you need it, please let me know.

Thanks.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...