Jump to content

[SOLVED] Displaying dates with multiple rows grouped together


SMPoint

Recommended Posts

Hello all,

 

Kind of stumped and am looking for some guidance. I have data that I grab "mysql_fetch_array" and it displays the results as expected. One by one, the "job" displays with all of the data for that day. The problem is I would like to group the data by day on the page so if I run a query of more than 1 day, I can take the multiple day results and display them properly. I am unfamiliar with displaying grouped data in a single page. Can someone point me in the right dir? This might help....

 

$sql = "SELECT * from XXX where start_search BETWEEN '$nowq_date' AND '$nowq5_date' ORDER BY start_search ASC";
$result = mysql_query($sql,$connection) or die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_array($result)) {
$start = $row["start"];
$end = $row["end"];
$date = $row["date"];

echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='4'><TR>
      <TD WIDTH='25%' NOWRAP>$date</TD>
      <TD WIDTH='20%'>$start</TD>
      <TD WIDTH='15%'>$end</TD>
      </TR></TABLE>";
}

 

This prints ....

 

02/12/2009 start1 end1

02/12/2009 start2 end2

02/12/2009 start3 end3

02/13/2009 start1 end1

02/13/2009 start2 end2

02/14/2009 start3 end3

 

But I want it to print ...

 

02/12/2009 start1 end1

02/12/2009 start2 end2

02/12/2009 start3 end3

SPACE HERE

02/13/2009 start1 end1

02/13/2009 start2 end2

SPACE HERE

02/14/2009 start3 end3

 

Link to comment
Share on other sites

$sql = "SELECT * from XXX where start_search BETWEEN '$nowq_date' AND '$nowq5_date' ORDER BY start_search ASC";
$result = mysql_query($sql,$connection) or die("Invalid query: " . mysql_error());
$previous_date = 0;
while ($row = mysql_fetch_array($result)) {
$start = $row["start"];
$end = $row["end"];
$date = $row["date"];
if($previous_date === 0){
      $previous_date = $date;
}else if($date != $previous_date){
      $previous_date = $date;
      echo "<br>"; // extra space
}


echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='4'><TR>
      <TD WIDTH='25%' NOWRAP>$date</TD>
      <TD WIDTH='20%'>$start</TD>
      <TD WIDTH='15%'>$end</TD>
      </TR></TABLE>";
}

 

Link to comment
Share on other sites

Well, it worked out great. I am totally confused, but only because I did not know what "===" was. I thought it was a typo.

 

I was unaware of indentity array operators. I have used all of the others thousands of times, but never seen those. Were those always around in older versions of PHP? Anyways, thanks a million!!

Link to comment
Share on other sites

Ok, well I thought I had it, but I do not. I was taking the premise of the code that mrdamien gave me and thought I could use it the same way. Instead of using it for spacing...<BR><BR>...I though I would throw in an include file that displays a top of a box and then I wanted to throw in a footer that would display the bottom of the box...wrap the results by day using some graphics. Here is the modified code...

 

$sql = "SELECT * from XXX where start_search BETWEEN '$nowq_date' AND '$nowq5_date' ORDER BY start_search ASC";
$result = mysql_query($sql,$connection) or die("Invalid query: " . mysql_error());
$previous_date = 0;
while ($row = mysql_fetch_array($result)) {
$start = $row["start"];
$end = $row["end"];
$date = $row["date"];
if($previous_date === 0){
      $previous_date = $date;
}else if($date != $previous_date){
      $previous_date = $date;
      include ("header.php");
}


echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='4'><TR>
      <TD WIDTH='25%' NOWRAP>$date</TD>
      <TD WIDTH='20%'>$start</TD>
      <TD WIDTH='15%'>$end</TD>
      </TR></TABLE>";
}

 

My question is, where do I put the code to properly wrap the footer data by date using an include file?

Link to comment
Share on other sites

