Jump to content

Creating Dynamic HTML table


eraxian

Recommended Posts

Hello guys, I'm new to PHP and web development in general so go easy on me.

 

Here's the sitrep. I created a database on a mysql server, it has 1 field. I want to create an html table dynamically when users click on a link, and display the data from the mysql database.

 

I found several scripts online for doing this but none have worked. This is what I have so far:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

 

<?php

$con = mysql_connect("myservername","myusername","mypassword");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("bands", $con);

 

$result = mysql_query("SELECT * FROM `list` WHERE name LIKE \'A%\' ORDER BY name");

 

echo "<table border='1'>

<tr>

<th>Band Name</th>

</tr>";

 

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

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

  echo "</tr>";

  }

echo "</table>";

 

mysql_close($con);

?>

<body>

</body>

</html>

 

 

AND this is the actual output I get when I open this page,

 

"Band Name "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['name'] . ""; echo ""; } echo ""; mysql_close($con); ?>"

 

So obviously somewhere I've made a grievous error, please help! :( Thank you 

Link to comment
Share on other sites

try this

<?php

$sql = "SELECT * FROM `list` WHERE name LIKE \'A%\' ORDER BY name";

echo table2Table ($sql);

function table2Table($query) {
    $result = mysql_query($query);

    $str = "<TABLE border='1' cellpadding='4'>\n";

    // column headings
          $str .=  "<tr>\n";
          while ($fld = mysql_fetch_field ($result)) {
                   $str .= "<th>{$fld->name}</th>\n";
          }
          $str .=  "</tr>\n";


    // list data
          while ($row = mysql_fetch_row($result)) {
          $str .=  "<tr>\n";
          foreach ($row as $field) {
                   $str .=  "<td>$field</td>\n";
          }
          $str .=  "</tr>\n";
    }

    $str .=  "</TABLE>\n";
    
    return $str;
}

?>

Link to comment
Share on other sites

:( still having the same type of issue, this is what I get when i inserted the code you posted,

 

\n"; // column headings $str .= "\n"; while ($fld = mysql_fetch_field ($result)) { $str .= "{$fld->name}\n"; } $str .= "\n"; // list data while ($row = mysql_fetch_row($result)) { $str .= "\n"; foreach ($row as $field) { $str .= "$field\n"; } $str .= "\n"; } $str .= "\n"; return $str; } ?>

 

 

Why is it not recognizing the rest of the code as part of the php script??? i'm baffled, plz help!

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.