OsirisElKeleni Posted October 10, 2014 Share Posted October 10, 2014 hey guys! i'm back again with another question I've been messing with the piece of code for a few days now and can't get it to function properly. i'm trying to make each row a different color which works... BUT it leaves out some data and prints it double... This is the piece of code that will help you find what i've been doing wrong... $color="1"; while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) { if($color==1){ $cardname = $row['Name']; $encodedcardname = str_replace(" ","%20",trim($cardname)); $url = "http://mtgimage.com/card/$encodedcardname.jpg"; echo "<tr bgcolor=''><td>"; echo "<a href=$url>$cardname</a>"; echo "</td><td>"; echo $row['Color']; echo "</td><td>"; echo $row['Type']; echo "</td><td>"; echo $row['Subtype']; echo "</td><td>"; echo $row['Power']; echo "</td><td>"; echo $row['Toughness']; echo "</td><td>"; echo $row['Manacost']; echo "</td><td>"; echo $row['Rarity']; echo "</td><td>"; echo $row['Expansion']; echo "</td><td>"; echo $row['Foil']; echo "</td><td>"; echo $row['Stock']; echo "</td></tr>"; $color="2"; } else { echo "<tr bgcolor='#F8F8F8'><td>"; echo "<a href=$url>$cardname</a>"; echo "</td><td>"; echo $row['Color']; echo "</td><td>"; echo $row['Type']; echo "</td><td>"; echo $row['Subtype']; echo "</td><td>"; echo $row['Power']; echo "</td><td>"; echo $row['Toughness']; echo "</td><td>"; echo $row['Manacost']; echo "</td><td>"; echo $row['Rarity']; echo "</td><td>"; echo $row['Expansion']; echo "</td><td>"; echo $row['Foil']; echo "</td><td>"; echo $row['Stock']; echo "</td></tr>"; $color="1"; } } echo "</table>"; A picture of the result of whats it outputting right now is attached to this thread. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted October 10, 2014 Solution Share Posted October 10, 2014 The "duplicates" are likely caused by $cardname (and the other variables) only being updated when $color == 1. Try moving the variables below before the if statement: if($color==1){ $cardname = $row['Name']; $encodedcardname = str_replace(" ","%20",trim($cardname)); $url = "http://mtgimage.com/card/$encodedcardname.jpg"; //... Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 10, 2014 Share Posted October 10, 2014 Also note that the code could be simplified a bit. The following is untested: <?php //INITIALIZE COUNT $count = 0; while($row = mysqli_fetch_assoc($retval)) { $cardname = $row['Name']; $encodedcardname = str_replace(" ","%20",trim($cardname)); $url = "http://mtgimage.com/card/$encodedcardname.jpg"; //GET ROW COLOR $bgcolor = ($count % 2) ? '' : ' bgcolor="#F8F8F8"'; //DISPLAY CURRENT ROW echo '<tr' . $bgcolor . '><td>'; echo "<a href=$url>$cardname</a>"; echo "</td><td>"; echo $row['Color']; echo "</td><td>"; echo $row['Type']; echo "</td><td>"; echo $row['Subtype']; echo "</td><td>"; echo $row['Power']; echo "</td><td>"; echo $row['Toughness']; echo "</td><td>"; echo $row['Manacost']; echo "</td><td>"; echo $row['Rarity']; echo "</td><td>"; echo $row['Expansion']; echo "</td><td>"; echo $row['Foil']; echo "</td><td>"; echo $row['Stock']; echo "</td></tr>"; //INCREMENT COUNT $count++; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
OsirisElKeleni Posted October 10, 2014 Author Share Posted October 10, 2014 This did do the trick! Thanks alot! The "duplicates" are likely caused by $cardname (and the other variables) only being updated when $color == 1. Try moving the variables below before the if statement: if($color==1){ $cardname = $row['Name']; $encodedcardname = str_replace(" ","%20",trim($cardname)); $url = "http://mtgimage.com/card/$encodedcardname.jpg"; //... I don't fully understand what all this does, so i rather not use it for now i could look into it but its working fine now, If you want to explain this piece of code i'd love to learn it but i dont really like to implement things in my web unless i understand it a bit and know how to mess around with it. Still you helped me and thanks for that! Also note that the code could be simplified a bit. The following is untested: <?php //INITIALIZE COUNT $count = 0; while($row = mysqli_fetch_assoc($retval)) { $cardname = $row['Name']; $encodedcardname = str_replace(" ","%20",trim($cardname)); $url = "http://mtgimage.com/card/$encodedcardname.jpg"; //GET ROW COLOR $bgcolor = ($count % 2) ? '' : ' bgcolor="#F8F8F8"'; //DISPLAY CURRENT ROW echo '<tr' . $bgcolor . '><td>'; echo "<a href=$url>$cardname</a>"; echo "</td><td>"; echo $row['Color']; echo "</td><td>"; echo $row['Type']; echo "</td><td>"; echo $row['Subtype']; echo "</td><td>"; echo $row['Power']; echo "</td><td>"; echo $row['Toughness']; echo "</td><td>"; echo $row['Manacost']; echo "</td><td>"; echo $row['Rarity']; echo "</td><td>"; echo $row['Expansion']; echo "</td><td>"; echo $row['Foil']; echo "</td><td>"; echo $row['Stock']; echo "</td></tr>"; //INCREMENT COUNT $count++; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 10, 2014 Share Posted October 10, 2014 If you want to explain this piece of code i'd love to learn it... Is there a particular line of code that I can help explain? The most foreign part is probably this: $bgcolor = ($count % 2) ? '' : ' bgcolor="#F8F8F8"'; This line uses the ternary operator to keep the code short. You could re-write it as: if($count % 2) { $bgcolor = ''; } else { $bgcolor = ' bgcolor="#F8F8F8"'; } More information about the ternary operator can be found here: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary To calculate the row color, I just used the modulus operator which is discussed here: http://php.net/manual/en/language.operators.arithmetic.php#70424 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.