why do i get dupilicate table header

Discussion in 'PHP' started by babyphp, Apr 21, 2008.

  1. #1
    when i run this code i get duplictae header

    $search=$_POST["search"];

    $result = mysql_query("SELECT * FROM contacts WHERE firstname LIKE '%$search%' OR lastname LIKE '%$search%' OR email LIKE '%$search%' OR phone LIKE '%$search%'");



    echo "<table>";
    while($row=mysql_fetch_array($result))


    {
    $firstname=$row["firstname"];
    $lastname=$row["lastname"];
    $email=$row["email"];
    $phone=$row["phone"];


    echo "<tr>FIRSTNAME<td>" . $firstname . "</td>LASTNAME<td>" . $lastname . "</td>EMAIL<td>" . $email . "</td>PHONE<td>" . $phone . "</td></tr>";





    }



    ?>
    </p>
    <p>&nbsp;</p>

    </body>
    </html>



    FIRSTNAMELASTNAMEEMAILPHONEFIRSTNAMELASTNAMEEMAILPHONEFIRSTNAMELASTNAMEEMAILPHONEFIRSTNAMELASTNAMEEMAILPHONE
    collins smith 5555
     
    babyphp, Apr 21, 2008 IP
  2. manni

    manni Active Member

    Messages:
    320
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I need to see the page on how it looks, then I can identify your problem :)

    But I think it is because you are not ending the table add this "</table>" below the loop
     
    manni, Apr 21, 2008 IP
  3. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #3
    You cannot have text inside <table> and outside the <td> tags. Check the HTML specs for tables, your resulting markup is invalid.
     
    wmtips, Apr 21, 2008 IP
  4. qurve

    qurve Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You are generating completely invalid html, try this template:

    
    <table>
    <thead>
    <tr>
    <th>First Name</th>
    <th>Last name</th>
    <th>Email</th>
    <th>Phone</th>
    </tr>
    </thead>
    <tbody>
    [loop]
    <tr>
    <td><?= htmlspecialchars($firstname) ?></td>
    <td><?= htmlspecialchars($lastname) ?></td>
    <td><?= htmlspecialchars($email) ?></td>
    <td><?= htmlspecialchars($phone) ?></td>
    </tr>
    [end loop]
    </tbody>
    </table>
    
    Code (markup):
    Also, please note that I added in escaping of output using the htmlspecialchars() function. This prevents stray characters in your file names like < and > from breaking your HTML.
     
    qurve, Apr 21, 2008 IP
  5. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #5
    This should work :)
    
    <?php
    $search = $_POST["search"];
    
    $result = mysql_query("SELECT * FROM contacts WHERE firstname LIKE '%$search%' OR lastname LIKE '%$search%' OR email LIKE '%$search%' OR phone LIKE '%$search%'");
    echo "<table>";
    
    // This is the table header
    echo "<tr><td>Firstname</td><td>Lastname</td><td>Email</td><td>Phone</td></tr>";
    while($row=mysql_fetch_array($result)) {
        $firstname=$row["firstname"];
        $lastname=$row["lastname"];
        $email=$row["email"];
        $phone=$row["phone"];
        echo "<tr><td>" . $firstname . "</td><td>" . $lastname . "</td><td>" . $email . "</td><td>" . $phone . "</td></tr>";
    }
    echo "</table>";
    ?>
    </p>
    <p>&nbsp;</p>
    
    </body>
    </html>
    
    PHP:
     
    xrvel, Apr 21, 2008 IP
  6. manni

    manni Active Member

    Messages:
    320
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    yes this is what i meant in my previous post good work :)
     
    manni, Apr 22, 2008 IP