how to get this result?????????please help

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
charli18
Forum Newbie
Posts: 11
Joined: Tue May 19, 2009 3:35 am

how to get this result?????????please help

Post by charli18 »

Hello dear programmers,

I have a table name products and 3 column name p_id, p_weight and p_flavor so for example we have different flavor with same wight how can I retrieve non repeated weight but with all flavor under it.
I need something like this
2 lbs
orange
strawberry
5 lbs
orange

but I've got
2 lbs
orange
5 lbs
orange
2 lbs
strawberry

this is my code:

Code: Select all

$sql=mysql_query("SELECT pn_weight, pn_flavor FROM products where pn_name='creatine monohidrat' ");
echo "<table width=50% align=center border=1 >";
while($row=mysql_fetch_array($sql)){
 
$test=mysql_num_rows($sql);
 
echo "<tr><td> $row[pn_weight] </td></tr><br \>";
 
 
echo "<tr><td> $row[pn_flavor] </td></tr><br \>";
}
Last edited by onion2k on Tue May 19, 2009 7:52 am, edited 1 time in total.
Reason: Fixed list and added PHP bbcode.
nmreddy
Forum Commoner
Posts: 25
Joined: Wed Feb 18, 2009 5:36 am

Re: how to get this result?????????please help

Post by nmreddy »

SELECT DISTINCT ( pn_weight),pn_flavor FROM products where pn_name='creatine monohidrat'

use this query ..
charli18
Forum Newbie
Posts: 11
Joined: Tue May 19, 2009 3:35 am

Re: how to get this result?????????please help

Post by charli18 »

as you see in my above example there is repeated 2 kg with 2 different flavor but I do not want repeated weight I just one to display column weight and related flavor for example we have 5kg creatine with 3 kind of flavor.

5lbs
strawberry
orange
chocolate


I do not need to display for repeated weight for each flavor like below example:
5lbs
strawberry
5lbs
orange
5lbs
chocolate
charli18
Forum Newbie
Posts: 11
Joined: Tue May 19, 2009 3:35 am

Re: how to get this result?????????please help

Post by charli18 »

nmreddy wrote:SELECT DISTINCT ( pn_weight),pn_flavor FROM products where pn_name='creatine monohidrat'

use this query ..
same result again.
charli18
Forum Newbie
Posts: 11
Joined: Tue May 19, 2009 3:35 am

Re: how to get this result?????????please help

Post by charli18 »

why there is no expert in this forum?????????
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: how to get this result?????????please help

Post by divito »

Forum Tip #145 - Being ungrateful will undoubtedly get your problem solved faster than actually searching or attempting to solve the problem yourself.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: how to get this result?????????please help

Post by onion2k »

charli18 wrote:why there is no expert in this forum?????????
There are plenty of experts here. It's only been 6 hours. Some of us have other things to do. Like our own work. If you post anything like that again I'll get annoyed.

Anyway...

Code: Select all

$prev_weight = 0;
 
$sql  = "SELECT `pn_weight`, `pn_flavor` ";
$sql .= "FROM `products` ";
$sql .= "where `pn_name`='creatine monohidrat' ";
$sql .= "ORDER BY `pn_weight` ASC, `pn_flavor` ASC";
 
if ($result = mysql_query($sql)) {
  while($row=mysql_fetch_object($result)){
    if ($prev_weight != $row->pn_weight) { echo $row->pn_weight."<br \>"; }
    echo $row->pn_flavor."<br \>";
    $prev_weight = $row->pn_weight;
  }
}
 
I hate tables so I've stripped out all your HTML. You'll need to add that back in.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: how to get this result?????????please help

Post by jaoudestudios »

onion2k wrote:
charli18 wrote:why there is no expert in this forum?????????
There are plenty of experts here. It's only been 6 hours. Some of us have other things to do. Like our own work. If you post anything like that again I'll get annoyed.
I'm already there. You only get 1 life with me!

Onion2k you must be a saint!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to get this result?????????please help

Post by califdon »

jaoudestudios wrote:Onion2k you must be a saint!
He is, believe me!

[signed,] The Pope
Post Reply