slaterino Posted June 8, 2011 Share Posted June 8, 2011 I am working with some code, which I've pasted below, and have a query about. I am outputting some data in the foreach loop at the bottom on my site. Now, my question is how can I store information from the start to echo out at the bottom. What I want is three pieces of information. I want the start date (eventStart) for each event, end date (eventEnd) for each event and also a simple statement which would say 'true' if the day of start date was the same as the end date or 'false' if they are different. What's the best way of using this data later on. Is it using an array? Sorry for being a bit thick but I'm just beginning to get my head around this whole array thing! Thanks for any future help, and 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(); 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. } // echo '<pre>' . print_r($r,true) . '</pre>'; //de-bugging. $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"; 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!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/ Share on other sites More sharing options...
Psycho Posted June 8, 2011 Share Posted June 8, 2011 I'm not going to analyze all of your code and try to determine the best approach - there's just too much. But, it looks like you are simply running a query to get some data that you then want to output later. Typically if I only need to process the data once I will generate the output when I do the while() loop on the database records. I will populate those results into a string variable. Then, I will use that variable to put the output into the appropriate location on the page. I don not put the results into an array - that only increases the memory needed to process the script. I will only put the results into an array if there are some array operations I need to perform on the result set - for example, if I need to split the dataset based upon a know list of IDs. Here is a rough example based on your explanation <?php $query = "SELECT eventStart, eventEnd FROM table"; $result = mysql_query($query); //Generate output $htmlOutput = ''; while($row = mysql_fetch_assoc($result)) { $sameDate = ($row['eventStart'] == $row['eventEnd']) ? 'True' : 'False'; $htmlOutput .= "<tr>\n"; $htmlOutput .= "<td>{$row['eventStart']}</td>\n"; $htmlOutput .= "<td>{$row['eventEnd']}</td>\n"; $htmlOutput .= "<td>{$sameDate}</td>\n"; $htmlOutput .= "</tr>\n"; } //more php processing code // . . . // . . . //PHP processing is complete, show HTML ?> <html> <body> Here are the results: <br> <table> <tr> <th>Start Date</th> <th>End Date</th> <th>Same Date?</th> </tr> <?php echo htmlOutput; ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227154 Share on other sites More sharing options...
spiderwell Posted June 8, 2011 Share Posted June 8, 2011 an array is definitely the right way to store this sort of information if you require to use it again later in the script, more likely nested arrays, just to make it even more fun for you! Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227155 Share on other sites More sharing options...
slaterino Posted June 8, 2011 Author Share Posted June 8, 2011 Okay, cheers for the suggestions. What's the general consensus on this issue? Is it better to use an array or htmloutput? The number one priority is for the site to be fast, so would that mean using html output? Best, Russ Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227174 Share on other sites More sharing options...
spiderwell Posted June 8, 2011 Share Posted June 8, 2011 personally i pass it all into a long string as shown by mjdamato, and would only pass it to another array if I needed to do other things with that data again later in the page/script. I have rarely done this, only really in MVC where the display is seperated from the database functions (MVC is Model View Controller, its an method of application design, applicable to any programming language, in case you didn't know) Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227189 Share on other sites More sharing options...
xyph Posted June 8, 2011 Share Posted June 8, 2011 I would almost NEVER pass it as a string like mjdamato and spiderwell would do it. An array adds a little overhead, and including the markup with the string kinda removes most differences in memory consumption. Each array key has an overhead of around 60 bytes. So if you have 60 characters of markup to add (not hard when you include styles) you literally lose any memory saving. I like keeping my data and my markup separate until the absolute end, where I send my data into a template parsing class. If you want to change the way your data is outputted, you only have to modify a single class. If you include the markup with your data, you have to find where this data is generated and change it there. This becomes a big issue when working with larger scripts. Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227217 Share on other sites More sharing options...
teynon Posted June 9, 2011 Share Posted June 9, 2011 My internet is retarded... Mod please delete this post. Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227240 Share on other sites More sharing options...
slaterino Posted June 9, 2011 Author Share Posted June 9, 2011 Okay, so using nested arrays is fine. Just one question, how the hell do I do that? I currently have a $data array which contains this information for my test data: 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] => ) Essentially for each one of the ID numbers (13,15,6) I would need an eventStart date and an eventEnd date. So, this would mean creating an array within that array - is that correct? I've pasted my current code again below. Any help on this would be massively appreciated. I'm just starting to understand arrays so to jump up to nested arrays is proving quite tricky!!! <?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!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227370 Share on other sites More sharing options...
xyph Posted June 9, 2011 Share Posted June 9, 2011 Wow, that's a lot of code to go through. Don't have time to look at it all, but I store my arrays like this. $array[ $result['id'] ] = array( 'eventStart' => $result[ 'eventStart' ], 'eventEnd' => $result[ 'eventEnd' ], 'today' => $result[ 'today' ], 'endWeek' => $result[ 'endWeek' ] ); PHP is pretty bad at handling huge amounts of results in arrays though... If you're dealing with extremely large datasets and your server can't handle it, you may want to consider pagination or limiting your returns by other means. Quote Link to comment https://forums.phpfreaks.com/topic/238823-storing-data-in-an-array-is-this-the-best-way/#findComment-1227533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.