jesushax Posted March 27, 2008 Share Posted March 27, 2008 hi was wonering how i can alternate code for records like records 1,3,5,7,9 have (THIS CODE) and records 2,4,6,8 have (THAT CODE) you know what i mean? thanks for help Quote Link to comment https://forums.phpfreaks.com/topic/98153-alternating-code-for-records/ Share on other sites More sharing options...
AdRock Posted March 27, 2008 Share Posted March 27, 2008 not really Quote Link to comment https://forums.phpfreaks.com/topic/98153-alternating-code-for-records/#findComment-502132 Share on other sites More sharing options...
obsidian Posted March 27, 2008 Share Posted March 27, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/98153-alternating-code-for-records/#findComment-502134 Share on other sites More sharing options...
jesushax Posted March 27, 2008 Author Share Posted March 27, 2008 yes the second one is what im after having different code for differnt recrds, its all about displaying them differnt thanks! Quote Link to comment https://forums.phpfreaks.com/topic/98153-alternating-code-for-records/#findComment-502152 Share on other sites More sharing options...
jesushax Posted March 27, 2008 Author Share Posted March 27, 2008 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/98153-alternating-code-for-records/#findComment-502170 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.