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>
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:
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
[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):
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):
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.
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.