Hi, i have a big problem trying to make my table sort things..
Table information is cooming from mysql db..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">	
  <head> 
  <title>Min telefonbok</title> 
	<link rel="stylesheet" type="text/css" href="style.css" />
  </head> 

  <body> 
  <h1>Telefonbok</h1> 

  <form id="form" method="post" action="phonebook.php">
     <fieldset> 
       <legend>Skapa ny kontakt</legend>
         <table> 
           <tr>  
           <th>Kontakter</th>
           </tr>
           <tr>
             <td>Förnamn</td>
             <td><input name="fnamn" type="text" id="fnamn" /></td> 
           </tr> 
           <tr>  
             <td>Efternamn</td> 
             <td><input name="enamn" type="text" id="enamn" /></td> 
           </tr>
           <tr>  
             <td>Telefonnummer</td> 
             <td><input name="telefonnummer" type="text" id="telefonnummer" value="" /></td> 
           </tr> 
           <tr>  
             <td>Beskrivning</td> 
             <td><textarea rows="10" cols="30" id="beskrivning" name="beskrivning" >
           </textarea></td> 
           </tr>
           <tr>  
             <td><label for="favorit">Favorit?</label></td> 
             <td><input name="favorit" type="checkbox" id="favorit" value="1" /></td> 
           </tr>
           <tr>
             <td><input name="save" type="submit" id="save" value="Save" /></td> 
             <td><input type="reset" name="reset" value="Reset" /></td> 
           </tr> 
         </table> 
       </fieldset>
  </form> 

<?php
		$dbh=mysql_connect("localhost","xxxxx","xxxxxx") or die (mysql_error());
		mysql_select_db("xxxxxx") or die(mysql_error());

		$enamn = mysql_query("SELECT * FROM contacts ORDER BY lastname");
		
		echo "<table border='1'>";
		echo "<tr> <th>Efternamn</th> <th>Förnamn</th> <th>Beskrivning</th> <th>Telefonnummer</th> <th>Favorit?</th></tr>";
		
		while ($row = mysql_fetch_array($enamn))
		{
			echo "<tr><td>"; 
			echo $row['lastname'];
			echo "</td><td>"; 
			echo $row['firstname'];
			echo "</td><td>"; 
			echo $row['description'];
			echo "</td><td>"; 
			echo $row['phonenumber'];
			echo "</td><td>"; 
			echo $row['favorite'];
			echo "</td></tr>"; 
		}
		echo "</table>";

?>
  </body> 
</html>

Recommended Answers

I have changed your line no 51-54

if($_GET['orderbycol']=="")
		$_GET['orderbycol']="lastname";
	

	$enamn = mysql_query("SELECT * FROM contacts ORDER BY  $_GET['orderbycol']"); 		
	echo "<table border='1'>";		
	echo "<tr> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=lastname'>Efternamn</a> </th> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=firstname'>Förnamn</a> </th>
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=beskcolname'>Beskrivning</a> </th> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=telfocolname'>Telefonnummer</a> </th> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=favcolname'>Favorit?</th></a> </tr>";
Jump to Post

user brace around order by phrase

$enamn = mysql_query("SELECT * FROM contacts ORDER BY {$_GET['orderbycol']}  ");
Jump to Post

All 7 Replies

i can not understand your question. please ask clearly.

I want to be able to click on the titles in the table.. And sort it alphabeticly. But i dont know how to make it that way. ATM it only sorts in "efternamn" (lastname)

I have changed your line no 51-54

if($_GET['orderbycol']=="")
		$_GET['orderbycol']="lastname";
	

	$enamn = mysql_query("SELECT * FROM contacts ORDER BY  $_GET['orderbycol']"); 		
	echo "<table border='1'>";		
	echo "<tr> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=lastname'>Efternamn</a> </th> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=firstname'>Förnamn</a> </th>
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=beskcolname'>Beskrivning</a> </th> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=telfocolname'>Telefonnummer</a> </th> 
	<th><a href='{$_SERVER['PHP_SELF']}?orderbycol=favcolname'>Favorit?</th></a> </tr>";

Getting this if a change the code as you explaned:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/sanstr09/www/lab4/index.php on line 69.

$enamn = mysql_query("SELECT * FROM contacts ORDER BY $_GET");

That row it is complaining about

user brace around order by phrase

$enamn = mysql_query("SELECT * FROM contacts ORDER BY {$_GET['orderbycol']}  ");
Member Avatar

Like the above reply says, you can pull out the info based on the column should be sorted and then display the info in the table. That would be a convenient way r8!

Thank you sooo much all, got it all worked out now :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.