SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    HTML table header keeps repeating for each row of DB table

    The following is the script I'm using to display all data in a table. All of the table row data displays in their own rows. There are two problems:

    1. The header cell row section should be seen only once. Right now it is repeated above each row displayed. Where do I put the info so it displays just once?

    2. Each row displays in its own table, with the header cells repeating at top once for each row. As a consequence, the table columns are not lined up, making it hard to read. How do I get the info to all display in the same table?

    The script is based loosely on Kevin Yanks' Build Your Own DB Driven Website book

    Thanks!
    Steve

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>SPOTLIGHT_VEH TABLE
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" type="text/css" href="associatedcss.css">
    </head>
    <body>
    <h1>SPOTLIGHT_VEH TABLE</h1>


    <?php
    require_once ('connect.php');
    ?>

    <?php
    $sv 
    = @mysql_query('SELECT DISTINCT id, vehicle_name, descr, year, photoid, filename, conversion FROM spotlight_veh');
    if (!
    $sv) {
      exit(
    '<p>Unable to obtain spotlight_veh data from the database.</p>');
    }
    ?>



    <?php





    while ($asv mysql_fetch_array($sv)) {

      echo 
    "<table class='chart800'><tr>";
      echo 
    "<td class='headercell'><p>id</p></td>";
      echo 
    "<td class='headercell'><p>vehicle_name</p></td>";
      echo 
    "<td class='headercell'><p>owner_name</p></td>";
      echo 
    "<td class='headercell'><p>descr</p></td>";
      echo 
    "<td class='headercell'><p>year</p></td>";
      echo 
    "<td class='headercell'><p>photoid</p></td>";
      echo 
    "<td class='headercell'><p>filename</p></td>";
      echo 
    "<td class='headercell'><p>conversion</p></td></tr>";

      
    $aid $asv['id'];
      
    $avname htmlspecialchars($asv['vehicle_name']);
      
    $aoname htmlspecialchars($asv['owner_name']);
      
    $adescr htmlspecialchars($asv['descr']);
      
    $ayear $asv['year'];
      
    $apid $asv['photoid'];
      
    $afilename htmlspecialchars($asv['filename']);
      
    $aconv htmlspecialchars($asv['conversion']);

      echo 
    "<tr><td class='row1'><p>$aid</p></td>";
      echo 
    "<td class='row1'><p>$avname</p></td>";
      echo 
    "<td class='row1'><p>$aoname</p></td>";
      echo 
    "<td class='row1'><p>$adescr</p></td>";
      echo 
    "<td class='row1'><p>$ayear</p></td>";
      echo 
    "<td class='row1'><p>$apid</p></td>";
      echo 
    "<td class='row1'><p>$afilename</p></td>";
      echo 
    "<td class='row1'><p>$aconv</p></td>";

    }
    ?>
    </tr></table>
    <p><a href="index1.php">Return to front page</a></p>
    <p><a href="spotlight_select.spotlight_veh.php">Refresh page</a></p>
    </body>
    </html>

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Move the headers out of the loop, like so:

    PHP Code:
    echo "<td class='headercell'><p>id</p></td>";
    echo 
    "<table class='chart800'><tr>";
    echo 
    "<td class='headercell'><p>vehicle_name</p></td>";
    echo 
    "<td class='headercell'><p>owner_name</p></td>";
    echo 
    "<td class='headercell'><p>descr</p></td>";
    echo 
    "<td class='headercell'><p>year</p></td>";
    echo 
    "<td class='headercell'><p>photoid</p></td>";
    echo 
    "<td class='headercell'><p>filename</p></td>";
    echo 
    "<td class='headercell'><p>conversion</p></td></tr>";


    while (
    $asv mysql_fetch_array($sv)) {

      
    $aid $asv['id'];
      
    $avname htmlspecialchars($asv['vehicle_name']);
      
    $aoname htmlspecialchars($asv['owner_name']);
      
    $adescr htmlspecialchars($asv['descr']);
      
    $ayear $asv['year'];
      
    $apid $asv['photoid'];
      
    $afilename htmlspecialchars($asv['filename']);
      
    $aconv htmlspecialchars($asv['conversion']);

      echo 
    "<tr><td class='row1'><p>$aid</p></td>";
      echo 
    "<td class='row1'><p>$avname</p></td>";
      echo 
    "<td class='row1'><p>$aoname</p></td>";
      echo 
    "<td class='row1'><p>$adescr</p></td>";
      echo 
    "<td class='row1'><p>$ayear</p></td>";
      echo 
    "<td class='row1'><p>$apid</p></td>";
      echo 
    "<td class='row1'><p>$afilename</p></td>";
      echo 
    "<td class='row1'><p>$aconv</p></td>";



  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you! That did the job!

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need while loop here and keep HTML out of PHP
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •