Jump to content

MYSQL output into HTML table format issue.


smw6180

Recommended Posts

I've been trying to figure this one out for days.  I have an issue with output from a mysql table not formatting the way I want it to.  The logic is fine, it's just the display that I can't quite get.  Below is a screenshot of my table.  Obviously what I'd like to do is not have all that whitespace in the table.  It makes things look really clunky.  Unfortunately I've not been able to figure out how to remove that whitespace and present the table in a professional looking way.  The relevant code is:

 

 

echo "<table width=100% border='5' bgcolor='cornsilk' bordercolor=#FFFFFF bordercolorlight=gray bordercolordark=black cellspacing='0' cellpadding='2'>";

 

 

foreach($dev as $line) {

if ($line !="") {

 

 

$query = "SELECT * FROM devices WHERE diskgroup1 LIKE \"%$line%\"

OR diskgroup2 LIKE \"%$line%\"

OR diskgroup3 LIKE \"%$line%\"

OR diskgroup4 LIKE \"%$line%\"

OR diskgroup5 LIKE \"%$line%\"

ORDER BY diskgroup1";

 

$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);

$diskresult = mysql_query($query);

 

}

 

 

$i=0;

while ($i < $numrows) {

$g1 = mysql_result($diskresult,$i,"diskgroup1");

$g2 = mysql_result($diskresult,$i,"diskgroup2");

$g3 = mysql_result($diskresult,$i,"diskgroup3");

$g4 = mysql_result($diskresult,$i,"diskgroup4");

$g5 = mysql_result($diskresult,$i,"diskgroup5");

 

echo "<td>$g1</td>";

echo "<td>$g2</td>";

echo "<td>$g3</td>";

echo "<td>$g4</td>";

echo "<td>$g5</td>";

echo "</tr>";

 

$i++;

}

echo "</table>";

 

 

Any help, or pointers to relevant websites on this would be appreciated.  I'm pretty much done with what I can figure out on my own at this point.  Thanks!

 

Edit:  Included <table> </table> tags.

 

-Steve

 

 

 

 

[attachment deleted by admin]

Hi smw6180,

 

From your posted code you are not defining a table, or a tr.

 

You need to change:

 

echo "<td>$g1</td>";
echo "<td>$g2</td>";
echo "<td>$g3</td>";
echo "<td>$g4</td>";
echo "<td>$g5</td>";
echo "</tr>";

 

to:

 

echo "<table>"; 
echo "<tr>";
echo "<td>$g1</td>";
echo "<td>$g2</td>";
echo "<td>$g3</td>";
echo "<td>$g4</td>";
echo "<td>$g5</td>";
echo "</tr>";
echo "</table>";

 

At the very least just to render correctly.  As a general rule: 

 

Every separate table needs to be defined by the <table> and </table> tags.

 

Every new row is created by using <tr> and </tr> tags

 

Every new column is created with <td> and </td> tags

 

<td>'s go inside <tr>'s which go inside <tables>'s

 

Hope this helps.

I added the table tags.  I'd rather not post the entire code because most of it's not relevant and it's insanely long.  If you look at the table image I attached you'll see the problem and what I'm hoping to accomplish.  The code I posted shows how I'm getting the values into the table image I attached.  The issue is with those spaces cropping up.  I just need to figure out how to get rid of those, or how to populate the html table differently if I am doing it incorrectly.

Ok, I've cleaned up the original code and gotten rid of extraneous comments and such, which I think made it short enough to post.

 

<?php

 

require "secure.php";

$link = mysql_connect($db_host, $db_user, $db_pass) or die ("<h2>Could not connect: </h2>" . mysql_error());

// Get the search variable

if ($_GET['host']==""){

$var = $_GET['id'];

$searchstuff = 1; } else {

$var = $_GET['host']; }

$trimmed = trim($var); //trim whitespace from the stored variable

 

mysql_connect($db_host, $db_user, $db_pass) or die (mysql_error());

mysql_select_db("$database") or die (mysql_error());

$query = "SELECT * FROM sandata WHERE Servername = \"$trimmed\" order by Servername";

$numresults= mysql_query($query);

$numrows= mysql_num_rows($numresults);

$result = mysql_query($query) or die(mysql_error());

 

if ($numrows == 0) {

echo "<h4>Results</h4>";

echo "<p>Sorry, "" . $trimmed . "" was not found in the database</p>";

echo "<br><a href=index.php>Back to search page.</a>";

exit();

}

 

echo "<img src=images\amfc_corp_tag.gif>";

echo "<center>";

echo "<br>Back to summary page for <a href=search.php?q=$var>$var</a>?";

echo "<br>Search for a different <a href=index.php>server</a>?";

echo "<h2>Zones for $var</h2>";

echo "<table border='5' bgcolor='cornsilk' bordercolor=#FFFFFF bordercolorlight=gray bordercolordark=black cellspacing='0' cellpadding='2'>";

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

 

$zone1 = $row["zone1"];

$zone2 = $row["zone2"];

