views:

56

answers:

3

Hey all, I've got an about page, and two HTML columns and I want to while loop the contents of my mysql query into BOTH columns, even though they are defined as two different divs. What I was thinking of doing is somehow dividing the amount of rows in the mysql database by 2, and then showing half and half, but I'm not sure how to do this (specifically doing the half and half mysql queries)

Could someone please either show me how I can halve a mysql database into two parts, or come up with a better solution to my problem? Thankyou :).

EDIT:

<?php $djs_all_db = mysql_query("SELECT * FROM djs")
        or die(mysql_error());      
        while($djs_all = mysql_fetch_array( $djs_all_db )) {
        echo    "
        <div class="row">
        <!--It does it once here-->
        <div class=\"column column-2\">
                <img src=\"images/about/" . $djs['username'] . "-profile.png\" alt=\"Profile\" class=\"profile-image\"/>

                <p class=\"float-left\"><strong>" . $djs['realname'] . "</strong></p>
                <p class=\"float-right\"><a href=\"#\" title=\"\">" . $djs['twitterusername'] . "</a></p>

                <div class=\"clear\"></div>

                <p>" . $djs['biography'] . "</p>

            </div>
        <!--but now for column 2?-->
            <div class=\"column column-3\">
                <img src=\"images/about/profile.png\" alt=\"Profile\" class=\"profile-image\"/>

                <p class=\"float-left\"><strong>Edward Smith, Developer</strong></p>
                <p class=\"float-right\"><a href=\"#\" title=\"\">Twitter</a></p>

                <div class=\"clear\"></div>

                <p>In tellus arcu, luctus sed vulputate ut, dictum sed nisi. Suspendisse commodo, enim sed mollis cursus, urna quam laoreet velit, vel sollicitudin arcu augue at quam.</p>
            </div>

        </div> <!--/.row-->" ?>
A: 

Try something like this:

$i=0;
//1rst div:
        while((count($mysql_results)/2) < $i) 
        {
            echo $mysql_results[$i]; 
            i++;
        }
//2nd div: 
        while((count($mysql_results)/2 >= $i) 
        {
            echo $mysql_results[$i]; 
            i++;
        }
cypher
The while loops are wrong, but this will work if you don't mind the first half of your results in the first div. Should be while $i<count($mysql_results)/2 and then while $i<count($mysqlresults) If you want them in both, loop through the results twice and in the first div do if(i%2==0) and include if true. In the second loop, do if(i%2==1) and include if true. Appologies if my syntax is off, I haven't done much with php.
Kendrick
Yes, sorry about the conditions :-)
cypher
A: 

As this reads to me, you're wanting to concatenate the contents of two separate columns and multiple rows in a table into two divs?

Try this:

while ($result = mysql_fetch_assoc($db_result)
{
  $div1 .= $result['column1'];
  $div2 .= $result['column2'];
}

echo "<div class="div1">$div1</div>";
echo "<div class="div2">$div2</div>";

Hope I read the question correctly....

Good luck.

Ian P
+1  A: 

You can append every odd to 1st column and every even to 2nd column variables and then flush them into the HTML.

See my pseudocode:

while(there_are_more_rows) {
  if(row_num_is_odd) {
    $first_column += $result_row;
  } else {
    $second_column += $result_row;
  }
}
print "<div='1st-column'>" . $first_column . "</div>";
print "<div='2nd-column'>" . $second_column . "</div>";
Michał Pękała