SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist jimday1982's Avatar
    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is only ONE of my records being echoed out?

    The code below SHOULD be displaying multiple rows of data since my query is returning mutiple results...I'm using the .= operator, so I'm not sure what's wrong...can anyone help? The $line6, $line7, and $line8 variables are the problems. Thanks.

    PHP Code:
    //No exception was found
      
    if ($exception != "yes"){
      
    $sql "SELECT * FROM inventory_matched WHERE code = '$code'";
      
    $result mysql_query($sql$link) or die(mysql_error());
      if (
    $myrow mysql_fetch_array($result)) {
          do {
              
    $code $myrow["code"];
              
    $description $myrow["description"];
              
    $quantity $myrow["in_stock"];
              
    $option_code $myrow["option_code"];
              
             
    $line6 .= "<tr bgcolor=FFFFFF><td><font face=arial color=000000 size=2>" $option_code "</font></td>";
             
    $line7 .= "<td><font face=arial color=000000 size=2>" $description "</font></td>";
             
    //Test to see if the quantity is less than zero to determine in stock/out of stock
              
    if ($quantity && $exception != "yes"){
                 
    $quantity "<b><font color=red>Out of Stock</font></b>";
              }
              else if (
    $quantity && $exception != "yes"){
                 
    $quantity "<b><font color=green>In Stock</font></b>";
              }
          
             
    $line8 .= "<td><font face=arial color=000000 size=2>" $quantity "</font></td></tr>";
          } while (
    $myrow mysql_fetch_array($result));
      }
      }
      echo 
    $line1;
      echo 
    $line2;
      echo 
    $line3;
      echo 
    $line4;
      echo 
    $line5;
      echo 
    $line6;
      echo 
    $line7;
      echo 
    $line8;
     echo 
    "</table>" $notification "<br><script language=\"JavaScript\">document.write(\"<font face='arial' size='2'><a href='javascript:this.close()'>Click here to close this window</a></font>\");</script>";
      echo 
    "</center></table>";
      
    ?>
      </body>
      </html> 
    Jimmy Day
    Senior Systems Analyst
    Piedmont Healthcare Corporation

  2. #2
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is only ONE of my records being echoed out?

    Hi,
    The reason I think only one row is being echoed is that you don't seem to be looping through the row correctly.

    I've ammended your code to give an example of one of many ways that you can loop through the result set.

    First i'm checking if the number of rows returned is greater or equal to one, if it is we can continue.

    Then, for each row number upto the number of rows returned I seek to that row in the result set, and fetch that into $myrow.

    PHP Code:
      $sql "SELECT * FROM inventory_matched WHERE code = '$code'";
      
    $result mysql_query($sql$link) or die(mysql_error());
      
      
    $l_sql_numrows mysql_num_rows($result);  // Get the number of rows the query returned
      
      // If any rows were returned we can continue
      
    if ( $l_sql_numrows >= )
      {
        
    $l_i 0;  // Set the initial row to be returned to 0 (first)
        
        // For every row up to the number of rows returned
        
    while ( $l_i $l_sql_numrows )
        {
            
    mysql_data_seek($result$l_i);    // Move to the current row in the result set
            
             
    $myrow mysql_fetch_array($result);    // Return the current row as an array
             
              
    $code $myrow["code"];
              
    $description $myrow["description"];
              
    $quantity $myrow["in_stock"];
              
    $option_code $myrow["option_code"];
              
            
    // YOUR CODE HERE.. e.g. check the quantity, add stuff to the lines
            
            
    $l_i++;    // Move onto the next row
          
    // end of while ( $l_i < $l_sql_numrows )
      
    }
      
      
    mysql_free_result($result);
      
      echo 
    $line1;
      echo 
    $line2;
      echo 
    $line3;
      
    // echo $line........ 
    I hope this helps, but I may need to clarify it some more.

    Harry

  3. #3
    Fully Sweet Car noddy's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Western Australia
    Posts
    759
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could also use a while loop

    PHP Code:
     while ($myrow mysql_fetch_array($result)) {
       
    //do stuff in here with the data
     
    // end shile 

  4. #4

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by noddy
    you could also use a while loop

    PHP Code:
     while ($myrow mysql_fetch_array($result)) {
       
    //do stuff in here with the data
     
    // end shile 
    Which I would also definitely suggest. Everything else is just a nightmare in terms of perfomance .


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •