Jump to content

for loop issue


vmicchia

Recommended Posts

I have a loop that goes through my records and displays them in a table. However when I do my search it finds the number of records and then displays the same record over and over that many times. here is my code

$result1 = mysql_query($sql1);
//echo $result;

$rows1 = mysql_num_rows($result);

if($rows1 < 1){
	cleanUp();
	$_SESSION["errorMsg"] = "There are no records for this search.";
	header("Location:index.php");
}

$row1 = mysql_fetch_array($result);

for($i = 0; $i < $rows1; $i++){
  	echo "<tr>";
    echo	"<td><a href=\"customerInfo.php?orderID=".trim($row1["order_id"])."\">".trim($row1["order_id"])."</a></td>\n" ;
       if(trim($row1["status"]) == P){
        	echo "<td>Processed</td>\n";
        }elseif(trim($row1["status"]) == F){
        	echo "<td>Failed</td>\n";
        }elseif(trim($row1["status"]) == B){
        	echo "<td>Back Ordered</td>\n";
        }elseif(trim($row1["status"]) == O){
        	echo "<td>Open</td>\n";
        }
    echo    "<td>".trim($row1["firstname"])." ".trim($row1["lastname"])."</td>\n";
    echo    "<td>".trim($row1["email"])."</td>\n";
    echo    "<td>".strftime("%c",is_string(trim($row["timestamp"])))."</td>\n";
    echo    "<td>$".trim($row1["total"])."</td>\n";
    echo "</tr>\n";
  } 

 

Thank you for any help in advance.

Link to comment
https://forums.phpfreaks.com/topic/206560-for-loop-issue/
Share on other sites

You are only fetching one row into $row1 and then displaying it multiple times.  Try this instead:

//$row1 = mysql_fetch_array($result);  // delete this

//for($i = 0; $i < $rows1; $i++){  // replace this with
while($row1 = mysql_fetch_array($result)) {

 

Also, you start using $result1 and then switch to using just $result.

 

 

Link to comment
https://forums.phpfreaks.com/topic/206560-for-loop-issue/#findComment-1080467
Share on other sites

Thank you AbraCadaver the code works. The reason result switches is because I copy pasted from 2 different parts of the code on accident.

 

One other problem I'm having is with the timestamp. it is a 10 digit time stamp and I use

strftime("%c",is_string(trim($row["timestamp"])))

but the year comes out as 1969 instead of 2010. Am I using the wrong code?

Link to comment
https://forums.phpfreaks.com/topic/206560-for-loop-issue/#findComment-1080473
Share on other sites

Thank you AbraCadaver the code works. The reason result switches is because I copy pasted from 2 different parts of the code on accident.

 

One other problem I'm having is with the timestamp. it is a 10 digit time stamp and I use

strftime("%c",is_string(trim($row["timestamp"])))

but the year comes out as 1969 instead of 2010. Am I using the wrong code?

 

That's totally the wrong code.  strftime needs a unix timestamp and you're giving it the result of is_string which is probably true.  Depending on what $row["timestamp"] looks like you probably need this:

date('Y', strtotime($row["timestamp"]));

 

Or use the MySQL date functions to just return the year from the query, depending on if you need the whole date later or not.

 

 

Link to comment
https://forums.phpfreaks.com/topic/206560-for-loop-issue/#findComment-1080477
Share on other sites

I tried that code and it sent the same result the time stamp looks like this in the data base: 1273863827 which is for 07/05/2010, 17:07. I didn't make the Db so I'm not sure what kind of time stamp it is.

 

Try:

 

date('Y',$row["timestamp"]);

Link to comment
https://forums.phpfreaks.com/topic/206560-for-loop-issue/#findComment-1081832
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.