Jump to content

[SOLVED] Inserting empty rows into multi-dimensional array


Recommended Posts

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

 

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/>';

}

}

 

  • 1 month later...

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.

 

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.