Page 1 of 1

Alternating Rows In PHP Problem

Posted: Fri Jul 23, 2010 7:48 am
by sohailamir
Hi, everyone, I've spent 2 days straight trying to get this to work so you guys are my last hope really, take a look at the snippet of code I'm editing at the moment:

Code: Select all

<?php

$result = mysql_query($query) or die ("Query failed");
$numofrows = mysql_num_rows($result);
  if(isset($_GET['search']))
  {
    if(isset($list) && count($list) > 0)
    {

      foreach($list as $row)
      {
   $i = 0; $i < $numofrows; $i++;
         print "<tr class=\"car-row-".($i % 2)."\">";
         ?>
         
            <td><a href="index.php?option=com_jumi&fileid=5&Itemid=44&vehicle_id=<?php echo $row['listingid']; ?>"><img src="http://www.findabreaker.co.uk/breakeradmin/uploaded/<?php echo $row['car_image']; ?>" /></a></td>
            <td><?php echo $row['listing_title']; ?></td>
            <!--<td width="20%"><?php //echo $row['cat_name']; ?></td>--?
            <!--<td width="20%"><?php //echo $row['postcode']; ?></td>-->
            <?php
              if(isset($_GET['postcode']) && strlen($_GET['postcode']) > 0)
              {
            ?>
            <td><?php echo $row['distance']; ?> kms</td>
            <?php
              }
            ?>
            <td><?php echo $row['county_name']; ?></td>
            <td><img src="images/glass.gif" width="16" height="16" border="0"/><a href="index.php?option=com_jumi&fileid=5&Itemid=44&vehicle_id=<?php echo $row['listingid']; ?>"> View Details</a></td>
          </tr>
      <?php
      }
    }
    else
    {
      echo 'No vehicles found!';
    }
  }
?>
        </tbody>
      </table>
      </div>
 
The URL for the live site is http://finda**take out this space**breaker.co.uk/index.php?car_listingcatid=&car_make=&car_model=&keywords=&postcode=&search=Search&option=com_jumi&fileid=4&Itemid=44&sort=title&sort_type=asc

What I'm trying to do is have alternating colours for the rows, but after looking everywhere on the net, none of the solutions work, I need to have a different style for alternating rows, like <tr class="class-1"> next row <tr class="class-2"> next <tr class="class-1"> etc... I hope you understand what I mean and anyone who can help me out I'd really really appreciate it.

Re: Alternating Rows In PHP Problem

Posted: Fri Jul 23, 2010 8:04 am
by buckit
This is how I do it...

please this in your loop

Code: Select all

$alternate = ($alternate == 'even') ? 'odd' : 'even';
where 'even' and 'odd' are your row style names


Then on the elements within the loop:

Code: Select all

<div class="<?php echo $alternate;?>"></div>

Re: Alternating Rows In PHP Problem

Posted: Fri Jul 23, 2010 9:51 am
by N1gel

Code: Select all

$i=0;
foreach($list as $row)
      {
         if($i % 2)
{
echo"<tr class='class1'>";
}
else
{
echo"<tr class='class2'>";
}
         
         ?>
         
            <td><a href="index.php?option=com_jumi&fileid=5&Itemid=44&vehicle_id=<?php echo $row['listingid']; ?>"><img src="http://www.findabreaker.co.uk/breakeradmin/uploaded/<?php echo $row['car_image']; ?>" /></a></td>
            <td><?php echo $row['listing_title']; ?></td>
            <!--<td width="20%"><?php //echo $row['cat_name']; ?></td>--?
            <!--<td width="20%"><?php //echo $row['postcode']; ?></td>-->
            <?php
              if(isset($_GET['postcode']) && strlen($_GET['postcode']) > 0)
              {
            ?>
            <td><?php echo $row['distance']; ?> kms</td>
            <?php
              }
            ?>
            <td><?php echo $row['county_name']; ?></td>
            <td><img src="images/glass.gif" width="16" height="16" border="0"/><a href="index.php?option=com_jumi&fileid=5&Itemid=44&vehicle_id=<?php echo $row['listingid']; ?>"> View Details</a></td>
          </tr>
      <?php
      $i++;
      }