Foreach within while passing (keys?) not values

I’m working on a calendar that has many events (with details) for each day. I want to click on the day and get each event, and its details, one after another. I think I’m getting close… but I’m getting numbers and letters back, not the values I put in the table. I’m really stumped… any help at all is greatly appreciated! THANKS!

//assign current member from session
$current_member = addslashes($_SESSION[‘SESS_MEMBER_ID’]);

// define table
$db_table = $TBL_PR . “events”;

// define SQL $query
$query = “SELECT member_id,event_id,event_time,event_title,event_desc,event_status FROM $db_table WHERE member_id=‘$current_member’ AND event_day=‘$day’ AND event_month=‘$month’ AND event_year=‘$year’ ORDER BY event_time”;

// run query & assign results to $events_array
$events_array = mysql_query ($query);

// move $events_array into $details_array
while ($details_array = mysql_fetch_array($events_array))
{
// print_r($details_array . “<p>”;
foreach ($details_array as $details)
{
$event_id = $details[‘event_id’];
echo $event_id . ‘<br>’;
$event_status = $details[‘event_status’];
echo $event_status . ‘<br>’;
$event_desc = $details[‘event_desc’];
echo $event_desc . ‘<br>’;
}
}

This may be what you are looking for.


while ($details_array = mysql_fetch_assoc($events_array))
{
  //	 print_r($details_array . "<p>";
  foreach ($details_array as $details => $val)
  {
    if($details == "event_id") $event_id = $val;
    echo $event_id . '<br>'; 
    if($details == "event_status") $event_status = $val;
    echo $event_status . '<br>';
    if($details == "event_desc") $event_desc = $val;
    echo $event_desc . '<br>';
  }
}

EDIT: Your problem came from the following places:
Additional foreach statement as what Shrapnel below explained.
Also, your understanding of foreach iteration seems to be flawed.
foreach takes the form of foreach($array as $value) or foreach($array as $key => $value), your method of using foreach seemed like you supposed foreach($array as $key), which is incorrect.

kefeso, I can’t say this code any better. It is quite unusual and senseless.

m300zx you have to understand what are you doing and what do all these variables mean.

$events_array is not an array. But resource.
you’re not moving anything with $details_array = mysql_fetch_array($events_array)
but fetching row after row.
$details_array is already array you need. another foreach is unnecessary.

just get rid of foreach ($details_array as $details) loop and use $details_array instead of $details

Yea lol I agree with you, it’s extremely redundant. I only did it like this to show him where he went wrong using his current method. Was gonna expect a reply from him then explain the root of the problem and show better implementations. I guess I should just explain where he went wrong fundamentally at first.

Using Shrapnel’s comments above, this implementation will suit you better:


while ($details_array = mysql_fetch_assoc($events_array))
{
    echo $details_array["event_id"] . '<br>'; 
    echo $details_array["event_status"] . '<br>'; 
    echo $details_array["event_desc"] . '<br>'; 
} 

I’m not seeing the purpose of foreach here?


while ($row = mysql_fetch_assoc($events_array)) {
    echo $row['event_id'];
    echo $row['event_desc'];
    //etc...
}

edit-im too slow.

:wink:

I’m sure I’m in need of an education - the following was my reasoning:

// move $events_array into $details_array
while ($details_array = mysql_fetch_array($events_array))

$details_array returns just one event (and its details) of the day, one at a time - hence it’s within the WHILE.

foreach ($details_array as $details)

now I process the one event returned above - getting the values of the event details contained within.

very wrong huh?

I’m sure I’m in need of an education - the following was my reasoning:

Nice to hear. Someone would ask not for education but for the code.

Yes, it is possible to loop over $details_array. But
$details is already scalar, not array.
Only you can do is just

foreach ($details_array as $details)
{
echo $details.'<br>';
}

Edit:
Almost forgot that
Note that also you have to use either mysql_fetch_assoc or mysql_fetch_row instead of mysql_fetch_array
or your details would be doubled, because of mysql_fetch_array nature

