Jump to content

Recommended Posts

Well, I'm not positive what "code" you are referring to, but there are a couple ways to alternate things. For instance, if you are wanting to alternate row colors, you'd simply have to do something like this:

<?php
$class = 'even';
while ($row = mysql_fetch_assoc($res))
{
  $class = $class == 'even' ? 'odd' : 'even'; // Here's where the magic happens
  echo "<tr class=\"$class\">\n";
  // Display your table record here
  echo "</tr>\n";
}
?>

 

However, if you are actually looking at handling the PHP differently for the separate records, you may want to try something like this:

<?php
$i = 0;
while ($row = mysql_fetch_assoc($res))
{
  ++$i;
  if ($i % 2 == 0) // Here are your even records
  {

  }
  else // Here are your odd records
  {

  }
}
?>

 

Hope this helps!

below is my code for my news, im trying to alternate the even rows so the image is on the left on them which is what ive done given the code you gave me, but whats happening is its just displaying the record1 twice on the page

 

is this becuase of the pagnation code?

 

cheers

 

<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/inner-head.php'); 

$max = 3; //amount of articles per page. change to what to want
$p = @$_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max; 

//view all the news articles in rows
$sql = mysql_query("SELECT * FROM tblNews LIMIT ".$limits.",$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_result(mysql_query("SELECT COUNT(NewsID) AS tot FROM tblNews"),0);	
//the total number of pages (calculated result), math stuff...
$totalpages = ceil($totalres / $max); 
echo '<h3>News</h3>';
echo "<div style='text-align:center;'>View Page: ";
for($i = 1; $i <= $totalpages; $i++){ 
//this is the pagination link
echo "<a href='short_news.php?p=$i'>$i</a>|";
}
echo "</div>";
while($news = mysql_fetch_array($sql))
{
$i = 0;
while ($row = mysql_fetch_assoc($sql))
{
  ++$i;
  if ($i % 2 == 0) // Here are your even records
  {
echo '<div style="float:left; width:100%; margin-bottom:10px;">';
echo '<a href="fullstory.asp?id='.$news["NewsID"].'">'.$news["NewsTitle"]."</a>";
echo "  -  <i>";
echo $news["NewsDateAdded"];
echo "</i></div>";

if (!empty($news["NewsImage1"])) {

echo '<div style="width:200px; float:left;">';
echo '<img style="border:1px dashed #666666;" src="/news/uploads/'.$news["NewsImage1"].' " alt=" '.$news["NewsImage1"].' " />';
echo '</div>';
}

echo '<div style="width:70%; margin-right:10px; float:left; text-align:justify;">';
echo str_replace("\n", "<br />",$news["NewsShort"]);
echo "</div>";

if (!empty($news["NewsAttachment"])) {
echo "<div style=\"float:left; width:100%;\">Attachment";
echo '<a href="/news/uploads/';
echo $news ["NewsAttachment"].'" onclick="target=\'blank\';">';
echo $news ["NewsAttachment"]."</a></div>";
} 
echo '<div style="padding:10px 0; border-bottom:1px dashed #666666; float:left; width:100%; text-align:center; margin-bottom:30px;"><a href="/news/full_news.php?id='.$news["NewsID"].'">'."View the full story"."</a></div>"; 
  }
  else // Here are your odd records
  {
echo '<div style="float:left; width:100%; margin-bottom:10px;">';
echo '<a href="fullstory.asp?id='.$news["NewsID"].'">'.$news["NewsTitle"]."</a>";
echo "  -  <i>";
echo $news["NewsDateAdded"];
echo "</i></div>";
echo '<div style="width:70%; margin-right:10px; float:left; text-align:justify;">';
echo str_replace("\n", "<br />",$news["NewsShort"]);
echo "</div>";

if (!empty($news["NewsImage1"])) {

echo '<div style="width:200px; float:left;">';
echo '<img style="border:1px dashed #666666;" src="/news/uploads/'.$news["NewsImage1"].' " alt=" '.$news["NewsImage1"].' " />';
echo '</div>';
}

if (!empty($news["NewsAttachment"])) {
echo "<div style=\"float:left; width:100%;\">Attachment";
echo '<a href="/news/uploads/';
echo $news ["NewsAttachment"].'" onclick="target=\'blank\';">';
echo $news ["NewsAttachment"]."</a></div>";
} 
echo '<div style="padding:10px 0; border-bottom:1px dashed #666666; float:left; width:100%; text-align:center; margin-bottom:30px;"><a href="/news/full_news.php?id='.$news["NewsID"].'">'."View the full story"."</a></div>"; 
  }
}

}
echo "<div style='text-align:center; clear:both;'>View Page: ";
for($i = 1; $i <= $totalpages; $i++){ 
//this is the pagination link
echo "<a href='short_news.php?p=$i'>$i</a>|";
}
echo "</div>";

include($_SERVER['DOCUMENT_ROOT'] . '/includes/inner-foot.php');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php');

?>

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.