How To Display Mysql Data In A Table Format

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

  1. #1
    how can i display this code in a table format like this

    FIRSTNAME LASTNAME EMAIL PHONE
    john smith @ 911

    this is what the code do at the moment
    john
    smith
    @
    911



    $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%'");

    while($row=mysql_fetch_array($result))

    {


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


    echo "$firstname <br> $lastname <br> $email <br> $phone <br>";




    }



    ?>



    </body>
    </html>
     
    babyphp, Apr 21, 2008 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    You can try this :)
    
    <?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>');
    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>');
    ?>
    
    PHP:
     
    xrvel, Apr 21, 2008 IP
  3. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    how can i have the ontop the dta so that all the name appear under the firstname, and last name appear under the lastname etc

    FIRSTNAME LASTNAME EMAIL PHONE
     
    babyphp, Apr 21, 2008 IP
  4. mim24

    mim24 Well-Known Member

    Messages:
    327
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Thank you, its worked for me
     
    mim24, Nov 28, 2012 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    
    
    [FONT=monospace][COLOR=#000000][B]<?php[/B][/COLOR]
    [COLOR=#000088]$search[/COLOR][COLOR=#339933]=[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]"search"[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
    
    [COLOR=#000088]$result[/COLOR] [COLOR=#339933]=[/COLOR] [URL="http://www.php.net/mysql_query"][COLOR=#990000]mysql_query[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#0000ff]"SELECT * FROM contacts WHERE firstname LIKE '%[COLOR=#006699][B]$search[/B][/COLOR]%' OR lastname LIKE '%[COLOR=#006699][B]$search[/B][/COLOR]%' OR email LIKE '%[COLOR=#006699][B]$search[/B][/COLOR]%' OR phone LIKE '%[COLOR=#006699][B]$search[/B][/COLOR]%'"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#ff0000][B]//change the next line to this[/B][/COLOR]
    [COLOR=#b1b100]echo[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]'<table><th><td>FIRSTNAME</td><td>LASTNAME</td><td>EMAIL</td><td>PHONE</td></th>[/COLOR][COLOR=#0000ff]'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#b1b100]while[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$row[/COLOR][COLOR=#339933]=[/COLOR][URL="http://www.php.net/mysql_fetch_array"][COLOR=#990000]mysql_fetch_array[/COLOR][/URL][COLOR=#009900]([/COLOR][COLOR=#000088]$result[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
        [COLOR=#000088]$firstname[/COLOR][COLOR=#339933]=[/COLOR][COLOR=#000088]$row[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]"firstname"[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
        [COLOR=#000088]$lastname[/COLOR][COLOR=#339933]=[/COLOR][COLOR=#000088]$row[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]"lastname"[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
        [COLOR=#000088]$email[/COLOR][COLOR=#339933]=[/COLOR][COLOR=#000088]$row[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]"email"[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
        [COLOR=#000088]$phone[/COLOR][COLOR=#339933]=[/COLOR][COLOR=#000088]$row[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]"phone"[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
        [COLOR=#b1b100]echo[/COLOR] [COLOR=#0000ff]"<tr><td>[COLOR=#006699][B]$firstname[/B][/COLOR]</td><td>[COLOR=#006699][B]$lastname[/B][/COLOR]</td><td>[COLOR=#006699][B]$email[/B][/COLOR]</td><td>[COLOR=#006699][B]$phone[/B][/COLOR]</td></tr>"[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#009900]}[/COLOR]
    [COLOR=#b1b100]echo[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]'</table>'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]?>
    [/B][/COLOR][/FONT]
    Code (markup):

     
    Rukbat, Nov 30, 2012 IP
  6. bigmarvelfan

    bigmarvelfan Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Do you always have to do $firstname = $row['firstname'] and such?
     
    bigmarvelfan, Nov 30, 2012 IP
  7. ott

    ott Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    this is what I use
    <?php
    
    $host="localhost"; // Host name 
    $username="user"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="database"; // Database name 
    $tbl_name="table"; // Table name
    
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
    // select record from mysql 
    $sql="SELECT * FROM $tbl_name ORDER BY firstname ASC";
    $result=mysql_query($sql);
    ?>
    
    
    <table width="1400" border="1" cellpadding="2" cellspacing="2" bgcolor="#CCCCCC">
    <tr>
    <td colspan="8" bgcolor="#FFFFFF"><strong>Lead Manager</strong></td>
    </tr>
    
    
    <tr>
    <td width="17" align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
    <td width="53" align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
    <td width="69" align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
    <td width="40" align="center" bgcolor="#FFFFFF"><strong>Phone</strong></td>
    
    
    </tr>
    
    
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    
    
    <tr>
    <td bgcolor="#FFFFFF"><? echo $rows['firstname']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['phone']; ?></td>
    </tr>
    <?php
    // close while loop 
    }
    ?>
    </table>
    
    
    <?php
    // close connection; 
    mysql_close();
    ?>
    
    
    
    Code (markup):
     
    Last edited: Nov 30, 2012
    ott, Nov 30, 2012 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    As opposed to?

    You can use

    $firstname = $row[0];

    if you prefer. Or you can use

    echo "<tr><td>$row['firstname']</td><td>$row['lastname']</td><td>$row['email']</td><td>$row['phone']</td></tr>";

    I don't know which part you're asking about.
     
    Rukbat, Nov 30, 2012 IP
  9. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #9
    As Rukbat stated, you can and it would be better practice to use:

    instead of

    As it is less of a load on the server.
     
    scottlpool2003, Dec 5, 2012 IP