$details_array returns just one event (and its details) of the day, one at a time - hence it’s within the WHILE.

As Shrapnel explained, $event_array isn’t really an array, it’s a mysql resource. You can think of the array as a local table, and remotes tables are stored on the mysql server. Your $event_array basically established a connection with the remote table and contains information on how to retrieve that table. BUT, your $event_array itself is not a local table yet.

By doing $details_array = mysql_fetch_array($event_array), you are actually fetching the remote table line by line into the local table $details_array. So your $details_array only contains one entry at a time.

By using mysql_fetch_array you are fetching an array with field keys in numerical sequence. If you want to fetch an array with field keys corresponding to the original table column name, use mysql_fetch_assoc.

now I process the one event returned above - getting the values of the event details contained within.

very wrong huh?

Yes you are right in saying that you are processing an array. But there is no point. Since by using mysql_fetch_assoc, you can essential write out your items of interest by calling the items directly with the key within the while loop. And the while loop will take you automatically to the next line of record from your mysql table after the current line.

EDIT: Just read Shrapnel’s comments above. Looked up mysql_fetch_array again, by default it automatically returns a doubling of columns with numerical index once and association index once too. So if you are calling by key names, using mysql_fetch_array would be fine, but there are additional resources that you don’t need.

MANY THANKS SHRAPNEL, having success with your suggestion and am off in the right direction now!

EXCELLENT explanation KEFESO! you guys just saved me a full day of hair pulling! I THANK YOU !

thanks again you guys. when I get all this digested & working I will post the code.

Good luck :wink:

the following is up and running. not nearly as difficult as I was trying to make it :-). many thanks again for the education to both you guys. one hour here is worth a day of reading & testing.

// define table
$db_table = $TBL_PR . “events”;

// define SQL $query
$query = “SELECT * FROM $db_table WHERE member_id=‘$current_member’ AND event_day=‘$day’ AND event_month=‘$month’ AND event_year=‘$year’ ORDER BY event_time”;

// run query, - this gathers what I want, but it’s still on the server
$events_remote = mysql_query ($query);

// move $events_remote into $events_array, my local array
// this while loop will jump to subsequent events automatically - SWEET!
while ($events_array = mysql_fetch_assoc($events_remote))
{
$event_title = $events_array[‘event_title’];
$event_desc = $events_array[‘event_desc’];
$date = date (“l, jS F Y”, mktime(0,0,0,$events_array[‘event_month’],$events_array[‘event_day’],$events_array[‘event_year’]));
$time_array = split(“:”, $events_array[‘event_time’]);
$time = date (“g:ia”, mktime($time_array[‘0’],$time_array[‘1’],0,$events_array[‘event_month’],$events_array[‘event_day’],$events_array[‘event_year’]));

?>
<!-- form to display event details –>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>

the following is up and running. not nearly as difficult as I was trying to make it :-). many thanks again for the education to both you guys. one hour here is worth a day of reading & testing.

// define table
$db_table = $TBL_PR . “events”;

// define SQL $query
$query = “SELECT * FROM $db_table WHERE member_id=‘$current_member’ AND event_day=‘$day’ AND event_month=‘$month’ AND event_year=‘$year’ ORDER BY event_time”;

// run query, - this gathers what I want, but it’s still on the server
$events_remote = mysql_query ($query);

// move $events_remote into $events_array, my local array
// this while loop will jump to subsequent events automatically - SWEET!
while ($events_array = mysql_fetch_assoc($events_remote))
{
$event_title = $events_array[‘event_title’];
$event_desc = $events_array[‘event_desc’];
$date = date (“l, jS F Y”, mktime(0,0,0,$events_array[‘event_month’],$events_array[‘event_day’],$events_array[‘event_year’]));
$time_array = split(“:”, $events_array[‘event_time’]);
$time = date (“g:ia”, mktime($time_array[‘0’],$time_array[‘1’],0,$events_array[‘event_month’],$events_array[‘event_day’],$events_array[‘event_year’]));

?>
<!-- form to display event details –>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>