I don't think there's an easy way of asking this question. However, here goes...
I am basically using some code where I am querying data at the start of the document and then outputting data in a different loop at the bottom, and I've got to a point where I believe I need to start having arrays within arrays and it's all getting very tricky!
At the start I am getting the post_ID, eventStart and eventEnd details from a query. Then, at the bottom I am outputting the ID and using that in a different query, however I really want to output the eventStart and eventEnd details too. How would I build this into my array?
Currently, if I echo the $data array at the bottom I get this:
Array
(
[06/09/2011] => Array
(
[0] => 13
[1] => 15
)
[06/10/2011] => Array
(
[0] => 15
)
[06/11/2011] =>
[06/12/2011] =>
[06/13/2011] => Array
(
[0] => 6
)
[06/14/2011] =>
[06/15/2011] =>
)
So, what I think I need is to have the eventStart and event End data nestled within the ID array, which is the array with the 6, 13 and 15 numbers.
How the hell do I do this? I was working on this code with someone else and am probably now in over my head. Any help would be massively appreciated!!
Here's my code:
<?php
$sql = "SELECT post_id,
DATE_FORMAT(start,'%m/%d/%Y') AS eventStart,
DATE_FORMAT(end,'%m/%d/%Y') AS eventEnd,
DATE_FORMAT(CURDATE(),'%m/%d/%Y') AS today,
DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 7 DAY),'%m/%d/%Y') AS endWeek
FROM $ec3->schedule s
JOIN $wpdb->posts p ON p.ID = s.post_id
WHERE post_status = 'publish' ";
$clause = array();
$htmlOutput = '';
for( $i = 0; $i < 7; $i++ ) {
$clause[] = "DATE_ADD(CURDATE(), INTERVAL $i DAY) BETWEEN DATE(start) AND DATE(end)";
}
$sql .= "AND (" . implode(' OR ', $clause) . ")";
$sql .= ' ORDER BY start DESC';
$result = mysql_query($sql) or trigger_error($sql . ' has failed. <br />' . mysql_error()); //pull data from database.
$dates = array(); //define dates array.
for($i = 0; $i < 7; $i++) {
$dates[] = date('m/d/Y', strtotime("+$i days")); //fill dates array with every date from now until 7 days from now.
}
foreach($dates as $days) { //go through dates array.
$data[$days] = NULL; //fill data array with keys for every date from now until 7 days.
}
if(mysql_num_rows($result) > 0) {
while($r = mysql_fetch_assoc($result)) { //while data exists in the database result resource.
if(strtotime($r['eventStart']) < strtotime($r['today'])) { //if the event started before today.
$r['eventStart'] = $r['today']; //make it's startdate today.
}
if(strtotime($r['eventEnd']) > strtotime($r['endWeek'])) { //if the event ends after this week is over.
$r['eventEnd'] = $r['endWeek']; //make the end of the week, it's end.
}
$started = false; //define a false variable.
foreach($data as $key => $value) { //loop through our data array.
if($key == $r['eventStart'] && $started == false) { //if the event starts at the present key, put it in the array.
$data[$key][] = $r['post_id'];
$started = ($key == $r['eventEnd']) ? false : true; //set started to true.
}
elseif($key == $r['eventEnd']) { //if the eventEnd is the present key, put it in the array,
$data[$key][] = $r['post_id'];
$started = false; //set started to false;
}
elseif($started == true) { //if started is true, then put the event in the array.
$data[$key][] = $r['post_id'];
}
}
}
foreach($data as $date => $v) { //loop through the data array.
echo date('D', strtotime($date)) . '<br />'; //echo the $date, followed by a line.
echo date('d', strtotime($date)) . '<br />---------<br />'; //echo the $date, followed by a line.
foreach($v as $event) {
$event_post = get_post($event);
$title = $event_post->post_title;
echo "Date of event:<br />";
echo $title . "<br />"; //each event is then echo'd to the page, followed by a break rule.
}
echo '<br />'; //after all of the events on this day, print another break rule, double spacing before the next date.
}
}
else {
echo 'No rows to show!';
}
?>
Modify message
Report to moderator 89.242.32.151