简体   繁体   中英

PHP function display problem

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

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

By default <p>...</p> and <div>...</div> will always a block (starts a new line always) so your code produce 7 lines.

The way to solve is simple use span instead of <p> or don't use anything.

Hope this helps.

function make_list ($parent = 0, $parent_url = '', $ctr = 0) {
    global $cat_link;
    global $cat_id;         
    $IDs   = array_keys($parent);
    $Count = count($IDs);
    for ($i = 0; $i < $Count; $i++) {
        echo '<div>';
        for($c = 0; ($c < 3) && ($i < $Count); ) {
            $ID  = $IDs[$i++];
            $CAT = $parent[$ID];

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

            $URL = $parent_url . $CAT['url'];                       
            if(isset($cat_link[$id])) {
                make_list($cat_link[$id], $URL, $ctr+1);
            }
        }
        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);

I simply add two nested loops: the inside loops three times and the outside just loop all elements. The key is that everytime the inside one loop, the element loop index ( i ) change too.

Let's try it and let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM