Jump to content

Recommended Posts

Hi,

I'm having a problem creating an HTML table in php.  I used php code that I believed was the proper format but I guess it is not.  I'm trying to pull data from a MySQL database and put the contents into a 3 cell table. There should be one cell in the first row and 2 cells side-by-side in the second row. The data is being displayed ok but just not in a table.  Maybe someone can steer me in the right direction to create an html table in php.

 

Here is the code tat I used:

<?php
echo “<center>”;
echo “<table width="600" cellpadding="6">”;
echo “<tr>”;
echo ”<td>”;

$dog = $_POST['breed'];
$servername = "localhost";
$username = "myusername";
$password = "**************";
$dbname = "mydatabase";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM doginfo WHERE breed='$dog'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      $detail = $size = $row["detail"];
      $breed = $row["breed"];
      $size = $row["size"];
      $height = $row["height"];
      $weight = $row["weight"];
      $life = $row["life"];
      $affect = $row["affection"];
      $playfull = $row["playfull"];
      $friendlyd = $row["friendly_dogs"];
      $friendlyst = $row["friendly_stranger"];
      $train = $row["training"];
      $health = $row["health"];
    }
} else {
    echo "0 results";
}
$conn->close();

echo nl2br("$detail" . "\n\n");
echo “/td>”;
echo “</tr>”;
echo “<tr>”;
echo “<td>”;
        .nl2br("Breed : " . "$breed" . "\n")
        .nl2br("Size : " . "$size" . "\n")
        .nl2br("Height : " . "$height" . "\n")
        .nl2br("Weight : " . "$weight" . "\n")
        .nl2br("Life Expectancy : " . "$life" . "\n\n")
    );

echo “/td>”;
echo “<tr>”;

echo “<tr>”;
echo “<td>”;
echo (
        nl2br("Based on a scale of 1 to 5\n\n")
       .nl2br(" Affection : " . "$affect" . "\n")
       .nl2br("Playfull : " . "$playfull" . "\n")
       .nl2br("Friendly with other dogs : " . "$friendlyd" . "\n")
       .nl2br("Friendly with strangers : " . "$friendlyst" . "\n")
       .nl2br("Ease of training : " . "$train" . "\n")
        );
    if($health == "n/a") {
       echo " ";
      } else {
       echo nl2br("Health : " ."$health" . "\n\n");
      }
    echo “</td>”;
    echo “</tr>”;
    echo “</table>”;
    echo ”</center>”;
?>

By the way,...  I am also having a problem bolding a line of text in the data displayed.

 

 

Thanks in advance.

Edited by jcoones
Link to comment
https://forums.phpfreaks.com/topic/294585-problem-creating-html-table-with-php/
Share on other sites

You're not writing valid HTML code. E.g.

 

echo “/td>”;

 

Aside from that, the code is very clunky.

 

 

<?php
 
 
$dog = $_POST['breed'];
 
$servername = "localhost";
$username = "myusername";
$password = "**************";
$dbname = "mydatabase";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
 
$sql = "SELECT * FROM doginfo WHERE breed='$dog'";
$result = $conn->query($sql);
if (!$result->num_rows)
{
    $output = "<tr><td>0 results</td></tr>\n";
else
{
    // output data of each row
    $output = '';
    while($row = $result->fetch_assoc())
    {
        $output .= "<tr>\n";
 
        $output .= "<td colspan='2'>\n";
        $output .= nl2br($detail);
        $output .= "</td>\n";
 
        $output .= "<td>\n";
        $output .= "Breed : {$row['breed']}<br>\n";
        $output .= "Size : {$row['size']}<br>\n";
        $output .= "Height : {$row['height']}<br>\n";
        $output .= "Weight : {$row['weight']}<br>\n";
        $output .= "Life : {$row['life']}<br><td>\n";
        $output .= "</td>\n";
 
        $output .= "<td>\n";
        $output .= "Based on a scale of 1 to 5<br>\n";
        $output .= "Affection : {$row['affection']}<br>\n";
        $output .= "Playfull : {$row['playfull']}<br>\n";
        $output .= "Friendly with other dogs : {$row['friendly_dogs']}<br>\n";
        $output .= "Friendly with strangers : {$row['friendly_stranger']}<br>\n";
        $output .= "Ease of training : {$row['training']}\n";
        $output .= ($row['health'] != "n/a") ? "<br>Health : {$row['health']}" : "";
        $output .= "</td>\n";
 
        $output .= "</tr>\n";
    }
}
$conn->close();
 
?>
 
<center>
<table width="600" cellpadding="6">
    <?php echo $output; ?>
</table>
</center>

Thanks Psycho.  I guess its not hard to tell that I am new to php.  :)

 

I replaced a closing curly brace after the 'else' that was missing in your code and gave it a try but now I'm getting a error saying: 

 

Parse error: syntax error, unexpected '"' in /home.... : eval()'d code on line 28.

 

I have gone over the code several times but I cannot find any erroneous "" .

Edited by jcoones

Look at the code in my response above. PHP Freaks colors the code to show different types of content in the code. If you look at that code you will see something different on one line. The reason is due to a missing quote mark at the beginning of the string.

Thanks cyberRobot,  but frankly, looking at those two lines that says change this

echo “<center>”;

to this:

echo "<center>";

look identical to me.

 

One uses curly / smart quotes. The other has straight quotes.

 

Perhaps your server is set up differently, but when I try something like the following:

<?php
echo “<center>hello</center>”;
?>
 
...I get the following parse error:
Parse error: syntax error, unexpected '>' in...

 

 

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.