Jump to content

Can't get PHP/HTML table to work


stevieontario

Recommended Posts

Afternoon Freaks,

 

I'm trying to display the results of a MySQL query in an HTML table, unsuccessfully. The query returns six rows, but my HTML table only returns one (besides the header row).

 

Here's the code:

</head>
<table border="1">
  <tr>
    <th>Fuel type</th>
    <th>Number of units</th>
    <th>Output</th>
    <th>Capability factor</th>
    <th>Pollution, in tons<sub>2</sub> emitted</th>
  </tr>
<?php
         $db_hostname = "localhost";
$db_name = "dbname";
$db_username = "joeblow";
$db_password = "password";
$cxn = mysql_connect($db_hostname,$db_username,$db_password) or die ("Could not connect: " . mysql_error());
		mysql_select_db($db_name);

         $sql = "SELECT output, fuel, numunits, capabilityfactor, pollution
     	FROM db1
     	GROUP BY fuel
     	ORDER BY sum(output) DESC";


		$result = mysql_query($sql, $cxn)	
			or die ("sorry, try again");

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

		extract($row);
		$capfactor = number_format($capabilityfactor,2)*100;
			if ($fuel == "fuel1") $fuel1_total = $output;
			if ($fuel == "fuel2") {$fuel2_total = $output; $fuel2_pollution = $pollution;}
			if ($fuel == "fuel3") {$fuel3_total = $output;	$fuel3_pollution = $pollution;}				
			if ($fuel == "fuel4") $fuel4_total = $output;
			if ($fuel == "fuel5") $fuel5_total = $output;
			if ($fuel == "fuel6") {$fuel6_total = $output; $fuel6_pollution = $pollution;}

			$waste = number_format($pollution);



}
?>
  <tr>
    <td><?php echo $fuel; ?></td>
    <td><?php echo $numunits; ?></td>
<td><?php echo $foutput; ?></td>
<td><?php echo $capfactor; ?></td>
<td><?php echo $waste; ?></td>	
  </tr>


</table>
<body>
</body>
</html>

 

What I get is the html table with the right header row but the only data row is the one corresponding to fuel6.

 

Something tells me the problem is with the positioning of the PHP tags, but I've tried all sorts of combinations and they usually produce an error that indicates I'm missing a curly bracket or the ?> tag.

 

I'm stumped. Anybody have an idea what I'm doing wrong?

Link to comment
Share on other sites

Your while loop is simply redefining variables on each iteration. You don't DO anything with those variables until AFTER all the rows from the result set have been processed. Thus, you are only displaying the last record since the data from the first rows have been overwritten.

Link to comment
Share on other sites

 <?php
$db_hostname = "localhost";
$db_name = "dbname";
$db_username = "joeblow";
$db_password = "password";
$cxn = mysql_connect($db_hostname,$db_username,$db_password) or die ("Could not connect: " . mysql_error());
mysql_select_db($db_name);

$sql = "SELECT output, fuel, numunits, capabilityfactor, pollution
        FROM db1
        GROUP BY fuel
        ORDER BY sum(output) DESC";
$result = mysql_query($sql, $cxn) or die ("sorry, try again");

//Generate the output into a variable
$output = '';
while ( $row = mysql_fetch_assoc($result))
{
    extract($row);
    $capfactor = number_format($capabilityfactor, 2) * 100;
    $waste = number_format($pollution);
    //Not sure what this block is for since none of these variables are used
    if ($fuel == "fuel1") $fuel1_total = $output;
    if ($fuel == "fuel2") {$fuel2_total = $output; $fuel2_pollution = $pollution;}
    if ($fuel == "fuel3") {$fuel3_total = $output;	$fuel3_pollution = $pollution;}				
    if ($fuel == "fuel4") $fuel4_total = $output;
    if ($fuel == "fuel5") $fuel5_total = $output;
    if ($fuel == "fuel6") {$fuel6_total = $output; $fuel6_pollution = $pollution;}

    $output .= "  <tr>\n";
    $output .= "    <td>{$fue}</td>\n";
    $output .= "    <td>{$numunits}</td>\n";
    $output .= "    <td>{$foutpu}</td>\n";
    $output .= "    <td>{$capfactor}</td>\n";
    $output .= "    <td>{$waste}</td>\n";
    $output .= "  </tr>\n";
}

