86Stang Posted October 20, 2009 Share Posted October 20, 2009 I've got a client that has a database with about 200 events at any given time. I'm trying to loop through the dates based on a form and show the title of the event if the start date of the form matches the start date of the event. My code: // dates $start_date = "2009-10-24"; $end_date = "2009-11-20"; // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to access database!"); // build query $qry = "SELECT * FROM table ORDER BY start_date asc"; $result = mysql_query($qry) or die("Error during query!"); // loop through every day from $start_date to $end_date for ($d=strtotime($start_date);$d<=strtotime($end_date);$d+=86400) { echo date("Y-m-d", $d) . "<br>"; while ($row = mysql_fetch_array($result)) { //echo $row[event_id] . " - "; if (strtotime($row[start_date]) == $d) { echo $row[title] . "<br>"; } } echo "<br><br>"; } I get a list of dates from the start date to the end date but the only titles I see are for the first date even though other titles should be showing. I know this isn't optimal coding - (the record count will never go beyond a few hundred) - but I'm wondering why nothing beyond the first date is outputting. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/ Share on other sites More sharing options...
sasa Posted October 20, 2009 Share Posted October 20, 2009 insert mysql_data_seek($result,0); just before while loop Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-940616 Share on other sites More sharing options...
86Stang Posted October 20, 2009 Author Share Posted October 20, 2009 Nothing. What is that suppose to do? Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-940618 Share on other sites More sharing options...
sasa Posted October 20, 2009 Share Posted October 20, 2009 in while loop you go to the end of $result resources when you change $d yoou must to reset $resource to begining Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-940624 Share on other sites More sharing options...
86Stang Posted October 21, 2009 Author Share Posted October 21, 2009 Ahh, got it. It didn't work though. Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-941225 Share on other sites More sharing options...
86Stang Posted October 22, 2009 Author Share Posted October 22, 2009 Any further thought on this from anyone? Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942113 Share on other sites More sharing options...
plautzer Posted October 22, 2009 Share Posted October 22, 2009 HI, ur missing the ' try strtotime($row['start_date'] instead of strtotime($row[start_date] same for the title, event_id Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942126 Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 HI, ur missing the ' try strtotime($row['start_date'] instead of strtotime($row[start_date] same for the title, event_id doesn't make a difference in this case. Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942132 Share on other sites More sharing options...
86Stang Posted October 22, 2009 Author Share Posted October 22, 2009 Nope, didn't help. Thanks for the suggestion though! Common sense tells me that the mysql_data_seek would've helped but it didn't. Grrr!!! Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942141 Share on other sites More sharing options...
plautzer Posted October 22, 2009 Share Posted October 22, 2009 HI, i would do the same like sasa.. u could also try throwing the result in an array first and than loop thru the array. this way u see if it would work. while ($row = mysql_fetch_array($result)) { $arr[] = array('start_date' => $row[]... } foreach ($arr as $array){ //echo $array['event_id'] . " - "; if (strtotime($array['start_date']) == $d) { echo $array['title'] . "<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942146 Share on other sites More sharing options...
mrMarcus Posted October 22, 2009 Share Posted October 22, 2009 use MySQL to do the check: // build query $qry = "SELECT * FROM table WHERE start_date>='".$start_date."' AND start_date<='".$end_date."' ORDER BY start_date asc"; $result = mysql_query($qry) or die("Error during query!"); // loop through every day from $start_date to $end_date while ($row = mysql_fetch_array($result)) { echo $row['title'] . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942160 Share on other sites More sharing options...
cags Posted October 22, 2009 Share Posted October 22, 2009 Personally I'd probably use a differen't approach altogether. Assuming you are attempting what I think, then I'd use something like this... $qry = "SELECT * FROM table ORDER BY start_date asc"; $result = mysql_query($qry) or die("Error during query!"); $cur_date = ""; while($row = mysql_fetch_assoc($result)) { if($row['start_date'] != $curdate) { echo $row['start_date']; $cur_date = $row['start_date']; } echo $row['title']; } Quote Link to comment https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942174 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.