PHP looping problem?
I'm trying to display the first row in one color and th开发者_开发百科e second row in another color but my code displays the result twice in both colors for example lets say I have 5 results my code will double the results by displaying 10 results. How can I fix this problem?
Here is the php code.
while ($row = mysqli_fetch_assoc($dbc)) {
//first row
echo '<h3 class="title"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
//second row
echo '<h3 class="title-2"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary-2"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
}
You need to change the class on each row:
$count = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if( $count % 2 == 0 ) {
$classMod = '';
} else {
$classMod = '-2';
}
//first row
echo '<h3 class="title' . $classMod . '"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary' . $classMod . '"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
$count++;
}
your code should be like this
CSS
.odd { background: #CCC }
.event { background: #666 }
PHP
$c = true;
while ($row = mysqli_fetch_assoc($dbc)) {
$style = (($c = !$c)?' odd':' even');
echo '<h3 class="title '.$style.'"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary '.$style.'"><a href="#" title="">' .substr($row['content'],0,255) . '</a></div>';
}
Here's a solution with minimal repetition:
$count = 0;
while (($row = mysqli_fetch_assoc($dbc)) && ++$count) {
printf(
'<h3 class="title%1$s"><a href="#" title="">%2$s</a></h3>'
. '<div class="summary%1$s"><a href="#" title="">%3$s</a></div>'
, $count % 2 ? "" : "-2"
, $row['title'] // might want to use htmlentities() here...
, substr($row['content'], 0, 255) // and here...
);
}
$i = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
$color =$i % 2;
echo '<h3 class="title-' .$color . '"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
$i++;
}
Your code just displays every result twice. Use a conditional (e.g. an integer or a boolean) to switch between rows (like: if true, then green; if false, then red).
For a boolean you could change the current value like so:
bool = !bool;
Couple of extra points:
- You don't (normally) need so many classes. If you have
<div class="stripe">
as your container, you can target the items with e.g..stripe h3
in CSS. - If you target the odd and even items in CSS with
.stripe h3
, you can then overwrite just the odd items. - In a perfect world, you should keep presentation in the CSS. All browsers but IE7 and below support
div:odd
to target any odd child of a parent. This may require changing the structure of your HTML slightly. For IE7 and below, I'd add classes with JavaScript instead of PHP. When IE7 is no more then you can just remove the JS.
By the way, you could do this code too:
while ($row = mysqli_fetch_assoc($dbc)) {
echo '<h3 class="title"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
if($row = mysqli_fetch_assoc($dbc)){
echo '<h3 class="title-2"><a href="#" title="">' . $row['title'] .'</a></h3>';
echo '<div class="summary-2"><a href="#" title="">' . substr($row['content'],0,255) . '</a></div>';
}
}
IMO, there is no excuse for not delegating this kind of non-critical, presentation layer decoration to client side code.
Just use a library like jQuery and access the odd and even rows like so:
<script>
$(document).ready(function()
{
//for your table rows
$("tr:even").css("background-color", "#F4F4F0");
$("tr:odd").css("background-color", "#EFF1F2");
});
</script>
You'll have us generating font tags next.
精彩评论