how to make DIV to print specific number of times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
biosynthesis
Forum Newbie
Posts: 4
Joined: Thu Nov 24, 2011 11:03 am

how to make DIV to print specific number of times

Post by biosynthesis »

Code: Select all


    for ($i = 1; $i < 6; $i++) {
        $categoryName = translateNumToCategory($i);
        $dbProducts = mysql_query('SELECT * FROM ' . $categoryName . " WHERE Promo = 1");
                while ($product = mysql_fetch_array($dbProducts)) {
                    $promoimg = '<div><img src="product_images/' . $product['Image'] . '" width="57" height="53" alt="Pic" /></div>';
                    echo $promoimg;}

                    
                
            
    }

How can i make php to print $promoimg only 6 times ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to make DIV to print specific number of times

Post by Celauran »

Add a LIMIT clause to your query.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: how to make DIV to print specific number of times

Post by mikeashfield »

Code: Select all

<?php
        for ($i = 1; $i < 6; $i++) {
        $categoryName = translateNumToCategory($i);
        $dbProducts = mysql_query('SELECT * FROM ' . $categoryName . " WHERE Promo = 1 LIMIT 0,6;");
                while ($product = mysql_fetch_array($dbProducts)) {
                    $promoimg = '<div><img src="product_images/' . $product['Image'] . '" width="57" height="53" alt="Pic" /></div>';
                    echo $promoimg;}       
    }
?>
biosynthesis
Forum Newbie
Posts: 4
Joined: Thu Nov 24, 2011 11:03 am

Re: how to make DIV to print specific number of times

Post by biosynthesis »

It seems that it does not work any other suggestions?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to make DIV to print specific number of times

Post by Celauran »

Code: Select all

SELECT * FROM tablename WHERE Promo = 1 LIMIT 6
There's no reason this won't work.
biosynthesis
Forum Newbie
Posts: 4
Joined: Thu Nov 24, 2011 11:03 am

Re: how to make DIV to print specific number of times

Post by biosynthesis »

DIVs not showing at all.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: how to make DIV to print specific number of times

Post by maxx99 »

first redo:

Code: Select all

$dbProducts = mysql_query('SELECT * FROM ' . $categoryName . " WHERE Promo = 1 LIMIT 0,6;");
to

Code: Select all

$q = 'SELECT * FROM ' . $categoryName . " WHERE Promo = 1 LIMIT 0,6;"
echo $q; //temporarily 
$dbProducts = mysql_query($q);
Are you sure that you have 5 tables with $categoryName you have?
Copy your select statements and exec it with "MySQL Workbench" or something, check if it returns any results. If no, there's your problem :)
biosynthesis
Forum Newbie
Posts: 4
Joined: Thu Nov 24, 2011 11:03 am

Re: how to make DIV to print specific number of times

Post by biosynthesis »

maxx99 wrote:first redo:

Code: Select all

$dbProducts = mysql_query('SELECT * FROM ' . $categoryName . " WHERE Promo = 1 LIMIT 0,6;");
to

Code: Select all

$q = 'SELECT * FROM ' . $categoryName . " WHERE Promo = 1 LIMIT 0,6;"
echo $q; //temporarily 
$dbProducts = mysql_query($q);
Are you sure that you have 5 tables with $categoryName you have?
Copy your select statements and exec it with "MySQL Workbench" or something, check if it returns any results. If no, there's your problem :)

Your method works. Thank you.
Post Reply