tooepic
tooepic

Reputation: 11

redirecting back to php file

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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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.

Upvotes: 0

Views: 167

Answers (2)

deceze
deceze

Reputation: 522342

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.

Upvotes: 1

Arda Xi
Arda Xi

Reputation: 2666

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.

Upvotes: 0

Related Questions