$zone3 = $row["zone3"];

$zone4 = $row["zone4"];

$devs = $row["devices"];

 

// Show zones

 

if ($zone1 !="") {

echo "<td width=80><div align=left>$zone1</td><tr></div>";

}

if ($zone2 !="") {

echo "<td width=80><div align=left>$zone2</td><tr></div>";

}

if ($zone3 !="") {

echo "<td width=80><div align=left>$zone3</td><tr></div>";

}

if ($zone4 !="") {

echo "<td width=80><div align=left>$zone4</td><tr></div>";

}

if ($zone1 == "" && $zone2 == "" && $zone3 == "" && $zone4 == "") {

echo "<td><div align=left>No active zones for $var</td></div><tr>";

}

echo "</table>";

 

echo "<h2><center>SAN attached devices for $var</h2>";

echo "<table width=100% border='5' bgcolor='cornsilk' bordercolor=#FFFFFF bordercolorlight=gray bordercolordark=black cellspacing='0' cellpadding='2'>";

# echo "<tr>";

# echo "<th>Disk Group 1</th>"; 

# echo "<th>Disk Group 2</th>";

# echo "<th>Disk Group 3</th>";

# echo "<th>Disk Group 4</th>";

# echo "<th>Disk Group 5</th>";

# echo "</tr>";

 

$dev = explode (" ", $devs);

reset($dev);

 

// Show devices

$s=0;

$count = 1 + $s;

 

 

if ($devs == "") {

echo "<td width=100%><div align=left>Database error!!!</td></div>";

exit();

}

 

 

 

 

foreach($dev as $line) {

if ($line !="") {

 

 

$query = "SELECT * FROM devices WHERE diskgroup1 LIKE \"%$line%\"

OR diskgroup2 LIKE \"%$line%\"

OR diskgroup3 LIKE \"%$line%\"

OR diskgroup4 LIKE \"%$line%\"

OR diskgroup5 LIKE \"%$line%\"

ORDER BY diskgroup1";

 

$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);

$diskresult = mysql_query($query);

 

}

 

 

$i=0;

while ($i < $numrows) {

$g1 = mysql_result($diskresult,$i,"diskgroup1");

$g2 = mysql_result($diskresult,$i,"diskgroup2");

$g3 = mysql_result($diskresult,$i,"diskgroup3");

$g4 = mysql_result($diskresult,$i,"diskgroup4");

$g5 = mysql_result($diskresult,$i,"diskgroup5");

 

echo "<td>$g1</td>";

echo "<td>$g2</td>";

echo "<td>$g3</td>";

echo "<td>$g4</td>";

echo "<td>$g5</td>";

echo "</tr>";

 

$i++;

}

 

 

# while ($row = mysql_fetch_array($diskresult)) {

# $group1 = $row["diskgroup1"];

# $group2 = $row["diskgroup2"];

# $group3 = $row["diskgroup3"];

# $group4 = $row["diskgroup4"];

# $group5 = $row["diskgroup5"];

 

 

# echo "<tr>";

# echo "</td><td>";

# echo $group1;

# echo "</td><td>";

# echo $group2;

# echo "</td><td>";

# echo $group3;

# echo "</td><td>";

# echo $group4;

# echo "</td><td>";

# echo $group5;

# echo "</td></tr>";

# echo "</tr>";

 

#}

 

 

}

}

echo "</table>";

exit();

 

 

I was reading your post in hopes to find answers to my my issue, and I noticed something:

 

 

 

# echo "<tr>";

# echo "</td><td>";

# echo $group1;

# echo "</td><td>";

# echo $group2;

# echo "</td><td>";

# echo $group3;

# echo "</td><td>";

# echo $group4;

# echo "</td><td>";

# echo $group5;

# echo "</td></tr>";

# echo "</tr>";

 

You have two closing </tr> closing tags at the end but I only seen one opening <tr> tag.  I'm somewhat a newbie at all this so maybe that was intentional and it's something I just haven't learned yet (which is entirely possible), otherwise I know misplaced tags can cause your HTML output to behave unfavorably to save the least, and syntax is always the last thing I check..haha, though I'm learning.

Nah the <TR> tags are fine.  While I know there's an extra </TR> in there, it indeed affects the display removing one of those, but unfavorably, so I leave it in.  Oddly enough it was accidental, initially, but turned out to do the trick.

 

Of course it didn't fix the output in that table I showed a screenshot of in the first post.  I just have no idea how to line up the columns.  The script iterates and does what it's supposed to do, and I even admit that based on that script the table looks as it should.  But it doesn't look as I'd LIKE it to look, which is the problem :)

 

 

Before this part you have closed last opened TR tag, but you start echoing new <td>'s in this part when they are not inside any opened TR tag.

 

if ($devs == "") {
echo "<td width=100%><div align=left>Database error!!!</td></div>";
exit();
}

 

Edit:

Also before that I meantioned already, you start table and not TR tag after starting new table and start outputting td tags before opening any TR tag here:

