1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to skip in a loop

Discussion in 'PHP' started by baris22, Feb 11, 2010.

Thread Status:
Not open for further replies.
  1. #1
    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:

     
    baris22, Feb 11, 2010 IP
    sarahk likes this.
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    use
    break;
    PHP:
     
    sarahk, Feb 11, 2010 IP
  3. systematical

    systematical Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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):
     
    systematical, Feb 11, 2010 IP
  4. elisha24

    elisha24 Well-Known Member

    Messages:
    359
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    160
    #4
    or use while statment,it will help u
     
    elisha24, Feb 11, 2010 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    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
     
    baris22, Feb 12, 2010 IP
  6. avimusicstore

    avimusicstore Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Use any of Above, this is so simple.
     
    avimusicstore, Feb 12, 2010 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    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:
     
    joebert, Feb 12, 2010 IP
    baris22 likes this.
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    $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>&nbsp;</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:
     
    php-lover, Feb 12, 2010 IP
    baris22 likes this.
  9. anxggxun

    anxggxun Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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):
     
    anxggxun, Feb 12, 2010 IP
    baris22 likes this.
  10. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Thank you all. All of them worked perfect. i tried 1 by 1.

    thanks again.


     
    baris22, Feb 12, 2010 IP
Thread Status:
Not open for further replies.