Hi everyone,

I hope all are fine.

Is there have anyone who knows about shorting a table whose containers are uploaded from the database.

Please help:icon_eek:

first of all, what is shorting a table ? secondly, what are containers ?
Sorry for my ignorance, I didn't understand your question.

thank you for responding.

Firstly,
there have a table of column:
<table><tr >
<th> ID</th>
<th> Name</th>
<th> Title</th>
<th>Option</th>
</tr></table>
this table data are uploaded from database.

I need to sort them.

Just like in phpmyadmin table data shorting method.

When click the id pass the value url sort=id
and change the query dynamically

thanks a lot

Ok..this might look a lil complicated, but it works.

<?php
$conn=mysql_connect($host,$user,$pass);
mysql_select_db($db);
if(isset($_GET['col']) && isset($_GET['order'])){
	$column=$_GET['col'];
	$order=$_REQUEST['order'];
	if($column == "blah1"){
		if($order=="asc"){ $order="desc"; } else { $order="asc"; }
		$value1="?col=blah1&order=$order";
		$query="select col1,col2,col3 from table order by $column $order";
	}
	if($column == "blah2"){
		if($order=="asc"){ $order="desc"; } else { $order="asc"; }
		$value2="?col=blah2&order=$order";
		$query="select col1,col2,col3 from table order by $column $order";
	}
	if($column == "blah3"){
		if($order=="asc"){ $order="desc"; } else { $order="asc"; }
		$value3="?col=blah3&order=$order";
		$query="select col1,col2,col3 from table order by $column $order";
	}
} else {
	$value1="?col=blah1&order=asc";
	$value2="?col=blah2&order=asc";
	$value3="?col=blah3&order=asc";
	$query="select col1,col2,col3 from table";
}
$result=mysql_query($query);
?>
<html>
<body>
<table border=1>
<th><a href="testtab.php<?php echo $value1 ?>">blah1</th><th><a href="testtab.php<?php echo $value2 ?>">blah2</th><th><a href="testtab.php<?php echo $value3 ?>">blah3</th>
<?php
while($row=mysql_fetch_array($result)){
	print "<tr><td>".$row['col1']."</td><td>".$row['col2']."</td><td>".$row['col3']."</td></tr>";
}
?>
</table>
</body>
</html>

Hope it helps.
Cheers,
Naveen

nav33n,
Sorry to bug you with this, but I was playing with your script and adding some stuff to suite my script and am having a few problems. Can you check what I've done here to see where i'm going wrong?

<?php 

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

// create query 
$conn=mysql_connect($host,$user,$pass);
mysql_select_db($db);
if(isset($_GET['col']) && isset($_GET['order'])){
	$column=$_GET['col'];
	$order=$_REQUEST['order'];
	if($column == "fname"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value1="?col=fname&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
	}
	if($column == "lname"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value2="?col=lname&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
	}
	if($column == "phone_num"){
		if($order=="asc"){ $order="desc"; } else { 
$order="asc"; }
		$value3="?col=phone_num&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
	}
	if($column == "ext"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value4="?col=ext&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
	}
	if($column == "title"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value5="?col=title&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
		}
	if($column == "dept"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value6="?col=dept&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
		}
	if($column == "fax"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value7="?col=fax&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table order by $column $order";
} else {
		$value1="?col=fname&order=asc";
		$value2="?col=lname&order=asc";
		$value3="?col=phone_num&order=asc";
		$value4="?col=ext&order=$order";
		$value5="?col=title&order=$order";
		$value6="?col=dept&order=$order";
		$value7="?col=fax&order=$order";
		$query="select col1,col2,col3,col4,col5,col6,col7 from table";
		}
[B]}  //I had to add this one or I got an invalid $end error[/B]
$result=mysql_query($query);
?>

<html>
<body>
<table border=1>
<th><a href="testtab.php<?php echo $value1 ?>">First Name</th>
<th><a href="testtab.php<?php echo $value2 ?>">Last Name</th>
<th><a href="testtab.php<?php echo $value3 ?>">Phone Number</th>
<th><a href="testtab.php<?php echo $value4 ?>">Extension</th>
<th><a href="testtab.php<?php echo $value5 ?>">Title</th>
<th><a href="testtab.php<?php echo $value6 ?>">Department</th>
<th><a href="testtab.php<?php echo $value7 ?>">Fax Number</th>
<?php
while($row=mysql_fetch_array($result)){
	print "<tr>
	<td>".$row['col1']."</td>
	<td>".$row['col2']."</td>
	<td>".$row['col3']."</td>
	<td>".$row['col4']."</td>
	<td>".$row['col5']."</td>
	<td>".$row['col6']."</td>
	<td>".$row['col7']."</td>
	</tr>";
}
?>

</body>
</html>

