tags:

views:

66

answers:

2

I'm trying to have my function display 3 links in a row then start a new row and display the other 3 links and so on but my code is not working right displaying my links all wrong.

Current output.

<div>
    <p>
        <a href="">A</a>
    </p>

    <p>
        <a href="">>A1</a>
    </p>
<div>
    <p>
        <a href="">Glass &amp; Mosaics</a>
    </p>
</div>

<div>
    <p>
        <a href="">>Handcrafted &amp; Finished Pieces</a>
    </p>
</div>
</div>
    <p>
        <a href="">>Entrepreneurislism</a>
    </p>

    <p>
        <a href="">>Photography</a>
    </p>
</div>
</div>

<div>
    <p>
        <a href="">>Antiques</a>
    </p>

    <p>
        <a href="">>Antiquities</a>
    </p>

    <p>
        <a href="">>Architectural &amp; Garden</a>
    </p>
</div>
</div>

<div></div>

<div>
    <p>
        <a href="">>Cameras &amp; Photo1</a>
    </p>
</div>
</div>
</div>
<div></div>
</div>  

Output should be.

Link1       Link2       Link3
Link4       Link5       Link6
Link7

Here is my code.

function make_list ($parent = 0, $parent_url = '', $ctr = 0) {
    global $cat_link;
    global $cat_id;         
    foreach ($parent as $id => $cat) {
        if(!empty($cat['id'])) {
            if($ctr%3 == 0) {
                echo '<div>';
            }

            if(in_array($cat['id'], $cat_id)){ 
                $url = $parent_url . $cat['url'];
                echo '<p><a href="' . $url . '" title="' . $cat['category'] . ' Category Link" rel="Articles Category">' . $cat['category'] . '</a></p>';           
            }

            $url = $parent_url . $cat['url'];                       
            if(isset($cat_link[$id])) {
                make_list($cat_link[$id], $url, $ctr+1);
            }

            if($ctr%3 == 0) { 
                echo '</div>';
            }               
        }
    }

    if($ctr%3 != 0) { 
        echo '</div>';
    }

}

$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");   

if(!$dbc) {
    print mysqli_error($mysqli);
} 

$cat_link = array();
while(list($id, $parent_id, $category, $url) = mysqli_fetch_array($dbc)) {
    $cat_link[$parent_id][$id] =  array('category' => $category, 'url' => $url, 'id' => $id);
}

make_list($cat_link[0], $url, $ctr);
A: 
NawaMan
that still dosn't solve the problem though.
bold
My code dose not produces 7 different lines.
bold
@bold: See my update.
NawaMan
only two links are displayed now but the code seems a little better.
bold
+1  A: 

What you're shooting for, I think, is something along these lines:

function make_list($parent)
{
    $link_count = 0;
    foreach($parent as $id => $category)
    {
        if($link_count % 3 == 0) echo '<div>';

        // display the link here
        // call make_list here to display sub-categories in an inner <div>

        if($link_count % 3 == 2) echo '</div>';

        $link_count++;
    }
}