Page 1 of 1

Problem with a For-Loop. Please help.

Posted: Tue Apr 03, 2007 1:27 am
by jdhorton77
I have a page that is going to print out the orders a customer has open. In the start I have an if statement that is going to see if there are any rows in the record set from a query. If there are none it prints out "No Orders In Progress". This part works fine.

But if there are rows in the record set it goes into a for loop that will use variable $i to increment through the recordset. My problem seems to be coming from the for loop, I think. I went through and commented out everything after the else and it printed out "No Orders In Progress" perfectly. But after I uncommented out the php code within the span tags, but left the for loop declaration commented out, it worked fine. I'm not sure what it can be. Could someone look over it and let me know if they see something please. Here's the code:

Code: Select all

<div id="generic"> 
      <div id="left_column"> 
        <div class="grey_block"> <span class="subtitle">Orders</span> 

          <?php  if(!mysql_num_rows($orders)) { ?>
          <span class="block">No Orders In Progress</span> 
          <?php } else { 

	for($i=0;$i<=mysql_num_rows;i++) {?>       // <----- if you comment out this line and line marked below it works.

          <div class="block"> <span class="inline">Order Number: </span> <span class="inline"><?php  echo mysql_result($orders,$i,0);  ?></span> 
          </div>
          <div class="block"> <span class="inline">Date Ordered: </span> <span class="inline"><?php  echo mysql_result($orders,$i,3);  ?></span> 
          </div>
          <div class="block"> <span class="inline">Currently: </span> <span class="inline"><?php  echo mysql_result($orders,$i,5);  ?></span> 
          </div>
          <?php if(mysql_result($orders,$i,5) == "complete") { ?>
          <div class="block"> <span class="inline">Date Complete: </span> <span class="inline"><?php  echo mysql_result($orders,$i,4);  ?></span> 
          </div>
          
          <?php  }
	}	// <---- not sure if I need any ";" here or not. But if you comment out this curly brace, and above, it works.	
         } ?>

        </div>
      </div>
    </div>
I have marked where I put the comment slashes and it worked. I don't know what's up, I've been looking at this for hours and I'm clueless. Thanks for all the help in advance.

Posted: Tue Apr 03, 2007 1:31 am
by Kieran Huggins

Code: Select all

<?php
  while($row = MySQL_fetch_assoc($orders)) {?>
    <div class="block"> <span class="inline">Order Number: </span> <span class="inline"><?php  echo mysql_result($row['0']);  ?></span>
//...
  }

Posted: Tue Apr 03, 2007 1:47 am
by jdhorton77
Ok, that looks simplier. Just want to make sure I understand what's going on with it. I get everything up until the ($row['0']); Would I need to increment up from 0? I looked up the mysql_result parameters, wouldn't I need to tell it which row number and field number?

Posted: Tue Apr 03, 2007 2:24 am
by Kieran Huggins
"Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead"

Basically, it gets the current row as an associative array. When in a while loop, it will keep doing this until it runs out of rows.

The example code should have been:

Code: Select all

<?php
  while($row = MySQL_fetch_assoc($orders)) {?>
    <div class="block"> <span class="inline">Order Number: </span> <span class="inline"><?php  echo $row['order_num'];  ?></span>
//...
  }
where 'order_num' is the column you want represented.

Check the man page for a clearer explanation:
http://www.php.net/function.mysql_fetch_assoc