Page 1 of 1

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

Posted: Tue May 19, 2009 3:48 am
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 \>";
}

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

Posted: Tue May 19, 2009 6:54 am
by nmreddy
SELECT DISTINCT ( pn_weight),pn_flavor FROM products where pn_name='creatine monohidrat'

use this query ..

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

Posted: Tue May 19, 2009 8:47 am
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

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

Posted: Tue May 19, 2009 8:50 am
by charli18
nmreddy wrote:SELECT DISTINCT ( pn_weight),pn_flavor FROM products where pn_name='creatine monohidrat'

use this query ..
same result again.

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

Posted: Tue May 19, 2009 9:03 am
by charli18
why there is no expert in this forum?????????

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

Posted: Tue May 19, 2009 9:19 am
by divito
Forum Tip #145 - Being ungrateful will undoubtedly get your problem solved faster than actually searching or attempting to solve the problem yourself.

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

Posted: Tue May 19, 2009 9:21 am
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.

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

Posted: Tue May 19, 2009 10:26 am
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!

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

Posted: Tue May 19, 2009 3:51 pm
by califdon
jaoudestudios wrote:Onion2k you must be a saint!
He is, believe me!

[signed,] The Pope