Here is the error i'm getting, which is not making sense to me.

Notice: Undefined variable: query in C:\Inetpub\wwwroot\dbphone_new.php on line 86

It gives my my column headings as links:
First Name Last Name Phone Number Extension Title Department Fax Number
but then i get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\dbphone_new.php on line 100

Can you point me in the right direction?

Thanks.

Try this.

<?php 

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

// create query 
$conn=mysql_connect($host,$user,$pass);
mysql_select_db($db);
if(isset($_GET['col']) && isset($_GET['order'])) {
	$column=$_GET['col'];
	$order=$_REQUEST['order'];
	if($column == "fname"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value1="?col=fname&order=$order";
		$query="select * from orders order by $column $order";
	}
	if($column == "lname"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value2="?col=lname&order=$order";
		$query="select * from orders order by $column $order";
	}
	if($column == "phone_num"){
		if($order=="asc"){ $order="desc"; } else { 
$order="asc"; }
		$value3="?col=phone_num&order=$order";
		$query="select * from orders order by $column $order";
	}
	if($column == "ext"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value4="?col=ext&order=$order";
		$query="select * from orders order by $column $order";
	}
	if($column == "title"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value5="?col=title&order=$order";
		$query="select * from orders order by $column $order";
		}
	if($column == "dept"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value6="?col=dept&order=$order";
		$query="select * from orders order by $column $order";
		}
	if($column == "fax"){
		if($order=="asc"){ $order="desc"; } else {
$order="asc"; }
		$value7="?col=fax&order=$order";
		$query="select * from orders order by $column $order";
} 
} else {
		$value1="?col=fname&order=asc";
		$value2="?col=lname&order=asc";
		$value3="?col=phone_num&order=asc";
		$value4="?col=ext&order=asc";
		$value5="?col=title&order=asc";
		$value6="?col=dept&order=asc";
		$value7="?col=fax&order=asc";
		$query="select * from orders";
}  //I had to add this one or I got an invalid $end error
$result=mysql_query($query);
?>

<html>
<body>
<table border=1>
<th><a href="sort.php<?php echo $value1 ?>">First Name</th>
<th><a href="sort.php<?php echo $value2 ?>">Last Name</th>
<th><a href="sort.php<?php echo $value3 ?>">Phone Number</th>
<th><a href="sort.php<?php echo $value4 ?>">Extension</th>
<th><a href="sort.php<?php echo $value5 ?>">Title</th>
<th><a href="sort.php<?php echo $value6 ?>">Department</th>
<th><a href="sort.php<?php echo $value7 ?>">Fax Number</th>
<?php
while($row=mysql_fetch_array($result)){
	print "<tr>
	<td>".$row['col1']."</td>
	<td>".$row['col2']."</td>
	<td>".$row['col3']."</td>
	<td>".$row['col4']."</td>
	<td>".$row['col5']."</td>
	<td>".$row['col6']."</td>
	<td>".$row['col7']."</td>
	</tr>";
}
?>

</body>
</html>

Now I'm getting this over and over:

Notice: Undefined index: col1 in C:\Inetpub\wwwroot\dbphone_new.php on line 92
Notice: Undefined index: col2 in C:\Inetpub\wwwroot\dbphone_new.php on line 93
Notice: Undefined index: col3 in C:\Inetpub\wwwroot\dbphone_new.php on line 94
Notice: Undefined index: col4 in C:\Inetpub\wwwroot\dbphone_new.php on line 95
Notice: Undefined index: col5 in C:\Inetpub\wwwroot\dbphone_new.php on line 96
Notice: Undefined index: col6 in C:\Inetpub\wwwroot\dbphone_new.php on line 97
Notice: Undefined index: col7 in C:\Inetpub\wwwroot\dbphone_new.php on line 98

for this area:

while($row=mysql_fetch_array($result)){
print "<tr>
<td>".$row['col1']."</td>
<td>".$row['col2']."</td>
<td>".$row['col3']."</td>
<td>".$row['col4']."</td>
<td>".$row['col5']."</td>
<td>".$row['col6']."</td>
<td>".$row['col7']."</td>
</tr>";
}
?>

Note: Enabling E_NOTICE during development has some benefits. For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr since PHP tries to treat "item" as constant. If it is not a constant, PHP assumes it is a string index for the array.

Source : http://nl3.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

Btw, notices can be ignored. Check if col1, col2,... col7 exists in the table !

I'm assuming you are wanting me to check my error reporting. Whether I have it on or off, I don't get data presented from the database, just the errors with the headers(reporting on), or just the headers (reporting off).

What am I not seeing here?

thanks.

Well, notices can be ignored. But if you want to get rid of notices without disabling it, initialize every variable before using it. Here is an example.

<?php
error_reporting(E_ALL);
$arr = array();
echo $arr['1'];
?>

