hello, this is my code. output of this code is like this: item amount item name 1 jacket 3 top 3 top 3 top 1 skirt what can i do to show like this? item amount item name 1 jacket 3 top top top 1 skirt If the item amount is bigger than 1, i just want to show item amount only once and skip the other ones. <table width="1000" align="center"> <tr> <td>Item amount</td> <td>Item name</td> </tr> <? $query="SELECT * FROM item WHERE orderr_reference ='$ref'"; $result=mysql_query($query); $num=mysql_numrows($result); $i=0; while ($i < $num) { $item_amount = mysql_result($result,$i,"item_amount"); $item_name = mysql_result($result,$i,"item_name"); ?> <tr> <td><?=$item_amount;?></td> <td><?=$item_name;?></td> </tr> <? $i++; } ?> </table> PHP:
If you are populating an array with data from a database and only want to show unique values use a GROUP BY clause in your query. $query="SELECT * FROM item WHERE orderr_reference ='$ref' GROUP BY item_name"; Code (markup): Sometimes GROUP BY wants a count or sum etc... so you might need too $query="SELECT count(item_name) as item_count,item_name FROM item WHERE orderr_reference ='$ref' GROUP BY item_name"; Code (markup):
i do not know how to use it. i tried group before but it did not work the way i wanted. what ever item amount is it is only displaying 1. Like this: 1 jacket 3 top 1 skirt
You're going to have to maintain an array of items that have been displayed. You'll need to work something similar to this into your code, though I'm not sure exactly how it's going to work with the "continue" because I don't mix HTML/PHP like you have there. $used = array(); while ($i < $num) { $item_amount = mysql_result($result,$i,"item_amount"); $item_name = mysql_result($result,$i,"item_name"); if( isset($used[$item_name])) { continue; } else if($item_amount > 1) { $used[$item_name] = true; } PHP:
$query = "SELECT item_name, item_amount FROM item WHERE orderr_reference = '$ref'"; $result = mysql_query($query); $record = array(); $td = ''; while($row = mysql_fetch_object($result)){ if(isset($record[$row->item_name]) && ($record[$row->item_name] == $row->item_amount)){ $td .= '<tr><td> </td><td>'.$row->item_name.'</td></tr>'; }else{ $td .= '<td>'.$row->item_amount.'</td><td>'.$row->item_name.'</td>'; $record[$row->item_name] = $row->item_amount; } } echo ' <table width="1000" align="center"> <tr> <td>Item amount</td> <td>Item name</td> </tr>'.$td.' </table>'; PHP:
t can be done by check the previous item in php script, if it same then don't show the amount if it's not same then show the amount. <table width="1000" align="center"> <tr> <td>Item amount</td> <td>Item name</td> </tr> <? $query="SELECT * FROM item WHERE orderr_reference ='$ref' order by item_name"; $result=mysql_query($query); $num=mysql_numrows($result); $item_name_tmp = "string that never be in the list"; $i=0; while ($i < $num) { $item_amount = mysql_result($result,$i,"item_amount"); $item_name = mysql_result($result,$i,"item_name"); if ($item_name != $item_name_tmp ){ $item_amount_print = $item_amount; $item_name_tmp = $item_name; }else{ $item_amount_print = ""; } ?> <tr> <td><?=$item_amount_print;?></td> <td><?=$item_name;?></td> </tr> <? $i++; } ?> </table> Code (markup):