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
biosynthesis
Forum Newbie
Posts: 4 Joined: Thu Nov 24, 2011 11:03 am
Post
by biosynthesis » Thu Nov 24, 2011 11:19 am
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 ?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Nov 24, 2011 11:45 am
Add a LIMIT clause to your query.
mikeashfield
Forum Contributor
Posts: 159 Joined: Sat Oct 22, 2011 10:50 am
Post
by mikeashfield » Thu Nov 24, 2011 12:11 pm
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
Post
by biosynthesis » Thu Nov 24, 2011 12:23 pm
It seems that it does not work any other suggestions?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Nov 24, 2011 12:35 pm
Code: Select all
SELECT * FROM tablename WHERE Promo = 1 LIMIT 6
There's no reason this won't work.
maxx99
Forum Contributor
Posts: 142 Joined: Mon Nov 21, 2011 3:40 am
Post
by maxx99 » Fri Nov 25, 2011 6:10 am
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
Post
by biosynthesis » Fri Nov 25, 2011 7:17 am
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.