tags:

views:

43

answers:

2

hello again,

following code is my php file that will list the people in my text file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>viewlist php</title>
</head>
<body>
<h1>List</h1>
<?php
$file = file("peoplelist.txt");
for($i=0; $i<count($file); $i++)
{
  $person = explode(",", $file[$i]);
  echo "<hr />";
  echo "<table cellspacing=10><tr><td>", $i+1,".", "</td>";
  echo "<td>", $person[0], "<br />";
  echo $person[1], "</td></tr></table>";
}
?>
<hr />
<p>
  <a href="sortatoz.php" target="_self">Sort A-Z</a><br />
  <a href="sortztoa.php" target="_self">Sort Z-A</a><br />
</p>
</body>
</html>

what i want to do is, when i click Sort A-Z link, the file called sortatoz.php will sort the list in my text file and redirect back to viewlist.php with the list in sort order. below is my sortatoz.php:

<?php
header("Location: http://myserver/workspace/viewlist.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>sort a to z</title>
</head>
<h1>List</h1>
<body>
<?php
$file = file("peoplelist.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
  $person = explode(",", $file[$i]);
  echo "<hr />";
  echo "<table cellspacing=10><tr><td>", $i+1,".", "</td>";
  echo "<td>", $person[0], "<br />";
  echo $person[1], "</td></tr></table>";
}
?>
<hr />
<p>
  <a href="sortvisitorsascending.php" target="_self">Sort Visitors A-Z</a><br />
  <a href="sortvisitorsdescending.php" target="_self">Sort Visitors Z-A</a><br />
</p>
</body>
</html>

now, when i click Sort A-Z link, it redirects back to viewlist.php...so I'm assuming the header() function is doing it's job.

but the problem is...it's not sorting.

i am very new with this, so bear with me and give me some guidance please. what can i do to my codes to redirect back viewlist.php with sorted list?

thanks in advance.

A: 

Basically, right now, you're doing the sorting right, but because you redirect to viewlist.php immediately none of the code below is executing. Why do you want to, anyway? You could just stay on sortatoz.php without the redirect and it would work perfectly.

Arda Xi
@Brant, some data would be like this:mike,jonesdavid,andersonjohnny,good..and each names are in each line.@Joey Adams, the all function is working fine. It's just when I added that header() function in sortatoz.php, it won't sort.@Arda Xi, I know it works perfectly without redirecting. But I wan't to make the sortatoz.php like a ghost file where the user won't see it....so yeah, if anyone have a solution for this, I would really appreciate the help. Thanks.
tooepic
In that case, read deceze's answer.
Arda Xi
+1  A: 
header("Location: http://myserver/workspace/viewlist.php");

sends an HTTP header that causes the browser to redirect. The code below is executing alright, but since the browser is redirecting to another page, the user won't see the result. On the site you're redirecting to, it doesn't matter your sortatoz.php page does, it's a different page.

Also, sortatoz.php doesn't do anything of "lasting value". It just reads the file's contents, sorts these contents in memory, then outputs them. It doesn't write the sorted entries back to the file, as you may think.

Since the code on both pages is virtually the same, you'd rather just send a variable in the URL and act on it.

if (isset($_GET['sort']) && $_GET['sort'] == 'asc') {
    sort($file);
}

And link to the site like viewlist.php?sort=asc.

deceze
thanks, if i understand correctly, i need to change the code in sortatoz.php to sort and then use fwrite function to rewrite the text file in sorted order for the viewlist.php to display in sorted order. thanks.
tooepic
@tooepic Yes, either that, or you simply leave the file as is and sort dynamically whenever you display the file. This depends on how big the file is, whether its more economical to sort it every time or just once, whether you actually want to store the data sorted etc. You certainly should **NOT** rewrite the file very time you want to display the data in another sort order. Storing data and displaying it are two separate things. If you'd use a proper database, sorting times become an absolute non-issue.
deceze