galvin Posted November 17, 2010 Share Posted November 17, 2010 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>'), ); } Quote Link to comment https://forums.phpfreaks.com/topic/218906-how-to-do-a-while-loop-to-add-data-sets-into-an-array/ Share on other sites More sharing options...
trq Posted November 17, 2010 Share Posted November 17, 2010 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>' ) ); } Quote Link to comment https://forums.phpfreaks.com/topic/218906-how-to-do-a-while-loop-to-add-data-sets-into-an-array/#findComment-1135272 Share on other sites More sharing options...
Jocka Posted November 17, 2010 Share Posted November 17, 2010 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/218906-how-to-do-a-while-loop-to-add-data-sets-into-an-array/#findComment-1135276 Share on other sites More sharing options...
galvin Posted November 17, 2010 Author Share Posted November 17, 2010 As usual, Thorpe's code worked perfectly (even with my lack of clarity...sorry about that). Thanks so much! And thanks Jocka, just saw that you replied as well! Quote Link to comment https://forums.phpfreaks.com/topic/218906-how-to-do-a-while-loop-to-add-data-sets-into-an-array/#findComment-1135277 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.