RSS Forums RSS

php query headers

Thread Solved
Reply
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

php query headers

  #1  
May 6th, 2008
Hey All,

I have a simple query form that displays from a mysql database, a list of names and phone numbers. What i'd like is a column heading at the top of the page but I can't seem to figure out how to put it in there. Here is my code so far:
<?php 

// set database server access variables: 
$host = "localhost"; 
$user = "user"; 
$pass = "password"; 
$db = "phonebook"; 

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

mysql_select_db($db) or die ("Unable to select database!"); 

$query = "SELECT * FROM people ORDER BY fname ASC"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table cellpadding=10 border=0>"; 
    while($row = mysql_fetch_row($result)) {
	    echo "<tr>"; 
        echo "<td>".$row[1]."</td>"; 
        echo "<td>".$row[2]."</td>"; 
        echo "<td>".$row[3]."</td>";
        echo "<td>".$row[4]."</td>";
        echo "<td>".$row[5]."</td>";
        echo "<td>".$row[6]."</td>";
        echo "<td>".$row[7]."</td>";
        echo "</tr>"; 
    } 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?> 
I don't want to use the actual table names because they are abbreviated, ie fname = First Name. But I do want to let the viewer know what each of the columns are. How can I do this.

Thanks.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 71
Reputation: amigura is an unknown quantity at this point 
Solved Threads: 7
amigura's Avatar
amigura amigura is offline Offline
Junior Poster in Training

Re: php query headers

  #2  
May 6th, 2008
guessing this is wat you mean. fill in missing names.

    echo "<table cellpadding=10 border=0>
<tr><td>first name</td><td>last name</td><td>a</td><td>b</td><td>c</td><td></td><td>d</td>"; 
    while($row = mysql_fetch_row($result)) {
	    echo "<tr> 
        <td>".$row[fname]."</td>
        <td>".$row[lname]."</td> 
        <td>".$row[3]."</td>
        <td>".$row[4]."</td>
        <td>".$row[5]."</td>
        <td>".$row[6]."</td>
        <td>".$row[7]."</td>
        </tr>"; 
    } 
    echo "</table>"; 
Reply With Quote  
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: php query headers

  #3  
May 6th, 2008
Ok, i think I answered my own question, someone please tell me if this was the wrong way to do this, but it works.
// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 

// create query 
$query = "SELECT * FROM people ORDER BY fname ASC"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table cellpadding=10 border=0>";
//I ADDED THE ECHO'S BELOW TO CREATE MY TABLE HEADINGS
//Before the "While" statement displays the headings just once at the top of the page.
    echo "<tr>";
    echo "<td><strong>First Name</strong></td>"; 
    echo "<td><strong>Last Name</strong></td>";
    echo "<td><strong>Phone Number</strong></td>";
    echo "<td><strong>Extension</strong></td>";
    echo "<td><strong>Title</strong></td>";
    echo "<td><strong>Department</strong></td>";
    echo "<td><strong>Fax Number</strong></td>";
    echo "</td>";
    while($row = mysql_fetch_row($result)) {
	    echo "<tr>"; 
        echo "<td>".$row[1]."</td>"; 
        echo "<td>".$row[2]."</td>"; 
        echo "<td>".$row[3]."</td>";
        echo "<td>".$row[4]."</td>";
        echo "<td>".$row[5]."</td>";
        echo "<td>".$row[6]."</td>";
        echo "<td>".$row[7]."</td>";
        echo "</tr>"; 
    } 
    echo "</table>"; 
} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?> 
I added my heading tags between my open table tag and my while statement. This displays the headings only once at the top of the page. This solved my problem, but is it the correct way to do this. Pointers would be appreciated.

Thanks,
Reply With Quote  
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: php query headers

  #4  
May 6th, 2008
Thanks Amigura, yes this is what i was looking for. It didn't occur to me at first that I had to put them in above the while statement. Thanks for the quick reply.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 701 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:16 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC