How to display database table data in a html table.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • McGowan

    How to display database table data in a html table.

    Hi, I'm trying to display data from a mysql database in a HTML table
    but for some reason my code isn't working. At the moment I have got it
    to read and display the headers and the first row of the table and it
    actually creates the remaining rows in the html table but it doesn't
    put any data in them. This is my code so far:

    <?php
    $con = mysql_connect(" localhost","REM OVED","REMOVED" );
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db ("db_0300931 9", $con);
    ?>

    <?php $selectedTable= $_GET["selectedTa ble"] ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />
    <title>Untitl ed Document</title>
    </head>
    <body>
    <?php echo $_GET["selectedTa ble"]; ?>
    <table border=1>
    <tr>
    <?php
    $headers = mysql_query("de scribe $selectedTable" );

    while($row = mysql_fetch_arr ay($headers))
    {
    ?>
    <th><?php echo $row[0]; ?></th>
    <?php
    }
    ?>
    </tr>
    <?php
    $headers = mysql_query("de scribe $selectedTable" );

    $data = mysql_query("se lect * from $selectedTable" );
    while($temp = mysql_fetch_arr ay($data))
    {
    ?>
    <tr>
    <?php
    $i=0;
    while(mysql_fet ch_array($heade rs))
    {
    ?>
    <td><?php echo $temp[$i]; ?></td>
    <?php
    $i++;
    }
    ?>
    </tr>
    <?php
    }
    ?>
    </table>
    </body>
    </html>

    It displays various tables depending on which table name is passed to
    it through the url so the number of columns cannot be a fixed number.
    Any help would be appreciated.

    Thanks,
    Oliver

  • Arjen

    #2
    Re: How to display database table data in a html table.

    McGowan schreef:
    Hi, I'm trying to display data from a mysql database in a HTML table
    but for some reason my code isn't working. At the moment I have got it
    to read and display the headers and the first row of the table and it
    actually creates the remaining rows in the html table but it doesn't
    put any data in them. This is my code so far:
    I used this code once ... There are faster ways but I really dont use
    tables anymore so I never rewrote the code.


    // table top
    echo '<table width="800">';

    // first row contains table headers (not using th)
    echo '<tr>';
    foreach ($allheaders as $header)
    {
    echo '<td>'.$header. '</td>';
    }
    echo '</tr>';

    // the data
    foreach ($alldata as $data)
    {
    echo '<tr>';
    foreach ($allheaders as $field)
    {
    // field is filled
    if ($data[$field])
    {
    echo '<td>'.$data[$field].'</td>';
    }
    // empty field
    else
    {
    echo '<td>&nbsp;</td>';
    }
    }
    echo '</tr>';
    }
    echo '</table>';

    --
    Arjen
    HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

    Comment

    • Jim Dornbos

      #3
      Re: How to display database table data in a html table.

      On Fri, 26 Jan 2007 12:19:58 -0500, McGowan <Boomer84@gmail .comwrote:
      <?php
      $i=0;
      while(mysql_fet ch_array($heade rs))
      {
      ?>
      <td><?php echo $temp[$i]; ?></td>
      <?php
      $i++;
      }
      ?>
      Don't you actually want this while pointing at $data instead of $headers?
      Or - get rid of this while entirely and move your $i=0 out one level.

      Potentially the blind leading the blind here, as I'm relatively new to PHP
      as well.

      HTH
      Jim

      --
      Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

      Comment

      • Rik

        #4
        Re: How to display database table data in a html table.

        McGowan <Boomer84@gmail .comwrote:
        Hi, I'm trying to display data from a mysql database in a HTML table
        but for some reason my code isn't working. At the moment I have got it
        to read and display the headers and the first row of the table and it
        actually creates the remaining rows in the html table but it doesn't
        put any data in them. This is my code so far:
        mysql_connect() ; etc...
        $result = mysql_query($qu ery);
        if($result){
        print '<table>';
        $row = mysql_fetch_ass oc($result);
        $heads = array_keys($row );
        $columns = count($heads);
        printf('<tr>'.s tr_repeat('<th> %s</th>',$columns). '</tr>',$heads);
        printf('<tr>'.s tr_repeat('<td> %s</td>',$columns). '</tr>',$row);
        while($row = mysql_fetch_ass oc($result)){
        printf('<tr>'.s tr_repeat('<td> %s</td>',$columns). '</tr>',$row);
        }
        print '</table>';
        }


        Optionally the printf()'s could be replaced by a nice
        implode('</td><td>',$row);
        --
        Rik Wasmus

        Comment

        • McGowan

          #5
          Re: How to display database table data in a html table.

          Thanks all. I've got it working now and I am less likely to hurt the
          first person I see now. It was really starting to frustrate me. Coffee
          probably doesn't help! :-D

          Comment

          Working...