Jump to content

PHP Generated Table Header Help


suttercain

Recommended Posts

Hi guys,

 

I have a page that generates tables from a MySQL database. Each table is separated by seriesTitle.

 

For better visualization:

http://www.supermandatabase.com/actionFigures/actionfigures.php

 

Now if you look at the first table on that page you're see the table header says "DC SUPER HEROES : SERIES 2" and then lists 5 action figures. The only thing is... Lex Luthor should get his own new table because he is in series 4, not series 2.

 

Here is the code I am using to generate the tables:

 

<?php
$sql = mysql_query("SELECT *
				FROM actionFigures 
				ORDER BY seriesTitle, title ASC") or die (mysql_error());

$current = '';
while($row = mysql_fetch_array($sql)) {
  if($current != $row['seriesTitle']) {
    $current = $row['seriesTitle'];
    echo "<table width='630' border='0' bgcolor=#253b5a cellpadding='3' cellspacing='1'>
<tr>
        <td width='530'><font color=#ffffff><b>" . strtoupper($current) . " : SERIES " . $row['series'] . "</b></font></td>
	<td width='100'<font color=#ffffff><b>YEAR</b></font></td>
</tr><br>";
  }
  
  //Continuation of CSS for the Alternating Color Rows
  $class = $class == 'even' ? 'odd' : 'even';
  
  //Populate the Tables from the Database
  echo "<tr class=\"$class\">\n";
  echo "<td><a href='view_actionfigures.php?id=" . $row['actionFigureId'] . "'>" . $row['title'] . "</a></td>\n";
  echo "<td>$row[year]</td>\n";
  echo "</tr>\n";
}
echo "</table>";
echo "<br>";
?>

 

I see that the echoed table is echoing anything with the same seriesTitle into the same table. How do I get it so it echoes the same seriesTitle and also the series?

 

Thanks for the help and advice.

 

SC

Link to comment
https://forums.phpfreaks.com/topic/49244-php-generated-table-header-help/
Share on other sites

Sure thing...

 

Here is the entire page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
@import url("../sd_style.css");
</style>

<?php require ('../get_connected.php'); ?>
<?php include ('../head.php'); ?>
<title>Superman Database :: Comics</title>
<body>
        <table width="986"  border="0" cellspacing="0" cellpadding="5" valign="top">
            <tr>
              <td colspan="2" valign='top' bgcolor="#FFFFFF">
			<table width="650" border="0" align="center" cellpadding="2" cellspacing="2">
                <tr>
                  <td colspan="2" valign='top'><?php echo "<h2>Action Figures</h2>"?></td>
                  </tr>
               
  <tr>
    <td colspan="2" valign='top'><?php       
//Convert seriesTitle Column to out put from the MySQL instead of HTML
$sql = mysql_query("SELECT *
				FROM actionFigures 
				ORDER BY seriesTitle, title ASC") or die (mysql_error());

$current = '';
while($row = mysql_fetch_array($sql)) {
  if($current != $row['seriesTitle']) {
    $current = $row['seriesTitle'];
    echo "<table width='630' border='0' bgcolor=#253b5a cellpadding='3' cellspacing='1'>
<tr>
        <td width='530'><font color=#ffffff><b>" . strtoupper($current) . " : SERIES " . $row['series'] . "</b></font></td>
	<td width='100'<font color=#ffffff><b>YEAR</b></font></td>
</tr><br>";
  }
  
  //Continuation of CSS for the Alternating Color Rows
  $class = $class == 'even' ? 'odd' : 'even';
  
  //Populate the Tables from the Database
  echo "<tr class=\"$class\">\n";
  echo "<td><a href='view_actionfigures.php?id=" . $row['actionFigureId'] . "'>" . $row['title'] . "</a></td>\n";
  echo "<td>$row[year]</td>\n";
  echo "</tr>\n";
}
echo "</table>";
echo "<br>";
?>	</td>
  </tr>
                </table>				<div align="left"><br>
                </div></td>
              <td width="18%" valign="top" bgcolor="#CCCCCC"></td>
            </tr>
                  <tr>
                    <td width="17%" valign="top"></td>
                  </tr>
			</table>
                </table>
<?php include('../foot.php'); ?>
</body>
</html>

 

 

And my MySQL table is set up as such

_____________________________________________________

|  TITLE  | actionFigureId | series |      seriesTitle      | image |

-------------------------------------------------------------

|Lex Luthor|                26|        4 | DC Super Heroes |    ll.jpg|

--------------------------------------------------------------

| Superman|                25          2 | DC Super Heroes |  sm.jpg |

--------------------------------------------------------------

 

I hope that table helps.

 

Thanks for any advice or suggestions.

try adding GROUP BY series in your select statement...

 

Your doing a ORDER BY seriesTitle, so I am assuming that everything that is being displayed is in the same Series Title but has a different series number, which is incorrect....You should have a different series title with a different series number...just taking a shot in the dark...

Hi Mpharo,

 

Thanks for the suggestions but sadly it didn't work. I instead just echoed the series into it's own column and the seriesTitle is in the header.

 

I would still like to do it the other way, but in the meantime I'll run it as is.

 

Thanks again.

Okay, let's try this again... if you look at:

http://www.supermandatabase.com/actionFigures/actionfigures.php

 

You'll see the first table is titled "DC SUPER HEROES : SERIES 2" which is generated by the MySQL database. But if you look in the series column you'll notice that there are some series 4 in there. Ideally they should get their own table but I have not been successful in getting that separated.

 

Here is the code that is used:

<?php
$sql = mysql_query("SELECT *
				FROM actionFigures 
				ORDER BY seriesTitle, series, title ASC") or die (mysql_error());

$current = '';
$series = '';
while($row = mysql_fetch_array($sql)) {
  if($current != $row['seriesTitle'] && $series != $row['series']) { // this is the line that generates the header
    $current = $row['seriesTitle'];
$series = $row['series'];
    echo "<table width='630' border='0' bgcolor=#253b5a cellpadding='3' cellspacing='1'>
<tr>
        <td width='530'><font color=#ffffff><b>" . strtoupper($current) . " : SERIES " . $series . "</b></font></td>
	<td width='100'<font color=#ffffff><b>SERIES</b></font></td>
	<td width='100'<font color=#ffffff><b>YEAR</b></font></td>
</tr><br>";
  }
?>

 

I am able to separate the title no problem, but when I try to get it to separate by two columns, I can;'t get it,

 

SC

Try something like this, it is untested and I am not sure what the rest of your table structure is, but this should give you a good start. I firmly believe the reason why your getting the results in your code is because you are sorting by seriesTitle first, then sorting secondly by series. In my code you are getting all the distinct seriesTitle's then using the results to get the rest of the information for just that distinct title.

 

<?php

$select=mysql_query("SELECT DISTINCT seriesTitle FROM actionFigures ASC") or die(mysql_error());

while($sql=mysql_fetch_arrray($select)){

echo "<table width='630' border='0' bgcolor=#253b5a cellpadding='3' cellspacing='1'><tr>
        <td width='530'><font color=#ffffff><b>" . strtoupper($current) . " : SERIES " . $series . "</b></font></td>
	<td width='100'<font color=#ffffff><b>SERIES</b></font></td>
	<td width='100'<font color=#ffffff><b>YEAR</b></font></td>
</tr>";

$select2 = mysql_query("SELECT * FROM actionFigures WHERE seriesTitle='$sql[seriesTitle]' ASC") or die (mysql_error());


while($row = mysql_fetch_array($select2)) {
  
echo "<tr><td>$row[TITLE]</td><td>$row[series]</td><td>$row[year]</td></tr>";

}
echo "</table>";
}
?>

 

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.