Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Loop 1

Status
Not open for further replies.

overyde

Programmer
May 27, 2003
226
ZA
Hi,
For some reason this has me stumped. Basically I'm calling data from a db and placing it in an array. How do I get the array to write a <tr> for every different "eventid" not evry entry? i.e. Basically the data below should have 2 rows in the table with <td> cells for each of the start dates and jobnames?
Code:
____________________________________________
12345 |2009-03-05       |2009-03-06         |
      |Sample Copywrite |Sample Copywrite   |
--------------------------------------------
56789 |2009-04-02       |                   |
      |Sample Copywrite2|                   |
--------------------------------------------
Code:
$yearevent[0] = array("startdate"=>"2009-03-05",
						   "jobname"=>"Sample Copywrite",
						   "eventid"=>"12345");	

$yearevent[1] = array("startdate"=>"2009-03-06",
						   "jobname"=>"Sample Copywrite",
						   "eventid"=>"12345");

$yearevent[2] = array("startdate"=>"2009-04-02",
						   "jobname"=>"Sample Copywrite2",
						   "eventid"=>"56789");

Reality is built on a foundation of dreams.
 
Hi

Why not use a structure that matches the display format you want to produce ?
Code:
$yearevent=array(
  "12345"=>array(
    array("startdate"=>"2009-03-05","jobname"=>"Sample Copywrite"),
    array("startdate"=>"2009-03-06","jobname"=>"Sample Copywrite")
  ),
  "56789"=>array(
    array("startdate"=>"2009-04-02","jobname"=>"Sample Copywrite2")
  )
);

Feherke.
 
The data comes from a dbase. Not really sure how I would set it up that whilst retrieving the data if the event id is the same then I would just add to the existing key/eventid and if it were different then set up a new key.

Reality is built on a foundation of dreams.
 
If your eventids are sorted, then use a temp variable to store the last eventid and compare it to the one you currently have. If its the same, new <td>, else new <tr>.

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
Hi

Ok, then let us keep your original data structure.
Code:
$last='';
foreach ($yearevent as $one) {
  if ($last!=$one['eventid']) {
    if ($last) echo "</tr>\n";
    echo "<tr>\n<td>$one[eventid]</td>\n";
    $last=$one['eventid'];
  }
  echo "<td>$one[startdate]<br>$one[jobname]</td>\n";
}
if ($last) echo "</tr>\n";
Note that
[ul]
[li]you not mentioned anything about the order, so I assumed same eventids are consecutive[/li]
[li]you not mentioned anything about the number of events with the same eventid, so I not handled that case[/li]
[/ul]

Feherke.
 
Just a quickie...
you have:
Code:
if ($last) echo "</tr>\n";
what does if ($last) actually mean. Does that test if $last has been set. I've never seen if used by itself without the {}.

Reality is built on a foundation of dreams.
 
Hi

overyde said:
what does if ($last) actually mean
Means that $last is set and its value is not [tt]false[/tt], [tt]null[/tt], 0, empty string or empty array.
overyde said:
I've never seen if used by itself without the {}.
Brackets ( {} ) are optional if the control structure applies to only one statement.

Feherke.
 
Feherke...thanks mate!
Legendary. Really helped me out of a pickle there ;)

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top