sabo86 Posted September 15, 2008 Share Posted September 15, 2008 hello guys here part of my code: for ($i=0; $i <$num_results; $i++){ $row = mysql_fetch_array($result); echo '<table width="800" border="0">'; if( $i = $i%2 ) { echo ' <tr bgcolor="#EFEFEF">';} else { echo ' <tr bgcolor="#C3C3C3">'; } echo '<td width="68" align="left" valign="top"><div align="left"><a href="'.$row["Picture"].'" target="_blank"><img src="'.$row["PictureThumbnail"].'" border="0" /></a></td>'; echo " <td><strong> ".($i+1).".Model : </strong>"; echo htmlspecialchars(stripslashes($row["Model"])); echo "<br><strong> Description: </strong>" ; echo htmlspecialchars (stripslashes($row["Description"])); echo "<br><strong> Category: </strong>"; echo htmlspecialchars (stripslashes($row["Category"])); echo "<br></td></table>"; } The results seems to be infinite loop of rows that doesn't end... any suggestions? Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/ Share on other sites More sharing options...
waynew Posted September 15, 2008 Share Posted September 15, 2008 for ($i=0; $i <$num_results; $i++) Where are you getting $num_results from? And why don't you just use a while instead of a for loop? while($row = mysql_fetch_assoc($result_from_query)){ echo $row['Description']; //etc } Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642342 Share on other sites More sharing options...
sabo86 Posted September 15, 2008 Author Share Posted September 15, 2008 This one may make it more clear $num_results = mysql_num_rows($result); echo "<p><i>Number of records found: ".$num_results."</i></p>"; $Model = $Description = $Category = $Image = null; for ($i=0; $i <$num_results; $i++){ $row = mysql_fetch_array($result); echo '<table width="800" border="0">'; if( $i = $i%2 ) { echo ' <tr bgcolor="#EFEFEF">';} else { echo ' <tr bgcolor="#C3C3C3">'; }; echo '<td width="68" align="left" valign="top"><div align="left"><a href="'.$row["Picture"].'" target="_blank"><img src="'.$row["PictureThumbnail"].'" border="0" /></a></td>'; echo " <td><strong> ".($i+1).".Model : </strong>"; echo htmlspecialchars(stripslashes($row["Model"])); echo "<br><strong> Description: </strong>" ; echo htmlspecialchars (stripslashes($row["Description"])); echo "<br><strong> Category: </strong>"; echo htmlspecialchars (stripslashes($row["Category"])); echo "<br></td></table>"; } Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642344 Share on other sites More sharing options...
waynew Posted September 15, 2008 Share Posted September 15, 2008 Try putting it inside the while loop method that I wrote above. Also, post your query. Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642346 Share on other sites More sharing options...
sabo86 Posted September 15, 2008 Author Share Posted September 15, 2008 but i want to use the for loop inorder to make different row background colors! Besides I need to numerate the results Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642348 Share on other sites More sharing options...
waynew Posted September 15, 2008 Share Posted September 15, 2008 You still can. Just $i = 0; while($row = mysql_fetch_assoc($result)){ echo $row['Description']; $i++;//Leave at end } Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642353 Share on other sites More sharing options...
sabo86 Posted September 15, 2008 Author Share Posted September 15, 2008 but the for loop was working before I put the if condition? what do you think the problem is? Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642356 Share on other sites More sharing options...
sabo86 Posted September 15, 2008 Author Share Posted September 15, 2008 The while loop worked... but the results are as following: 1.Model : Executive Office Arco Description: Executive offices wooden frames anthracite lacquered; worktops, doors and drawer-fronts. Category: European Office Furniture 2.Model : MOD. SERIE 2000 Description: Work Station Category: Local Office Furniture 1.Model : MOD.098 Description: Anthropornetric desk Category: School and University Furniture 2.Model : Operative Seatings T Description: I love this descript Category: Seatings In other words, the numerations are 1 & 2 only!!! how can this be solved? here is the code again: $i=0; while ($row = mysql_fetch_array($result)) { echo '<table width="800" border="0">'; if( $i = $i%2 ) { echo ' <tr bgcolor="#EFEFEF">';} else { echo ' <tr bgcolor="#D3D3D3">'; }; echo '<td width="68" align="left" valign="top"><div align="left"><a href="'.$row["Picture"].'" target="_blank"><img src="'.$row["PictureThumbnail"].'" border="0" /></a></td>'; echo " <td><strong> ".($i+1).".Model : </strong>"; echo htmlspecialchars(stripslashes($row["Model"])); echo "<br><strong> Description: </strong>" ; echo htmlspecialchars (stripslashes($row["Description"])); echo "<br><strong> Category: </strong>"; echo htmlspecialchars (stripslashes($row["Category"])); echo "<br></td></table>";$i++; } Thanks much Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642372 Share on other sites More sharing options...
kenrbnsn Posted September 15, 2008 Share Posted September 15, 2008 This if statement <?php if( $i = $i%2 ) ?> is using the single "=" which is the assignment operator. You should be using the double "==" which is the comparison operator for "equals". What you're doing now is reassigning $i to be $i%2, causing the infinite loop. Ken Link to comment https://forums.phpfreaks.com/topic/124379-if-statement/#findComment-642386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.