Jump to content

Alternating row colours problem [RESOLVED}


AdRock

Recommended Posts

I am having problems alternating my divs similar to rows

I want 2 records dsiplayed per page each with an image on opposite sides of the page.

I can't get it to work.  I tried using a for loop and it either crashes the browser or it tries to keep adding loads of records to the page.

I can;t see why it's not working if anyone can see what the problem is  ???

This displays ok but with the images on the right
[code]<h2 style="margin-bottom:0">About Honeylands - A little history</h2><hr>
<div>
<?php

include "includes/connection.php";

$webpage = basename(history);
function pagination_five($total_pages,$page){

    global $webpage;

    $max_links = 10;
    $h=1;
    if($page>$max_links){
        $h=(($h+$page)-$max_links);
    }
    if($page>=1){
        $max_links = $max_links+($page-1);
    }
    if($max_links>$total_pages){
        $max_links=$total_pages+1;
    }
    echo '<div class="page_numbers">
      <ul>';
    if($page>"1"){
        echo '<li class="current"><a href="/'.$webpage.'/1">First</a></li>
              <li class="current"><a href="/'.$webpage.'/'.($page-1).'">Prev</a></li>
              ';
    }
     
    if($total_pages!=1){
        for ($i=$h;$i<$max_links;$i++){
            if($i==$page){
                echo '<li><a class="current">'.$i.'</a></li>';
            }
            else{
                echo '<li><a href="/'.$webpage.'/'.$i.'">'.$i.'</a> </li>';
            }
        }
    }
     
    if(($page >="1")&&($page!=$total_pages)){
        echo '<li class="current"><a href="/'.$webpage.'/'.($page+1).'">Next</a></li>
              <li class="current"><a href="/'.$webpage.'/'.$total_pages.'">Last</a></li>
              ';
    }
     
    echo '</ul>
            </div>
            ';
}

$result = mysql_query("Select count(*) FROM `info` WHERE id BETWEEN '9' AND '13'")
or die (mysql_error());
$numrows = mysql_fetch_row($result);
 
if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1);
$entries_per_page = 2;   

$total_pages = ceil($numrows[0]/$entries_per_page);
$offset = (($page * $entries_per_page) - $entries_per_page);

    //after we have $total_pages and $page, we can include the 
    //pagination style wherever we want on the page.
    //so far there is no output from the script, so we could have 
    //pagination before or after the pages results
     
    //before the results 

$result = mysql_query("SELECT * FROM `info` WHERE id BETWEEN '9' AND '13' LIMIT 
                       $offset,$entries_per_page");

while($row=mysql_fetch_array($result)){
     
    if($i % 2) { //this means if there is a remainder
        echo "<div class='info' id='left'>";
    } else { //if there isn't a remainder we will do the else
        echo "<div class='info' id='right'>";
    }
    echo "<p class='style2'><img src='/images/".$row['photo']."' alt='".$row['alternate']."'>".nl2br($row['content'])."</p>";
    echo "</div>";
}

  //or after the results
   
pagination_five($total_pages,$page);

?>
</div>[/code]

This is where the problem is
[code]$result = mysql_query("SELECT * FROM `info` WHERE id BETWEEN '9' AND '13' LIMIT 
                       $offset,$entries_per_page");

while($row=mysql_fetch_array($result)){
     
    if($i % 2) { //this means if there is a remainder
        echo "<div class='info' id='left'>";
    } else { //if there isn't a remainder we will do the else
        echo "<div class='info' id='right'>";
    }
    echo "<p class='style2'><img src='/images/".$row['photo']."' alt='".$row['alternate']."'>".nl2br($row['content'])."</p>";
    echo "</div>";
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/28442-alternating-row-colours-problem-resolved/
Share on other sites

it's because you're not even setting $i:

[code]<?php
$result = mysql_query("SELECT * FROM `info` WHERE id BETWEEN '9' AND '13' LIMIT 
                      $offset,$entries_per_page");

$i = 1;

while($row=mysql_fetch_array($result)){
   
    if($i % 2) { //this means if there is a remainder
        echo "<div class='info' id='left'>";
    } else { //if there isn't a remainder we will do the else
        echo "<div class='info' id='right'>";
    }
    echo "<p class='style2'><img src='/images/".$row['photo']."' alt='".$row['alternate']."'>".nl2br($row['content'])."</p>";
    echo "</div>";

    ++$i;
}
?>[/code]
I think this is from one of the PHPFreaks Tutorials, But I always use it.
[code]
<?php
$color1 = "#f6f6ff"; 
$color2 = "#e7e8ff";
$row_count = 0;
while ($row=mysql_fetch_array($result)) {
        $row_color = ($row_count % 2) ? $color1 : $color2;
        echo "<div class='info' id='left' style='background-color:$row_color;'>";
        echo "<p class='style2'><img src='/images/".$row['photo']."' alt='".$row['alternate']."'>".nl2br($row['content'])."</p>";
        echo "</div>";
$row_count++;
}
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

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