Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2013, 10:26 PM   PM User | #1
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 99
Thanks: 77
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
Trying to create a hyperlink

I am pulling data from a database and displaying it in a simple table.
My goal is to figure out how to be able to click on one of the 2 table headers, and make the page reload, sorting on the column selected.

I am first of all trying to figure out to make the headers into "hyperlinks". I cannot figure it out.

Here is my code, and I would appreciate any help or advice. Thank you.

Code:
        // get results from database 
        $result = mysql_query("SELECT * FROM mytable ")  
                or die(mysql_error());   
                 
        // display data in table                
        echo "<table border='1' cellpadding='10'>"; 
        echo "<tr><th>Last Name</th><th>First Name</th></tr>"; 

        // loop through results of database query, displaying them in the table 
       	$count= 0;
	    while($row = mysql_fetch_array( $result )) { 
        ++$count;
		
	  
	  // echo out the contents of each row into a table 
	             echo "<tr>";
				 echo '<td width="100">' . $row['nameLast'] . '</td>';
				 echo '<td width="100">' . $row['nameFirst'] . '</td>';
                 echo "</tr>";   
        }  

        echo "</table>"; 
?>
Eggweezer is offline   Reply With Quote
Old 02-03-2013, 03:45 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, this is really 90% a PHP question and only 10% MySQL.

And I don't use PHP. So bear with me if I make a couple of PHP typos.
Code:
<!-- don't use PHP to write out large blocks of HTML -->
<!-- makes it harder to read, harder to write, and tiny bit slower -->
<table border="1"' cellpadding="10">; 
<tr>
    <th><a href="thispage.php?by=nameLast">Last Name</a></th>
    <th><a href="thispage.php?by=nameFirst">First Name</a></th>
</tr>
<?php
// make you db connection here ...

$orderby = $_GET["by"];
if ( ! isset($orderby) || ( $orderby != "nameLast" && $orderby != "nameFirst" ) ) 
{
    $orderby = "nameLast"; // or whatever default you choose
}

// get results from database 
$result = mysql_query("SELECT * FROM mytable ORDER BY $orderby ")  
                or die(mysql_error());   

... rest the same ...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Eggweezer (02-03-2013)
Old 02-03-2013, 01:53 PM   PM User | #3
Eggweezer
New Coder

 
Join Date: May 2012
Posts: 99
Thanks: 77
Thanked 0 Times in 0 Posts
Eggweezer is an unknown quantity at this point
Thanks Old Pendant. I appreciate your taking the time. Makes perfect sense once someone shows you!
Eggweezer is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:29 PM.


Advertisement
Log in to turn off these ads.