adzie Posted July 17, 2009 Share Posted July 17, 2009 Hello again folks, hope somone can help, I want my db results to display in a different format ie odd left and even right side of page. Any help appreciated. <?php $query = "SELECT * FROM members WHERE group='1'"; $result = mysql_query($query) or die('Error.'); $number = mysql_numrows($result); if ($number > 0) { for ($i=0; $i<$number; $i++) { $location = mysql_result($result,$i, "location"); $image = mysql_result($result,$i, "image"); ?> <div class="ac"> <h5><?php echo "$location"; ?></h5> <a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a> </div> <br /> <?php } } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 17, 2009 Share Posted July 17, 2009 you're going to have to give us more than that. do you already have styles defined that will put the image where you want it in each case? Quote Link to comment Share on other sites More sharing options...
ldougherty Posted July 17, 2009 Share Posted July 17, 2009 I'm not sure that I understand you completely but you could simply put the results into a table. Essentially start the <TR> before you start outputting the results Have a counter that increments after each set of data is echoed Have a <TD></TD> around each result Have an if statement stating if the counter is odd then </TR><TR> The logic is that you'll have a <TR>, two sets of <TD></TD> and then a </TR><TR> to start the next line. Quote Link to comment Share on other sites More sharing options...
adzie Posted July 17, 2009 Author Share Posted July 17, 2009 what I was hoping to do for each row/result was to create a section so for the odd rows <div class="ac"> <h5><?php echo "$location"; ?></h5> <a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a> </div> <br /> and for the even rows <div class="ac2"> <h5><?php echo "$location"; ?></h5> <a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a> </div> <br /> Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 17, 2009 Share Posted July 17, 2009 this is merely a matter of using the modulus operator to check if the counter is even or odd on each iteration of the loop: for ($i=0; $i<$number; $i++) { $location = mysql_result($result,$i, "location"); $image = mysql_result($result,$i, "image"); $div_class = ($i % 2 == 1) ? 'class_if_odd' : 'class_if_even'; ?> <div class="<?php echo $div_class; ?>"> <h5><?php echo "$location"; ?></h5> <a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a> </div> <br /> <?php } } ?> change the two classes in the line i added to be whatever you want (here, most likely "ac" and "ac2" based on your messages). so you're aware, the modulus operator returns the remainder after dividing by the divisor. here we're using 2, so any even numbers will return 0 (because they're divisible by 2), and odd numbers will return 1. Quote Link to comment 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.