1

i have one long list generated from a query, but i need to output to a maximum of 4 lists (4 columns) with a maximum of 17 results in each list (total max of 68) as there is a set height and width to fit this in, im new to php so im not sure where to start. I hope that makes sense!

<li class="dir">List title
        <?php
        // The following query gets all brands that appear in the mens categories and orders by manufacturer name
        $mens_categories_products_query = "SELECT * FROM `products_to_categories` `pc` ";
        $mens_categories_products_query .= "LEFT JOIN `products` `pr` ON `pc`.`products_id` = `pr`.`products_id` ";
        $mens_categories_products_query .= "LEFT JOIN `manufacturers` `mn` ON `pr`.`manufacturers_id` = `mn`.`manufacturers_id` ";
        $mens_categories_products_query .= "WHERE `pc`.`categories_id` IN (";
        $mens_categories_products_query .= substr($mens_categories_products_ids, 0, strlen($mens_categories_products_ids)-1);
        $mens_categories_products_query .= ") GROUP BY `pr`.`manufacturers_id` ORDER BY `mn`.`manufacturers_name`";
        //$mens_manufacturers_query = tep_db_query("SELECT * FROM `manufacturers` ORDER BY `manufacturers_name` ASC;");

        // Run the new query 
        $mens_manufacturers_query = tep_db_query($mens_categories_products_query);
        $mens_manufacturers_list = "";
        while ($mens_manufacturers = tep_db_fetch_array($mens_manufacturers_query)) {

            $mens_manufacturers_list .= '<li><a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $mens_manufacturers['manufacturers_id']) . '">' . $mens_manufacturers['manufacturers_name'] . '</a></li>';

        }

        echo "<ul>$mens_manufacturers_list</ul>";
        ?>
        </li>    

3 Answers 3

1

Maybe something like this?

$i = 1;
print "<ul>";
while ($mens_manufacturers = tep_db_fetch_array($mens_manufacturers_query)) {
            if ($i % 17 == 1) { 
                print "<ul>";
            }
            if ($i == 68) {
                print '<li><a href="readmore.php">Read more</a></li>';
            }
            else {
                print '<li><a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $mens_manufacturers['manufacturers_id']) . '">' . $mens_manufacturers['manufacturers_name'] . '</a></li>';
            }
            if ($i % 17 == 0) { 
                 print "</ul>";
            }
            $i++;    
}

With CSS something like

ul {
    width: 21%;
    float: left;
}

UPDATED:

Code updated to correct numbering loop on the first <ul>. Added 'read more' link on 68th entry.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks, at what point would i add this code in? im assuming under //Run the new query but im not sure what to leave in or replace?
Yes, that would be the right place. This would output four <ul></ul>'s inside <li class="dir"> that you could then style to fit your container.
You could take out the lines from //Run the new query up to echo "<ul>$mens_manufacturers_list</ul>"; as I wasn't using the $mens_manufacturers_list variable.
great thanks, stackoverflow is an awesome resource, im new here, much appreciated feedback!
Hope it's useful for you! If this answer works for you, please consider marking it as accepted.
|
0

try adding an icrement variable and at 17 rows add another ul

$i = 0;
$mens_manufacturers_list = '<ul>';
$dbresult = tep_db_fetch_array($mens_manufacturers_query);
while ($mens_manufacturers = $dbresult) {
    $i++;
    $mens_manufacturers_list .= '<li><a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $mens_manufacturers['manufacturers_id']) . '">' . $mens_manufacturers['manufacturers_name'] . '</a></li>';

    if($i%17==0){
        $mens_manufacturers_list .= '</ul>';
    }
    if(count($dbresult) < $i){
        $mens_manufacturers_list .= '<ul>';
    }
}
echo $mens_manufacturers_list;

3 Comments

you can add after $i++ another condition, if($i == 1){ $mens_manufacturers_list .= "<li>Brand name</li>"; } else ...
Hi, im not having much luck as i think im not quite getting it right as in add the code, its coming back as invalid argument for this line: WARNING: INVALID ARGUMENT SUPPLIED FOR FOREACH() IN The line is: foreach ($dbresult as $mens_manufacturers) {
thanks, what do i replace with "while" exactly - sorry im bad at php and need specifics!
0

thorn, ive changed the code a little im wondering how i can get your updated code with the read more to work in this:

        <?php $i = 1;
        echo "<li class=\"dir\">BRANDS<ul>";
        $skate_manufacturers_query = tep_db_query($skate_categories_products_query);
        while ($skate_manufacturers = tep_db_fetch_array($skate_manufacturers_query)) {

        if ($i % 17 == 0) 
        echo "</ul></li><!-- start col --><li class=\"dir\">&nbsp;<ul>";
        echo '<li><a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $skate_manufacturers['manufacturers_id']) . '">' . $skate_manufacturers['manufacturers_name'] . '</a></li>';
        if ($i % 18 == 0) 
        echo "<!-- end one col -->";
        $i++;    
        } ?>

    </ul> <!-- needs these added -->
</li>  <!-- needs these added -->   

1 Comment

ok ive got it working, how do i get it to stop outputting results at 68, a maximum results sort of thing...

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.