I'm fairly new to PHP (have been coding with Perl for about 5 years). I'm trying to create an event calendar that is continuosly updatable.
So far I have created the database (MySQL) and the data input form. This part works.
Now I'm creating the PHP script that read data from the database for a certain week and show the complete events list.
My query get's the following data:
$EventID
$EventName
$EventDate
$EventHour
$EventComments
$EventPlaceName
$EventPlaceAddress
$EventPlaceCity
$EventPlaceStateorProvince $EventPlaceCountry
$EventPlaceWebSite
$EventTicketPrice
$EventTickePriceCurrency
$EventArtist1...n
$EventArtistisMainArtist
these records are ordered by EventDate, EventPlaceCity and EventID. I want the resulting HTML table to show the events ordered by date, and then by city, and then by ID in order to group rows belonging to the same event.
The Artist may contain several artists for the same EventID, so that I get several rows with the same data bar the Artist and ArtistisMain (this means that the artist is the main artist and comes in first place) after the equijoin Event table->Artist table.
In order to translate into HTML I thought the best would to create a multidimensional array, that goes like this:
$data =
array ("$EventDate1" =>
array ("$EventID" =>
array ("name" => $EventName
,"date" => $EventDate
,"hour" => $EventHour
,"comments" => $EventComments
,"place" => $EventPlaceName
,"address" => $EventPlaceAddress
,"city" => $EventPlaceCity
,"state" => $EventPlaceState
,"country" => $EventPlaceCountry
,"www" => $EventPlaceWebSite
,"price" => $EventTicketPrice
,"currency" => $EventCurrency
,"artists" => array (
"main" => array ($artist1,...)
,"sec" => array ($artist1,...)
)
)
)
, "$EventDate2" => the same as above
, ....
)
I haven't any problems populating the 1st and 2nd nested arrays, but when it comes to the "artists" array doesn't work.
Because of the rows with duplicate values I check if a value already exists with:
if (!in_array ("$EventID", $data["$EventDate"])
before creating the new nested array.
I also check whether the artists array already exists:
if (!array_key_exists("artists", $data["$EventDate"]["$EventID"]))
If not the I create the nested arrays "main" and "sec" for the key "artists" and populate them:
For instance if IsMain is "yes" for an artist then:
$data["$EventDate"]["$EventID"]["artists"]["main"][] = $EventArtist
Even though I keep getting records containing just one artist, precisely the last one returned by the query. If an event contains three artists (= three rows) starting with the main artist, I get a records with an empty "main" array and the "sec" array containing just one artist, as if the test above had failed and "recreated" the arrays "main" and "sec" for every loop of the recordset.
Any clues why this doesn't work as expected? I'd really appreciate your input.
Miguel