thekraut Posted September 11, 2008 Share Posted September 11, 2008 Hi - I'm not a total newbie, but can't get my head around to solve this particular problem From a mysql database query I get a multi-dimensional array as result Example No Name Time Location 2 Minnie 720 Hol Inn 5 Micky 735 Central 3 Donald 745 Hilton 2 Goofy 755 CQ That view is fine on screen, but I need to print this out and on the print out it needs to have additional empty rows in between the results based on the no. field Example 2 Minnie 720 Hol Inn (1 extra empty row) 5 Micky 735 Central (4 extra empty rows) 3 Donald 745 Hilton (2 extra empty rows) 2 Goofy 755 CQ (1 extra empty row) I've been mulling over this problem for some time now and tried array_splice but can't get the required result. I assume I need to unset the keys and reset them to (key+1) for the 2nd non-empty row (key+1+4) for 3rd non-empty row and (key+1+4+2) for 4th non-empty row I seem to have a bit of mental block here and can't figure out how to go about it. Any help pointing me in the right direction would be appreciated. I'm using PHP 5.2.5 MySQL 4.1.13 Quote Link to comment https://forums.phpfreaks.com/topic/123706-solved-inserting-empty-rows-into-multi-dimensional-array/ Share on other sites More sharing options...
discomatt Posted September 11, 2008 Share Posted September 11, 2008 Simply use a for loop. for( $i=1; $i<$row['no']; $i++ ) { echo '*** blank row ***'; } Quote Link to comment https://forums.phpfreaks.com/topic/123706-solved-inserting-empty-rows-into-multi-dimensional-array/#findComment-638767 Share on other sites More sharing options...
thekraut Posted September 12, 2008 Author Share Posted September 12, 2008 Thanks a lot - it works and gives me the required result, but it ends in an infinite loop. Is there any way to stop that happening? Below is the code I'm using. $sql = 'SELECT b.date, b.tourid, b.paxname,b.firstname, b.paxnumber, b.pickuptime, b.pickuppoint FROM b.... b where bla bla bla ORDER BY bla bla bla ; $result=mysql_query($sql); $num=14; if (!$result) { die('Could not query:' . mysql_error()); } for ($i=0; $i <$num; $i++) { $row=mysql_fetch_object($result); print(" <table> <tr> <td class=\"paxno\">".$row->paxnumber." </td> <td class=\"surname\">".$row->paxname." </td> <td class=\"firstname\">".$row->firstname." </td> <td class=\"pickuptime\">".$row->pickuptime." </td> <td class=\"pickuphotel\">".$row->pickuppoint."</td></tr></table> "); for( $i=1; $i<$row->paxnumber; $i++ ) { echo 'achtung<br/>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/123706-solved-inserting-empty-rows-into-multi-dimensional-array/#findComment-639773 Share on other sites More sharing options...
thekraut Posted October 23, 2008 Author Share Posted October 23, 2008 bump still trying to stop this infinite loop Quote Link to comment https://forums.phpfreaks.com/topic/123706-solved-inserting-empty-rows-into-multi-dimensional-array/#findComment-672658 Share on other sites More sharing options...
.josh Posted October 23, 2008 Share Posted October 23, 2008 Okay first off, you should not be basing your outer loop off a solid number like 14, because if your query returns less than 14 you will end up getting errors from your fetch_object. Whatever limit you want to set on the results should be in the query string itself, and you should put your fetch_object inside the outer loop like so: while($row=mysql_fetch_object($result)) { // rest of code here } Also, you have your table tag inside the loop. That's sure to cause you some aesthetic troubles. As far as your actual problem: Your infinite loop is being caused by using the same variable $i for your inner loop as you are in your outer loop. Quote Link to comment https://forums.phpfreaks.com/topic/123706-solved-inserting-empty-rows-into-multi-dimensional-array/#findComment-672692 Share on other sites More sharing options...
thekraut Posted October 29, 2008 Author Share Posted October 29, 2008 First of all - Thank you very much Crayon Violent and discomatt Your advice did finally solve my problem. By renaming the inner loop to $ii instead of $i I got the required results. In regards to having the table tag inside the loop - that gets me all result rows displayed. When I put it outside the loop I only get 1 row displayed. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/123706-solved-inserting-empty-rows-into-multi-dimensional-array/#findComment-677904 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.