Jump to content

How to do a while loop to add data sets into an array?


galvin

Recommended Posts

Ill spare you the full code because I don't think you'll need it to answer this, but I have code where an array called $days1 is being created.  It's working fine as is to bring back 1 set of data, but I want to keep adding to the array for however many sets of data there are in the "while" statement.

 

So my question is how do I write this so that ALL sets of data brought back by MySQL (whether it's 1 row or 50) keep getting added to the array, rather than just having one set of info in my array?

 

I feel like I need to put "$days1 = array(" before the WHILE statement and then put the closing ")" after all the loops are done, but that didn't work.

 

Can anyone help me out here? (assuming it makes sense :) )...

 

	while ($hunches = mysql_fetch_array($gethunches)) {			
		 $days1 = array( 
			$hunches['day']=>array(NULL,NULL,'<span class="red">' . $hunches['firstname'] . " " .	$hunches['lastname'] . "-" .	$hunches['dob'] . "--" . $hunches['sex'] . '</span>'), 
			);
	}

It's pretty difficult to tell exactly how you want this array formatted, but heres my best guess.

 

while ($hunches = mysql_fetch_array($gethunches)) {         
    $days1[] = array(
        $hunches['day'] => array(
            NULL,NULL,'<span class="red">' . $hunches['firstname'] . " " .   $hunches['lastname'] . "-" .   $hunches['dob'] . "--" . $hunches['sex'] . '</span>'
        )
    );
}

hm.. well there are a couple of ways you could do it I believe.

// TRY SOME OF THESE STEPS

// CHANGE WHILE STATEMENT TO THIS
   while ($hunches = mysql_fetch_array($gethunches)) {         
          $days1[] = $hunches['day']=>array(NULL,NULL,'<span class="red">' . $hunches['firstname'] . " " .   $hunches['lastname'] . "-" .   $hunches['dob'] . "--" . $hunches['sex'] . '</span>'
            );
      }

// OR ADD NUMBERS YOURSELF?
   $i = 0;
   while ($hunches = mysql_fetch_array($gethunches)) {         
          $days1[$i] = $hunches['day']=>array(NULL,NULL,'<span class="red">' . $hunches['firstname'] . " " .   $hunches['lastname'] . "-" .   $hunches['dob'] . "--" . $hunches['sex'] . '</span>'
            );
          $i++;
      }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.