?>

</head>
<table border="1">
  <tr>
    <th>Fuel type</th>
    <th>Number of units</th>
    <th>Output</th>
    <th>Capability factor</th>
    <th>Pollution, in tons<sub>2</sub> emitted</th>
  </tr>
  <?php echo $output; ?>
</table>
<body>
</body>
</html>

Link to comment
Share on other sites

mj, thanks -- however please accept my humble apology for having led you astray by omitting a line of code in my original post. Cut and paste sloppiness on my part.

 

The line is "$foutput = number_format($output);" -- it should have appeared just before the block of code about which you said "Not sure what this block is for since none of these variables are used."

 

Hopefully my adding it will explain what that code block is for.

 

Here's how the original code should have looked:

 

</head>
<table border="1">
  <tr>
    <th>Fuel type</th>
    <th>Number of units</th>
    <th>Output</th>
    <th>Capability factor</th>
    <th>Pollution, in tons<sub>2</sub> emitted</th>
  </tr>
<?php
         $db_hostname = "localhost";
$db_name = "dbname";
$db_username = "joeblow";
$db_password = "password";
$cxn = mysql_connect($db_hostname,$db_username,$db_password) or die ("Could not connect: " . mysql_error());
		mysql_select_db($db_name);

         $sql = "SELECT output, fuel, numunits, capabilityfactor, pollution
     	FROM db1
     	GROUP BY fuel
     	ORDER BY sum(output) DESC";


		$result = mysql_query($sql, $cxn)	
			or die ("sorry, try again");

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

		extract($row);
			$foutput = number_format($output);			
			$capfactor = number_format($capabilityfactor,2)*100;
			if ($fuel == "fuel1") $fuel1_total = $output;
			if ($fuel == "fuel2") {$fuel2_total = $output; $fuel2_pollution = $pollution;}
			if ($fuel == "fuel3") {$fuel3_total = $output;	$fuel3_pollution = $pollution;}				
			if ($fuel == "fuel4") $fuel4_total = $output;
			if ($fuel == "fuel5") $fuel5_total = $output;
			if ($fuel == "fuel6") {$fuel6_total = $output; $fuel6_pollution = $pollution;}

			$waste = number_format($pollution);



}
?>
  <tr>
    <td><?php echo $fuel; ?></td>
    <td><?php echo $numunits; ?></td>
<td><?php echo $foutput; ?></td>
<td><?php echo $capfactor; ?></td>
<td><?php echo $waste; ?></td>	
  </tr>


</table>
<body>
</body>
</html>

Link to comment
Share on other sites

Did you read my first response and the follow up code I provided? I pointed out the error and I provided some code that would solve your problem. As for the missing line of code, no it does not explain what the block of code was that I indicated. You have a succession of if() statemetns that in turn define other variables - but you never use those variables in your output. That is why I stated that I do not understand what that block is for.

 

To be more specific you are, depending on the value of $fuel, defining the variables: $fuel1_total, $fuel2_total, $fuel3_total, $fuel4_total, $fuel5_total, $fuel6_total, $fuel2_pollution, $fuel3_pollution, $fuel6_pollution. But, those variables are not used at all in the output. If you need those values elsewhere in your code you are going to need to save them to an array instead of redefining the save variable over and over in the while() loop.

Link to comment
Share on other sites

yes, read it and implemented it and it worked. Thanks very much.

 

However, I have discovered a new problem -- more accurately, clarified my original problem. And that is my HTML table definition. It does not appear to allow for the addition of more than one row of data. I want six rows.

 

I suspect I'll need to write a php function that adds HTML table rows dynamically, which also means I need to learn more about HTML tables.

 

Back to my reading!

Link to comment
Share on other sites

The code I provided should output a row for each record in the result set whether that is 1 record (1 row), 6 records (6 rows), or 600 records (600 rows). If you would like more help please provide a more detailed description of the problem and your current results.

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.