Jump to content

Looping Problem


86Stang

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/178372-looping-problem/
Share on other sites

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>";
               }
}

 

Link to comment
https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942146
Share on other sites

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 />'; }

Link to comment
https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942160
Share on other sites

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'];
}

Link to comment
https://forums.phpfreaks.com/topic/178372-looping-problem/#findComment-942174
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.