leachus2002 Posted December 14, 2010 Share Posted December 14, 2010 Hi There, I have an SQL query that returns 10 rows, which I want to echo over 2 columns and 5 rows, however, I want rows 1-5 on the left hand column and rows 6-10 on the right hand side. Is there an easy way to do this? Normally I would do a fetch_array but, that would place the rows in order of, left, right, left, right - if that makes sense? Table is a standard table with 2 columns and 5 rows. Thanks Matt Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 14, 2010 Share Posted December 14, 2010 Not the most elegant; however, since you are only dealing with 10 records, perhaps something like this... <?PHP /* connect to db */ include('db.php'); /* create query */ $query = "SELECT * FROM table_name"; /* execute query */ $result = mysql_query($query); /* put the content into an array */ while($row=mysql_fetch_array($result)) { $cell_content[] = $row['some_field_name]; } /* start the table */ echo "<table>"; /* loop thru the array */ $i=0; while($i<10) { echo "<tr><td>" . $cell_content[$i] . "</td><td>" . $cell_content[$i+5] . "</td></tr>"; $i = $i +2; } /* close the table */ echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted December 14, 2010 Author Share Posted December 14, 2010 Thanks for that code. Looking good. However its only bringing back 8 rows and I have the following error: Notice: Undefined offset: 11 in ............. Notice: Undefined offset: 13 in ............. Any ideas? My guess is that its going above 10? Cheers Matt Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 14, 2010 Share Posted December 14, 2010 show your existing code Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted December 14, 2010 Author Share Posted December 14, 2010 echo " <table width='100%' cellspacing='0' cellpadding='2'><form name='sysstatus' action='index.php' method='post'>"; $i = 1; while($i<10){ echo "<tr><td width='150'>".$sysname[$i]."</td><td> </td><td width='30'> </td><td width='150'>".$sysname[$i+5]."</td><td> </td></tr>"; $i = $i +2; } echo "</form></table>"; Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 14, 2010 Share Posted December 14, 2010 initially set $i=0 NOT $i=1 Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted December 14, 2010 Author Share Posted December 14, 2010 Sorry, thats a typo on my part, but still not working Thanks Matt Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 14, 2010 Share Posted December 14, 2010 My error, should be... echo "<table>"; /* loop thru the array */ $i=0; while($i<5) { echo "<tr><td>" . $cell_content[$i] . "</td><td>" . $cell_content[$i+5] . "</td></tr>"; $i = $i +1; } /* close the table */ echo "</table>"; Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted December 14, 2010 Author Share Posted December 14, 2010 Awesome - thank you very much! 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.