Space between Groupby rendered rows???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Space between Groupby rendered rows???

Post by simonmlewis »

Hi

Code: Select all

$result = mysql_query ("SELECT * FROM diary WHERE raceid = '$id' ORDER BY cartype ASC");
while ($row = mysql_fetch_object($result))
      {
      $resultuser = mysql_query ("SELECT * FROM admin WHERE id = '$row->userid'");
      while ($rowuser = mysql_fetch_object($resultuser))
      {
echo "<tr valign='top'>
      <td>$rowuser->firstname $rowuser->lastname</td>
      <td>$row->carmake, $row->carmodel</td>
      <td>$row->cartype</td>
      <td>$row->transponder</td>
      <td>$row->frequency</td>
      <td>";
      if ($cookietype == "admin") { echo "<a href='index.php?page=attendees&id=$id&diaryid$row->id&event=$event&=&d=yes' style='text-decoration: none; color: #444444'>Admin Delete</a><br/>";}
      if ($cookieid == "$row->userid") { echo "<a href='index.php?page=attendees&id=$id&diaryid$row->id&event=$event&=&d=yes' style='text-decoration: none; color: #444444'>Delete</a>";}
      echo "</td></tr>";
      }
mysql_free_result($resultuser);
} 	mysql_free_result($result);
This products all car attendees of a race, ordered by Car Type.

Is it possible to put a blank spaced row when one car type ends, and another begins.

Ie. you have three '2WD' and then you have two'4WD'.

Can it show like this?

Fred 2WD
Bob 2WD
Ginger 2WD

Arthur 4WD
Alf 4WD

???
I would assume it checks the word being rendered and if the next one in the query is different from the one before, then put in a space, but I cannot work it out.
I tried this:

Code: Select all

$cartype=$row->cartype
if ($cartype != $cartype) { echo "stick in a space";}
But obviously as each row shows, that $cartype WILL be correct.

Help!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Space between Groupby rendered rows???

Post by mikosiko »

Code: Select all

....

$cartype = "";
while ($row = mysql_fetch_object($result))
{
  if ($cartype != $row->cartype) {
     echo "<br />";  // or whatever you want
     $cartype = $row->cartype;
  }
 ....

}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Space between Groupby rendered rows???

Post by simonmlewis »

mmmm well this puts a space after every row of the rendered table.

Code: Select all

$cartype = "";
$result = mysql_query ("SELECT * FROM diary WHERE raceid = '$id' ORDER BY cartype ASC");
while ($row = mysql_fetch_object($result))
      {
      $resultuser = mysql_query ("SELECT * FROM admin WHERE id = '$row->userid'");
      while ($rowuser = mysql_fetch_object($resultuser))
      {
echo "<tr valign='top'>
      <td>$rowuser->firstname $rowuser->lastname</td>
      <td>$row->carmake, $row->carmodel</td>
      <td>$row->cartype</td>
      <td>$row->transponder</td>
      <td>$row->frequency</td>
      <td>";
      if ($cookietype == "admin") { echo "<a href='index.php?page=attendees&id=$id&diaryid$row->id&event=$event&=&d=yes' style='text-decoration: none; color: #444444'>Admin Delete</a><br/>";}
      if ($cookieid == "$row->userid") { echo "<a href='index.php?page=attendees&id=$id&diaryid$row->id&event=$event&=&d=yes' style='text-decoration: none; color: #444444'>Delete</a>";}
      echo "</td></tr>";
      if ($cartype != $row->cartype) { echo "<tr><td colspan='6'>&nbsp;</td></tr>";}
      }
mysql_free_result($resultuser);
} 	mysql_free_result($result);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Space between Groupby rendered rows???

Post by shawngoldw »

take another look at mikosiko's post. You missed something.

Shawn
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Space between Groupby rendered rows???

Post by simonmlewis »

Code: Select all

      if ($cartype != $row->cartype) { echo "<tr><td colspan='6'>&nbsp;</td></tr>";
      $cartype = $row->cartype;}
Found it. But it producing a slightly peculiar result with no pattern to it, as far as I can tell. See attached.
Attachments
pattern.jpg
pattern.jpg (42.18 KiB) Viewed 177 times
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Space between Groupby rendered rows???

Post by shawngoldw »

It looks like you are doing the check after you print the row, you need to check first then print the row.

Shawn
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Space between Groupby rendered rows???

Post by simonmlewis »

I'm not exactly sure what this achieves.
Can you please post it showing where I'd put a blank row in?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Space between Groupby rendered rows???

Post by mikosiko »

simonmlewis wrote:

Code: Select all

      if ($cartype != $row->cartype) { echo "<tr><td colspan='6'>&nbsp;</td></tr>";
      $cartype = $row->cartype;}
Found it. But it producing a slightly peculiar result with no pattern to it, as far as I can tell.....
write the code that I gave to you in the exact same position that I wrote it.... is very clear.

Code: Select all

....

$cartype = "";
while ($row = mysql_fetch_object($result))
{
  if ($cartype != $row->cartype) {
     echo " .... ";  // or whatever you want
     $cartype = $row->cartype;
  }
 ....

}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Space between Groupby rendered rows???

Post by simonmlewis »

Code: Select all

$cartype = "";
$result = mysql_query ("SELECT * FROM diary WHERE raceid = '$id' ORDER BY cartype ASC");
while ($row = mysql_fetch_object($result))
      {
      $resultuser = mysql_query ("SELECT * FROM admin WHERE id = '$row->userid'");
      while ($rowuser = mysql_fetch_object($resultuser))
      {
echo "<tr valign='top'>
      <td>$rowuser->firstname $rowuser->lastname</td>
      <td>$row->carmake, $row->carmodel</td>
      <td>$row->cartype</td>
      <td>$row->transponder</td>
      <td>$row->frequency</td>
      <td>";
      if ($cookietype == "admin") { echo "<a href='index.php?page=attendees&id=$id&diaryid$row->id&event=$event&=&d=yes' style='text-decoration: none; color: #444444'>Admin Delete</a><br/>";}
      if ($cookieid == "$row->userid") { echo "<a href='index.php?page=attendees&id=$id&diaryid$row->id&event=$event&=&d=yes' style='text-decoration: none; color: #444444'>Delete</a>";}
      echo "</td></tr>";
      }
mysql_free_result($resultuser);
} 	mysql_free_result($result);
This produces only a few of the records expected.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply