PHP while loop

Discussion in 'PHP' started by dean5000v, Aug 5, 2008.

  1. #1
    ok ive got this code to print out the information from the database onto the page.

    <?php
    $query = "SELECT * from Storage";
    $result = mysql_query($query);
    
    
    while ($row = mysql_fetch_assoc($result)) {
        echo "<div id=\"test\" style=\"width: 160px; float: left; margin-top: 30px; margin-left: 10px; display: inline; border: 1px solid black;\"><div id=\"image\" \"style=\"\">" ."<img src=\"images/storage/"
    		
    	
    	.$row["item_code"] .".jpg\"></div>" ."<div id=\"text\" style=\"width: 160px; padding-left: 10px; font-family: verdana; font-size: 10pt; \">" 
    		
    	
    	.$row["item_desc"] ."<br>"
    	."£".$row["item_price"] ."</div></div>";
    }
    
    ?>
    Code (markup):
    was wondering does anyone know how to change the order on how it prints out the information for example if i add in 3 stock items in the database called 1,2,3 then it will print them out 3,2,1, can anyone give me a hint on how to change this around.
     
    dean5000v, Aug 5, 2008 IP
  2. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Adjust your query to:

    select * from storage order by stock desc
    Code (markup):
     
    ahowell, Aug 5, 2008 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    $query = "SELECT * from Storage";
    $result = mysql_query($query);
    
    
    for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
        
        if (!mysql_data_seek($result, $i)) {
        
            echo "Cannot seek to row $i: " . mysql_error() . "\n";
            continue;
            
        }//end if
        
        $row = mysql_fetch_assoc($result);    
      
        echo "<div id=\"test\" style=\"width: 160px; float: left; margin-top: 30px; margin-left: 10px; display: inline; border: 1px solid black;\"><div id=\"image\" \"style=\"\">" ."<img src=\"images/storage/"
    		
    	
    	.$row["item_code"] .".jpg\"></div>" ."<div id=\"text\" style=\"width: 160px; padding-left: 10px; font-family: verdana; font-size: 10pt; \">" 
    		
    	
    	.$row["item_desc"] ."<br>"
    	."£".$row["item_price"] ."</div></div>"; 
        
    }//end for loop
    PHP:
     
    php-lover, Aug 5, 2008 IP
  4. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks alot works fine now :D
     
    dean5000v, Aug 5, 2008 IP