jcoones Posted February 13, 2015 Share Posted February 13, 2015 (edited) 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 February 13, 2015 by jcoones Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 13, 2015 Share Posted February 13, 2015 It looks like you are using smart / curly quotes. Try changing lines like this echo “<center>”; To this echo "<center>"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 13, 2015 Share Posted February 13, 2015 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> Quote Link to comment Share on other sites More sharing options...
jcoones Posted February 14, 2015 Author Share Posted February 14, 2015 Thanks cyberRobot, but frankly, looking at those two lines that says change this echo “<center>”; to this: echo "<center>"; look identical to me. Quote Link to comment Share on other sites More sharing options...
jcoones Posted February 14, 2015 Author Share Posted February 14, 2015 (edited) 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 February 14, 2015 by jcoones Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2015 Share Posted February 15, 2015 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 17, 2015 Share Posted February 17, 2015 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.