Having trouble echo data into 2 columns

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

inoob
New php-forum User
New php-forum User
Posts: 31
Joined: Tue Nov 06, 2012 12:20 pm

Having trouble echo data into 2 columns

Postby inoob » Sun Sep 08, 2013 1:59 pm

I am trying to echo random 10 results from my database and display them into 2 columns. My problem is it dislpays in 1 column and I have no idea how to split the 10 random records by 5 for both sides of the table

Heres the code:

Code: Select all

<?php
$query = '(SELECT * FROM database) ORDER BY RAND() LIMIT 10';
$result = mysqli_query($conn,$query) or die("Could not perform the requested query!<BR>
".mysqli_error()); while ($row = mysqli_fetch_array($result)) { echo '<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%">
<tr><td  width="100%">
  <table border="0" cellspacing="0" style="border-collapse: collapse" width="100%" cellpadding="0"><tr><td width="17">
<img border="0" src="images/arrow.gif" width="17" height="17"></td><td width="97%"><b>
  <a href="article.php?id='.$row['fldID'].'">'.$row['fldTitle'].'</a></b></td>
</tr></table></td></tr></table><p>';}?>



Instead of having the results in 1 table cell column like this:
[ record 1 ][ ]
[ record 2 ][ ]
[ record 3 ][ ]
[ record 4 ][ ]
[ record 5 ][ ]
[ record 6 ][ ]
[ record 7 ][ ]
[ record 8 ][ ]
[ record 9 ][ ]
[ record 10 ][ ]


I want it to display all 10 records in 2 table cell columns like this:
[ record 1 ][ record 6 ]
[ record 2 ][ record 7 ]
[ record 3 ][ record 8 ]
[ record 4 ][ record 9 ]
[ record 5 ][ record 10 ]

What can I do?

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Having trouble echo data into 2 columns

Postby johnj » Sun Sep 08, 2013 8:46 pm

create one row with two columns. Before that you need to collect all the record rows in a array(e.g. array_value) and then display it like this:
<tr><td>array_value[0]</td><td>array_value[5]</td></tr>
<tr><td>array_value[1]</td><td>array_value[6]</td></tr>
<tr><td>array_value[2]</td><td>array_value[7]</td></tr>
<tr><td>array_value[3]</td><td>array_value[8]</td></tr>
<tr><td>array_value[4]</td><td>array_value[9]</td></tr>


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 3 guests