I have a div named as container and inside container I have two div column1 and column2.Inside column1 and column2 I have to fetch record from database and have to show it here.My code look like this
<div id="container" style="height:600px; width:600px; border:1px solid #990000">
<?php $str=mysql_query("SELECT * FROM users") or die(mysql_error());
while($rec=mysql_fetch_array($str){
?>
<div id="column1" style="height:200px; width:295px; border:1px solid #0000FF;float:left;">
/*Here this record should be of id=1 */
<img src="<?php echo $rec['pic'];?>" alt="No image" />
<?php echo $rec['detail'];
</div>
<div id="column2" style="height:200px; width:295px; border:1px solid #0000FF;float:left;">
/*Here this record should be of id=2 */
<img src="<?php echo $rec['pic'];?>" alt="No image" />
<?php echo $rec['detail'];
</div>
<?php } ?>
I want to have in column1 record of user having id=1 and in column2 id=2 and again in column1 id=3 and column2 id=4 and so on. Any help me in this looping?
You are using a deprecated extension, by the way. I highly recommend you use the newer MySQLi or PDO extensions to interface with your MySQL database in PHP for new development as this will make your job so much easier.
The answer to what you want to do is simply to traverse over the result set in multiples of two, which can be achieved as demonstrated below with the PDO extension.
The other option is to use a counter and flip it with each iteration then follow that by a condition to determine which div you want to output for this iteration of the loop.