Jump to content

DataTable in different color rows.


OsirisElKeleni
Go to solution Solved by cyberRobot,

Recommended Posts

hey guys!

i'm back again with another question :D

 

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!

post-173017-0-37372900-1412962636_thumb.png

Link to comment
Share on other sites

  • Solution

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";
     //...
Link to comment
Share on other sites

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>"; 
?>
Link to comment
Share on other sites

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>"; 
?>
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.