1

I have built the following function, but is it possible to get it to split my lists from one into more so I can have a maximum of 8 <li>'s per <ul>?

function buildProductsMenu($base) {

    $sql = "SELECT *
            FROM tbl_category";
    $result     = dbQuery($sql);
    while ($row = dbFetchAssoc($result)) {
    echo "<ul>";        
        echo "<li class='title'>$row[cat_name]</li>";
            $sqlProd = "SELECT *
                FROM tbl_product WHERE cat_id = $row[cat_id]";
            $resultProd     = dbQuery($sqlProd);
            while ($rowProd = dbFetchAssoc($resultProd)) {
                extract($rowProd);
                echo "<li><a href='".$base."products/".strtolower($row['cat_name'])."/".strtolower($pd_name)."'>".$pd_name."</a></li>";
            }       
    echo "</ul>";               
    }
}

Went with jurgemaister Solution

function buildProductsMenu($base) {

    $sql = "SELECT *
            FROM tbl_category";
    $result     = dbQuery($sql);
    while ($row = dbFetchAssoc($result)) {
        echo "<ul>";        
        echo "<li class='title'>$row[cat_name]</li>";
        $sqlProd = "SELECT * FROM tbl_product WHERE cat_id = $row[cat_id]";
        $resultProd     = dbQuery($sqlProd);
        $counter = 1;
        while ($rowProd = dbFetchAssoc($resultProd)) {
            extract($rowProd);
            if($counter % 12 == 0) {
                $counter = 1;
                echo "</ul><ul style='margin-top:25px;'>";
            }
            echo "<li><a href='".$base."products/".strtolower($row['cat_name'])."/".strtolower($pd_name)."'>".$pd_name."</a></li>";
            $counter++;
        }
        echo "</ul>";
    }
}

2 Answers 2

1

You can add a counter, and when that counter reaches 8, you start a new ul.

$counter = 1;
while ($rowProd = dbFetchAssoc($resultProd)) {
    extract($rowProd);

    if($counter % 8 == 0) {
        $counter = 1;
        echo "</ul><ul>";
    }

    echo "<li><a href='".$base."products/".strtolower($row['cat_name'])."/".strtolower($pd_name)."'>".$pd_name."</a></li>";
    counter++;
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! changed it to 12 and added a margin top. Check my edit :D
1

Okay, instead of echoing the LIs right from the function I suggest you add them all to an array, once things are in an array, its always easier to manage.

Use array_chunk() on the final array of LIs and it will split it into groups of eight.

2 Comments

Then you should also use mysql_fetch_array() instead of mysql_fetch_assoc See: de.php.net/manual/en/function.mysql-fetch-array.php
Don't know if the used dbFetchAssoc is a wrapper to the mysql function but it would be even better to use mysqli and the fetch_array function: de.php.net/manual/en/mysqli-result.fetch-array.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.