Jump to content

2 arrays in foreach loop not looping


sford999

Recommended Posts

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 Edited by sford999
Link to comment
Share on other sites

$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
Share on other sites

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
Share on other sites

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