This will generate the notice undefined index since $arr doesn't have any value and isn't initialized where as, this wouldn't generate any notice.

<?php
error_reporting(E_ALL);
$arr = array();
$arr['1']="";
echo $arr['1'];
?>

Ok, I'm still playing with it and have gotten the data to display. When I click on my column headings to sort by that heading, I get a "page can not be found" error.

Dude! check the hyperlink.

I was able to get my data by using $row instead of col, would the sorting work better using row instead of column? Heres the code I've used so far:

<?php

$host = "localhost";
$user = "root";
$pass = "xx";
$db = "phonebook";
// create query
$conn=mysql_connect($host,$user,$pass);

mysql_select_db($db);

if(isset($_GET['col']) && isset($_GET['order'])) {
$column=$_GET['col'];
$order=$_REQUEST['order'];

if($column == "fname"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value1="?col=fname&order=$order";
	$query="select * from people order by $column $order";
}
if($column == "lname"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value2="?col=lname&order=$order";
	$query="select * from people order by $column $order";
}
if($column == "phone_num"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value3="?col=phone_num&order=$order";
	$query="select * from people order by $column $order";
}
if($column == "ext"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value4="?col=ext&order=$order";
	$query="select * from people order by $column $order";
}
if($column == "title"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value5="?col=title&order=$order";
	$query="select * from people order by $column $order";
}
if($column == "dept"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value6="?col=dept&order=$order";
	$query="select * from orders order by $column $order";
}
if($column == "fax"){
	if($order=="asc"){ $order="desc"; } else {
	$order="asc"; }
	$value7="?col=fax&order=$order";
	$query="select * from people order by $column $order";
}

} else {
	
	$value1="?col=fname&order=asc";
	$value2="?col=lname&order=asc";
	$value3="?col=phone_num&order=asc";
	$value4="?col=ext&order=asc";
	$value5="?col=title&order=asc";
	$value6="?col=dept&order=asc";
	$value7="?col=fax&order=asc";
	$query="select * from people";
	
}

$result=mysql_query($query);

?>

<html>
<body>
<table border=1>
<th><a href="sort.php<?php echo $value1 ?>">First Name</th>
<th><a href="sort.php<?php echo $value2 ?>">Last Name</th>
<th><a href="sort.php<?php echo $value3 ?>">Phone Number</th>
<th><a href="sort.php<?php echo $value4 ?>">Extension</th>
<th><a href="sort.php<?php echo $value5 ?>">Title</th>
<th><a href="sort.php<?php echo $value6 ?>">Department</th>
<th><a href="sort.php<?php echo $value7 ?>">Fax Number</th>
</table>

</body>
</html>

<?php

while($row = mysql_fetch_row($result)) {
	 	echo "<table cellpadding=10 border=0>";
	    echo "<tr>"; 
        echo "<td><font size='2'>".$row[1]."</td>"; 
        echo "<td><font size='2'>".$row[2]."</td>"; 
        echo "<td><font size='2'>".$row[3]."</td>";
        echo "<td><font size='2'>".$row[4]."</td>";
        echo "<td><font size='2'>".$row[5]."</td>";
        echo "<td><font size='2'>".$row[6]."</td>";
        echo "<td><font size='2'>".$row[7]."</td>";
        
        echo "</tr>";
}		
echo "</table>";

?>

I may be way off but if my $value was based on $row instead of ?col would I get a better result? My while statement worked with the $row statement. The hyperlink looks ok to me.

umm.. col is the column name which you want to sort.. Is it working for you ?

umm.. col is the column name which you want to sort.. Is it working for you ?

No its not working for me. I'm still getting a "page can not be found" error. :(

What have you named this script ? Is it called sort.php ?

What have you named this script ? Is it called sort.php ?

Ultimately it will be called "dbphone.php" but right now it is "dbphone_new.php" because its under construction :)

Then the links should be dbphone_new.php in all these cases.

<th><a href="sort.php<?php echo $value1 ?>">First Name</th>

Then the links should be dbphone_new.php in all these cases.

AArggg, wish i'd seen that..... that got the first column sorting fine, click once, sorts dsc, click again, sorts asc. The rest of the columns get the page error as before.

:) Did you change all the column links ?

Yes I changed them all. I've got a couple of different configurations going, one of them i went back to part of the original script you provided and it is working a little better. What it does now is if I go to sort.php (http://localhost/sort.php) i get column headings and data. If I click on "First Name" (colomn 1) it sorts perfectly like it is supposed to (which gives a page ref of: http://localhost/sort.php?col=fname&order=desc). If I click on any other column heading, result: page error. If I go back to the original page (http://localhost/sort.php), I can then click any column heading and it will work, but only that column heading, all others will error if clicked.
Does this make sense?