echo "<table border='5' bgcolor='cornsilk' bordercolor=#FFFFFF bordercolorlight=gray bordercolordark=black cellspacing='0' cellpadding='2'>";
while ($row = mysql_fetch_array($result)) {

$zone1 = $row["zone1"];
$zone2 = $row["zone2"];
$zone3 = $row["zone3"];
$zone4 = $row["zone4"];
$devs = $row["devices"];

// Show zones

if ($zone1 !="") {
echo "<td width=80><div align=left>$zone1</td><tr></div>";
}

The real issue is below.  If you look at the section in bold you'll see that each row in the database is iterated one by one.  So for example, anything in 'diskgroup1' all prints out at one time.  Then it moves to diskgroup2, etc, etc.  But it's already put in <tr>'s so it starts the next diskgroup lower, instead of right next to the previous one, as I'd like.  This seems to indicate I'm fetching the data incorrectly, or that there's another way to output it, but I can't figure out what, to save my life.

 

echo "<h2><center>SAN attached devices for $var</h2>";

echo "<table width=100% border='5' bgcolor='cornsilk' bordercolor=#FFFFFF bordercolorlight=gray bordercolordark=black cellspacing='0' cellpadding='2'>";

echo "<tr>";

echo "<th>Disk Group 1</th>"; 

echo "<th>Disk Group 2</th>";

echo "<th>Disk Group 3</th>";

echo "<th>Disk Group 4</th>";

echo "<th>Disk Group 5</th>";

#echo "</tr>";

 

$dev = explode (" ", $devs);

reset($dev);

 

// Show devices

$s=0;

$count = 1 + $s;

 

 

if ($devs == "") {

echo "<td width=100%><div align=left>Database error!!!</td></div>";

exit();

}

 

 

 

 

foreach($dev as $line) {

if ($line !="") {

 

 

$query = "SELECT * FROM devices WHERE diskgroup1 LIKE \"%$line%\"

OR diskgroup2 LIKE \"%$line%\"

OR diskgroup3 LIKE \"%$line%\"

OR diskgroup4 LIKE \"%$line%\"

OR diskgroup5 LIKE \"%$line%\"

ORDER BY diskgroup1";

 

$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);

$diskresult = mysql_query($query);

 

}

 

 

$i=0;

while ($i < $numrows) {

$g1 = mysql_result($diskresult,$i,"diskgroup1");

$g2 = mysql_result($diskresult,$i,"diskgroup2");

$g3 = mysql_result($diskresult,$i,"diskgroup3");

$g4 = mysql_result($diskresult,$i,"diskgroup4");

$g5 = mysql_result($diskresult,$i,"diskgroup5");

 

echo "<tr>";

echo "<td>$g1</td>";

echo "<td>$g2</td>";

echo "<td>$g3</td>";

echo "<td>$g4</td>";

echo "<td>$g5</td>";

echo "</tr>";

 

$i++;

}

}

 

}

echo "</table>";

#echo "</center>";

exit();

What I need is all the data for diskgroup1 listed under the Group 1 column.  Then the same for Group 2, 3, 4, 5.

 

If I remove the <TR> tags from the loop I get every line side by side, which means that the output doesn't match the heading (data for group1 goes under group1, data for group2 lines up under group2, etc).

How bout doing it like this?

 

<?php
while ($row = mysql_fetch_assoc($diskresult))
{
    echo '<td>';
    echo '<td>'. $row['column1'] .'</td>';
    echo '<td>'. $row['column2'] .'</td>';
    echo '<td>'. $row['column3'] .'</td>';
    echo '<td>'. $row['column4'] .'</td>';
    // etc..
    echo '</tr>';
}

 

Btw. you still have the first problem in your code I meantioned earlier. Which can in general mess up your structure or lead into mysterious behaviour.

Yeah, you're right.  I fixed it.  Ok, with the code you gave I have the exact same results as I've been having.

 

if ($devs == "") {

echo "<tr>";

echo "<td width=100%><div align=left>Database error!!!</td></div>";

echo "</tr>";

exit();

}

 

 

foreach($dev as $line) {

if ($line !="") {

 

 

$query = "SELECT * FROM devices WHERE diskgroup1 LIKE \"%$line%\"

OR diskgroup2 LIKE \"%$line%\"

OR diskgroup3 LIKE \"%$line%\"

OR diskgroup4 LIKE \"%$line%\"

OR diskgroup5 LIKE \"%$line%\"

ORDER BY diskgroup1";

 

$numresults=mysql_query($query);

$numrows=mysql_num_rows($numresults);

$diskresult = mysql_query($query);

 

}

 

echo '<tr>';

while ($row = mysql_fetch_assoc($diskresult))

{

    echo '<td>'. $row['diskgroup1'] .'</td>';

    echo '<td>'. $row['diskgroup2'] .'</td>';

    echo '<td>'. $row['diskgroup3'] .'</td>';

    echo '<td>'. $row['diskgroup4'] .'</td>';

    echo '<td>'. $row['diskgroup5'] .'</td>';

    echo '</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.