while ($row = mysql_fetch_array($result)) {
$start = $row["start"];
$end = $row["end"];
$date = $row["date"];
if($previous_date === 0){
      $previous_date = $date;
}else if($date != $previous_date){
      include ("header.php");
}

echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='4'><TR>
      <TD WIDTH='25%' NOWRAP>$date</TD>
      <TD WIDTH='20%'>$start</TD>
      <TD WIDTH='15%'>$end</TD>
      </TR></TABLE>";
if($date != $previous_date){
      $previous_date = $date;
      include ("footer.php");
}

}

I think this is what you want...

Link to comment
Share on other sites

It did not work so I just did the include in the same section as your modified code...

 

if($date != $previous_date){
      $previous_date = $date;
      include ("footer.php");
}

 

changed to

 

      include ("footer.php");

 

and it includes the footer, but only the first result of each day is wrapped, not all of the data of each day. Does that help?

Link to comment
Share on other sites

$sql = "SELECT * from XXX where start_search BETWEEN '$nowq_date' AND '$nowq5_date' ORDER BY start_search ASC";
$result = mysql_query($sql,$connection) or die("Invalid query: " . mysql_error());

if (mysql_num_rows ($result) == 0){ echo "blah"; }

$group_date = 0;

while ($row = mysql_fetch_array($result)) {

$start = $row["start"];
$end = $row["end"];
$date = $row["date"];

if($group_date === 0){ $group_date = $date; }
else if($date != $group_date){ 
$group_date = $date;
include ("includes/header_pop_group.php");
}

echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='3'>
<TR>
   <TD WIDTH='25%' BACKGROUND='images/group-blue-02.gif' HEIGHT='15'><B>XXX</B></TD> 
   <TD WIDTH='25%' BACKGROUND='images/group-blue-02.gif' HEIGHT='15'><B>ZZZ</B></TD> 
   <TD WIDTH='25%' BACKGROUND='images/group-blue-02.gif' HEIGHT='15'><B>YYY</B></TD> 
</TR>
<TR>
  <TD WIDTH='25%' VALIGN='TOP'>$date</TD>
  <TD WIDTH='25%' VALIGN='TOP'>$start</TD>
  <TD WIDTH='25%' VALIGN='TOP'>$end</TD>
</TR></TABLE>";

include ("includes/footer_pop.php");

}

 

The above code prints each result per day like this...

 

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

header wrapped

02/13/2009 data data

footer wrapped

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

header wrapped

02/14/2009 data data

footer wrapped

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

 

and I want it to go like this...

 

header wrapped

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

02/12/2009 data data

footer wrapped

header wrapped

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

02/13/2009 data data

footer wrapped

header wrapped

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

02/14/2009 data data

footer wrapped

Link to comment
Share on other sites

try

<?php
$sql = "SELECT * from XXX where start_search BETWEEN '$nowq_date' AND '$nowq5_date' ORDER BY start_search ASC";
$result = mysql_query($sql,$connection) or die("Invalid query: " . mysql_error());
$previous_date = 0;
while ($row = mysql_fetch_array($result)) {
$start = $row["start"];
$end = $row["end"];
$date = $row["date"];
if($previous_date === 0){
	$previous_date = $date;
	include ("header.php");
}else if($date != $previous_date){
	$previous_date = $date;
	include ("footer.php");
	include ("header.php");
}


echo "<TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='4'><TR>
      <TD WIDTH='25%' NOWRAP>$date</TD>
      <TD WIDTH='20%'>$start</TD>
      <TD WIDTH='15%'>$end</TD>
      </TR></TABLE>";
}
include ("footer.php");
?>

Link to comment
Share on other sites

When I saw

 

		include ("footer.php");
	include ("header.php");

 

It totally clicked. I never thought to run the footer then the header right under. I was looking to run header and somewhere else to run footer. Thanks to both sasa and mrdamien for being that extra set of eyes. This helped tremendously.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.