sford999 Posted May 8, 2013 Share Posted May 8, 2013 I have the code below which is meant to loop through each array showing the respective data from each array, but it is only displaying date - name And not all 4 items in the array The info in the database is stored as date_redeemed column = 1368140499|1368140500|1368140501|1368140502 user_redeemed column = name|name2|name3|name4 $result = $db->getRow("SELECT * FROM table_codes WHERE code= '$code' LIMIT 1"); $dates = explode('|', $result['date_redeemed']); $users = explode('|', $result['user_redeemed']); foreach(array_combine($dates, $users) as $date => $user) { $getUser = $db->getRow("SELECT * FROM users WHERE id = '".$user."'"); echo date('d/m/Y', $date).' - '.$user; } Doing print_r() on both the $dates & $users variables show all items in the arrays Array ( [0] => 1368140499 [1] => 1368140500 [2] => 1368140501 [3] => 1368140502 ) Array ( [0] => name [1] => name2 [2] => name3 [3] => name4 ) Any insight/help is appreciated Link to comment https://forums.phpfreaks.com/topic/277810-2-arrays-in-foreach-loop-not-looping/ Share on other sites More sharing options...
akphidelt2007 Posted May 8, 2013 Share Posted May 8, 2013 The code works for me when substituting the strings in to $dates and $users. Where is $code coming from and are you sure those are actually the arrays you are getting? Try it with out $getUser... and see if it works. Link to comment https://forums.phpfreaks.com/topic/277810-2-arrays-in-foreach-loop-not-looping/#findComment-1429166 Share on other sites More sharing options...
sford999 Posted May 8, 2013 Author Share Posted May 8, 2013 $code is passed as a variable in the URL using $_GET The information on $dates & $users are stored in a single row in the database. INSERT INTO `table_codes` (`id`, `code`, `date_redeemed`, `user_redeemed`) VALUES (7, 'BBB', '1368140499|1368140500|1368140501|1368140502', 'name|name2|name3|name4'); Link to comment https://forums.phpfreaks.com/topic/277810-2-arrays-in-foreach-loop-not-looping/#findComment-1429171 Share on other sites More sharing options...
akphidelt2007 Posted May 8, 2013 Share Posted May 8, 2013 Well, this works perfectly fine... $dates = explode('|', "1368140499|1368140500|1368140501|1368140502"); $users = explode('|', "name|name2|name3|name4"); foreach(array_combine($dates, $users) as $date => $user) { echo date('d/m/Y', $date).' - '.$user.'<br>'; } //produces 09/05/2013 - name 09/05/2013 - name2 09/05/2013 - name3 09/05/2013 - name4 So either your query is not getting the desired results you want or the $getUser query is creating some kind of error halting the loop to continue. Link to comment https://forums.phpfreaks.com/topic/277810-2-arrays-in-foreach-loop-not-looping/#findComment-1429173 Share on other sites More sharing options...
Barand Posted May 8, 2013 Share Posted May 8, 2013 I have the code below which is meant to loop through each array showing the respective data from each array, but it is only displaying date - name And not all 4 items in the array The info in the database is stored as date_redeemed column = 1368140499|1368140500|1368140501|1368140502 user_redeemed column = name|name2|name3|name4 I would really recommend that you normalize that table design. Something like +------+---------+---------------------+----------+ | id | code | date_redeemed | user_id | +------+---------+---------------------+----------+ | 1 | 123 | 2013-05-10 00:01:39 | 101 | | 2 | 123 | 2013-05-10 00:01:40 | 102 | | 3 | 123 | 2013-05-10 00:01:41 | 508 | | 4 | 123 | 2013-05-10 00:01:42 | 298 | +------+---------+---------------------+----------+ Link to comment https://forums.phpfreaks.com/topic/277810-2-arrays-in-foreach-loop-not-looping/#findComment-1429176 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.