/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Show most popular product

Hi there,

I’m struggling with some MYSQL and PHP code… I want to display the 5 most popular products so i wrote this sql query:

[CODE]
SELECT products_product_id, COUNT(*) AS mostPopular FROM order_details GROUP BY products_product_id ORDER BY mostPopular DESC
[/CODE]

That’s working great, now the second part. My product names are in a different table (products) so i want to display the 5 most popular products from the table products.

So this is where it goes wrong, the code below isn’t working and probably it’s something very stupid… but i can’t find it, aaah! ?

[CODE]
function getMostPopular(){
$result=mysql_query(“SELECT products_product_id, COUNT(*) AS mostPopular FROM order_details GROUP BY products_product_id ORDER BY mostPopular DESC”);
$row=mysql_fetch_array($result);
$max_count = $row[‘products_product_id’];

$sql=mysql_query(“SELECT * FROM products WHERE product_id=$max_count”);

while($rij = mysql_fetch_array($sql)){
echo ‘<a href=”details/’.$rij[‘product_id’].'”><li>’.$rij[‘name’].'</li></a>’;
}

}
[/CODE]

Can anybody help me?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@CapSep 25.2012 — Hi, the first query seemes to be incorrect. If you want to display only 5 product you need to limit your query adding at the end the instraction LIMIT 0,5 ( where 5 is the product number you want dislay ). Howhever, the better way is to create only one query using LEFT JOIN. Sonthing like:
[code=php]
$result=mysql_query("SELECT product.name AS name, order.products_product_id AS product_id, COUNT(order.*) AS mostPopular FROM products AS product LEFT JOIN order_details ON product.products_product_id = order.products_product_id GROUP BY order.products_product_id ORDER BY mostPopular DESC LIMIT 0,5");
[/code]


Check php manual, mysql_fecth_array returns a mysql table. If you want to display all rows about your query you need to use that instruction in a while as:
[code=php]
while( $row = mysql_fetch_array( $result ) )
{
echo '<a href="details/'.$row['product_id'].'"><li>'.$row['name'].'</li></a>';
}
[/code]


But attenction, you use product_product_id in the query, and product_id in the while.
Copy linkTweet thisAlerts:
@mjstam1989authorSep 25.2012 — Thanks! You're input helped me to get the right query with left join.
×

Success!

Help @mjstam1989 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...