Gilbert Skrevet 6. august 2003 Del Skrevet 6. august 2003 txt-dokument med informasjon: bnm1||tff bnm2||turr bnm3||turi bnm4||turd $fil = file('info/ordliste.php'); foreach($fil as $e => $p){ list($ord,$forklaring) = explode("||", $p); echo "<tr><td [b]bgcolor=ffffff[/b]><b><a name="$ord"><li> 8$ord</b></a><br>$forklaring</td></tr>"; } Ønsker at scripet skal gjøre annenhver bgcolor til en annen farge. slik at bnm1 og bnm3 blir med f.eks. #129855 Sikker på at dette er mulig ;-) Lenke til kommentar
Valagar Skrevet 6. august 2003 Del Skrevet 6. august 2003 Før loopen setter du: $i = 0; Deretter bytter du ut <td>en med: <td style="background: " . (($i++ % 2 == 0) ? "#FFFFFF" : "#CCCCCC") . ""> Har ikke testet, men tror koden skal virke. Lenke til kommentar
magikern Skrevet 7. august 2003 Del Skrevet 7. august 2003 quoter litt fra et annet forum: I've seen some people ask this before, so I thought I'd write a short tutorial. How do you alternate row colors using PHP? It's simple really, wherever you data is, whether it's in an array, database, or file, just loop through it and print this line: <tr bgcolor="<?=($i%2)?"#ffffff":"#c0c0c0";?>"> <td>YOUR DATA GOES HERE</td> </tr> The % is just the remainder if the top number (numerator) was divded by the bottom number (denominator). To further explain this.... it may be easier to read through examples... Example 1: Data in an array <table> <? $myarray = array('apples', 'oranges', 'banana'); $i = 0; foreach($myarray as $value) { ?> <tr bgcolor="<?=($i%2)?"#ffffff":"#c0c0c0";?>"><td><?= $value ?></td></tr> <? $i++; } ?> </table> This should look like this: Example 2: Data in a Database $sql = "SELECT * FROM fruit"; $results = mysql_query($sql); $i = 0; while($the_fruits = mysql_fetch_row($results)) { ?> <tr bgcolor="<?=($i%2)?"#ffffff":"#c0c0c0";?>"><td><?= $the_fruits["fruit"] ?></td></tr> <? $i++; } Example 3: Data in a file Say myfile.txt contains: 1,apple 2,orange 3,banana The code would look like this: $filename = 'myfile.txt'; // Read file contents $fileptr = @fopen($filename, 'r'); while (!feof($fileptr)) $temp .= fread($fileptr, 4096); $source = explode("n", $temp); $i = 0; foreach($source as $data) { list($rownum,$fruit) = explode(",",$data); if($i%2) $rowcolor = '#ffffff'; else $rowcolor = '#c0c0c0'; echo '<tr bgcolor="'. $rowcolor.'"><td align="right" width="100">'. $rownum .'</td><td width="100">'. $fruit .'</td></tr>'; $i++; } echo '</table>'; <?=($i%2)?"#ffffff":"#c0c0c0";?> is the shorthand 'if' conditional statement. It means the same thing as: <? if($i%2) echo "#ffffff"; else echo "#c0c0c0"; ?> you can also use $i & 1, which does a binary check on the number. if the last bit is 1, then it's odd. if not, it's even example (decimal to binary): 1 => 1 2 => 10 3 => 11 4 => 100 5 => 101 etc I think it's a little faster too. <table> <? $myarray = array('apples', 'oranges', 'banana'); $i = 0; foreach($myarray as $value) { ?> <tr bgcolor="<?=($i&1)?"#ffffff":"#c0c0c0";?>"><td><?= $value ?></td></tr> <? $i++; } ?> </table> and yet another way: <?php echo("<table>"); $num = 10; for($i = 0; $i < $num; $i++) { if(round($i/2) == $i/2) { echo "<tr bgcolor="#cccccc">"; } else { echo "<tr bgcolor="#eeeeee">"; } echo "<td>$i</td></tr>"; } echo("</table>"); ?> det skulle være en god del forskjellige måter å gjøre dette på... Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå