php alternating value?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

lets assume that this is my code:

Code: [ Select ]
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $altenatingVarible . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>
  1. <?php
  2. $con = mysql_connect("localhost","peter","abc123");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7. mysql_select_db("my_db", $con);
  8. $result = mysql_query("SELECT * FROM Persons");
  9. echo "<table border='1'>
  10. <tr>
  11. <th>Firstname</th>
  12. <th>Lastname</th>
  13. </tr>";
  14. while($row = mysql_fetch_array($result))
  15. {
  16. echo "<tr>";
  17. echo "<td>" . $row['FirstName'] . "</td>";
  18. echo "<td>" . $row['LastName'] . "</td>";
  19. echo "<td>" . $altenatingVarible . "</td>";
  20. echo "</tr>";
  21. }
  22. echo "</table>";
  23. mysql_close($con);
  24. ?>


Every time a new html table row is created I need $altenatingVarible to alternate between 1 and 2. So the first row $altenatingVarible would equal 1. And the second row $altenatingVarible equals 2. And the third row $altenatingVarible equals 1 again, and so on.

I considered replacing $altenatingVarible with rand(1,2); but there would only be a 50% probability with each row, that i would get the 1,2,1,2,1,2,1,2,1 pattern that I require. Is there another way enirely to reach this goal that I am missing?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Is it something truly random that you want, or do you just want every other row? In that case it's Odd's and Even's. Add a counter to your row and use the modulus operator.

Something like this:
PHP Code: [ Select ]
<?php
$Count = 0;
while($row = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" .(($Count % 2 == 0) ? 'Even 2' : 'Odd 1'). "</td>";
  echo "</tr>";
$Count++;
}
?>
  1. <?php
  2. $Count = 0;
  3. while($row = mysql_fetch_array($result))
  4. {
  5.   echo "<tr>";
  6.   echo "<td>" . $row['FirstName'] . "</td>";
  7.   echo "<td>" . $row['LastName'] . "</td>";
  8.   echo "<td>" .(($Count % 2 == 0) ? 'Even 2' : 'Odd 1'). "</td>";
  9.   echo "</tr>";
  10. $Count++;
  11. }
  12. ?>


If you want it to go Odd, Even instead of Even, Odd, just move the counter incrementer before the output.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

http://develobert.blogspot.com/2008/01/ ... bject.html

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 57 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.