The examples I'm generally finding on the web of how to do nested loops are confusing and extremely scarce. Here's what I'm trying to do...
I have an array named $data that I believe qualifies as a multidimensiona l array:
$data=array( ID, Title, Description, UpdateID, Update, UpdateDate)
This data is from a query of a MySQL database that linked 2 tables so in reality I have:
$data[0] = array(1, Job1, Clean up the room, 1, Waiting for sunshine, 2008-03-23)
$data[1] = array(1, Job1, Clean up the room, 2, Still waiting, 2008-03-24)
$data[2] = array(1, Job1, Clean up the room, 3, Looking better, 2008-03-25)
$data[3] = array(2, Job2, Wash the car, 1, Kid has the car, 2008-03-21)
$data[4] = array(2, Job2, Wash the car, 2, Car is home, 2008-03-24)
$data[5] = array(2, Job2, Wash the car, 3, Car washed, 2008-03-25)
So what I want to do is handle each of these data sets by doing a foreach on ID and then a foreach on each Update ID so that it ends up like:
Item ID: 1 Title: Clean the room
Update ID: 1 Update: Waiting for sunshine Updated: 2008-03-23
Update ID: 2 Update: Still waiting Updated: 2008-03-24
Update ID: 3 Update: Looking better Updated: 2008-03-25
Item ID: 2 Title: Wash the car
Update ID: 1 Update: Kid has the car Updated: 2008-03-21
Update ID: 2 Update: Car is home Updated: 2008-03-24
Update ID: 3 Update: Car washed Updated: 2008-03-25
Can someone please comment on this syntax?
$query = "SELECT ID, Title, Description, UpdateID, Update, UpdateDate FROM ...";
$result = mysql_query($qu ery) or die('Error, query failed');
While($data = mysql_fetch_ass oc($result))
{
foreach ($data as $ID=> $UpdateID){
echo 'Item ID:'.$ID.' Title: '.$Title;
foreach ($UpdateID as $UpdateID){
echo 'Update ID: '.$UpdateID.' Update: '.$Update.' Updated: '.$UpdateDate;
}
}
}
Any assistance is appreciated.
I have an array named $data that I believe qualifies as a multidimensiona l array:
$data=array( ID, Title, Description, UpdateID, Update, UpdateDate)
This data is from a query of a MySQL database that linked 2 tables so in reality I have:
$data[0] = array(1, Job1, Clean up the room, 1, Waiting for sunshine, 2008-03-23)
$data[1] = array(1, Job1, Clean up the room, 2, Still waiting, 2008-03-24)
$data[2] = array(1, Job1, Clean up the room, 3, Looking better, 2008-03-25)
$data[3] = array(2, Job2, Wash the car, 1, Kid has the car, 2008-03-21)
$data[4] = array(2, Job2, Wash the car, 2, Car is home, 2008-03-24)
$data[5] = array(2, Job2, Wash the car, 3, Car washed, 2008-03-25)
So what I want to do is handle each of these data sets by doing a foreach on ID and then a foreach on each Update ID so that it ends up like:
Item ID: 1 Title: Clean the room
Update ID: 1 Update: Waiting for sunshine Updated: 2008-03-23
Update ID: 2 Update: Still waiting Updated: 2008-03-24
Update ID: 3 Update: Looking better Updated: 2008-03-25
Item ID: 2 Title: Wash the car
Update ID: 1 Update: Kid has the car Updated: 2008-03-21
Update ID: 2 Update: Car is home Updated: 2008-03-24
Update ID: 3 Update: Car washed Updated: 2008-03-25
Can someone please comment on this syntax?
$query = "SELECT ID, Title, Description, UpdateID, Update, UpdateDate FROM ...";
$result = mysql_query($qu ery) or die('Error, query failed');
While($data = mysql_fetch_ass oc($result))
{
foreach ($data as $ID=> $UpdateID){
echo 'Item ID:'.$ID.' Title: '.$Title;
foreach ($UpdateID as $UpdateID){
echo 'Update ID: '.$UpdateID.' Update: '.$Update.' Updated: '.$UpdateDate;
}
}
}
Any assistance is appreciated.
Comment