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>'), 
			);
	}

Link to comment
Share on other sites

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>'
        )
    );
}

Link to comment
Share on other sites

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++;
      }

Link to comment
Share on other sites

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.