:-/ Can you post your latest code ?

Here is my latest code. Its named sort.php just to keep things simple. Data displays good but the sorting errors after the first column is selected.

<html>
<body>

<?php
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "xxx";
$db = "phonebook";
// create query
$conn=mysql_connect($host,$user,$pass);
mysql_select_db($db);
if(isset($_GET['col']) && isset($_GET['order'])) {
$column=$_GET['col'];
$order=$_REQUEST['order'];
    if($column == "fname"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value1="?col=fname&order=$order";
        $query="select * from people order by $column $order";
    }
    if($column == "lname"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value2="?col=lname&order=$order";
        $query="select * from people order by $column $order";
    }
    if($column == "phone_num"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value3="?col=phone_num&order=$order";
        $query="select * from people order by $column $order";
    }
    if($column == "ext"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value4="?col=ext&order=$order";
        $query="select * from people order by $column $order";
    }
    if($column == "title"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value5="?col=title&order=$order";
        $query="select * from people order by $column $order";
    }
    if($column == "dept"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value6="?col=dept&order=$order";
        $query="select * from people order by $column $order";
    }
    if($column == "fax"){
        if($order=="asc"){ $order="desc"; } else {
        $order="asc"; }
        $value7="?col=fax&order=$order";
        $query="select * from people order by $column $order";
    } 

}else {

    $value1="?col=fname&order=asc";
    $value2="?col=lname&order=asc";
    $value3="?col=phone_num&order=asc";
    $value4="?col=ext&order=asc";
    $value5="?col=title&order=asc";
    $value6="?col=dept&order=asc";
    $value7="?col=fax&order=asc";
    $query="SELECT * FROM people";
}
$result=mysql_query($query);

?>

<table border=1>
<th><a href="sort.php<?php echo $value1 ?>">First Name</th>
<th><a href="sort.php<?php echo $value2 ?>">Last Name</th>
<th><a href="sort.php<?php echo $value3 ?>">Phone Number</th>
<th><a href="sort.php<?php echo $value4 ?>">Extension</th>
<th><a href="sort.php<?php echo $value5 ?>">Title</th>
<th><a href="sort.php<?php echo $value6 ?>">Department</th>
<th><a href="sort.php<?php echo $value7 ?>">Fax Number</th>

<?php
while($row=mysql_fetch_row($result)){
print "<tr>
<td>".$row[1]."</td>
<td>".$row[2]."</td>
<td>".$row[3]."</td>
<td>".$row[4]."</td>
<td>".$row[5]."</td>
<td>".$row[6]."</td>
<td>".$row[7]."</td>
</tr>";
}

?>
</table>
</body>
</html>

Looks ok to me. I don't understand. What error are you getting ? What do you mean by sorting errors ?

Ok, I'll try to explain. When I open sort.php I see headings and listed data. I click on the first column heading (First Name) and it sorts by first name asc. I click on the First Name heading again, it sorts by first name dsc, working perfectly for that column. If I then click on the second column heading (Last Name), I get a "page cannot be found" page.

If I close the page and start over, opening sort.php again, and instead of clicking on the first column, I click on the second column first (Last name), it sorts correctly until I click on a different column heading, then I get the "page cannot be found" page.

When sort.php first opens, the heading links display on mouse over:

http://sort.php?col=fname&order=asc
http://sort.php?col=lname&order=asc
http://sort.php?col=phone_number&order=asc
etc...........

But once a column heading is clicked to sort, all other headings say on mouse over:


http://localhost/sort.php<br/><b>Notice</b>:Undefined variable:value2 in <b>C:/Inetpub/wwwroot/sort.php</b>on line<b>76</b><br/>

So I guess in a nutshell, once a column heading is clicked, all the other column headings will show an Undefined variable, they will not sort and error out.

Does this make sense? Sorry if it doesn't. Could it be that the page is not refreshing back to original page once a column is clicked? Just guessing here.

huh.. Thats strange.. Disable notices and try again.. The page is refreshed since its reloaded again. Strange.

Ahhhhh, disabling notices did the trick, no more errors. The only thing its doing now (which I can live with) is once I click on a different heading, it takes two clicks to start the sort. It looks like the first click gives the focus, the second click does the sort. It does this anytime I change the column. Its deffinately something I can live with but I did notice thats what it was doing.

Thanks for all your hard work, you are patient and dedicated!!!!! :)

Heh.. so, notices were the culprit here ? eh ? But, this code isn't really "brilliant". But it works. You can tune it a little more!
Umm.. It shouldn't take 2 clicks to sort. Because, first time, no records will be sorted. When you click on a column, it will see if $order is set. If its set to asc, it will change it to desc and vice versa. So, it has to do its work anyway. Strange ! :-/ But I am glad its atleast working !

Be a part of the DaniWeb community

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