php dosyasına geri yönlendirme

2 Cevap

Aşağıdaki kod benim metin dosyasına insanları listeler benim php dosyasıdır.

<!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>

i sırala AZ linki tıkladığınızda artık, bu yüzden) işlevi bu kadar iş yapıyor (başlık varsayarak yaşıyorum ... viewlist.php geri yönlendirir.

ama sorun sıralama değil ... değildir.

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?

şimdiden teşekkürler.

2 Cevap

header("Location: http://myserver/workspace/viewlist.php");

yönlendirmek için tarayıcı neden bir HTTP başlığı gönderir. Aşağıdaki kodu tamam yürütülürken, ancak tarayıcı başka bir sayfaya yönlendirme olduğundan, kullanıcı sonucunu görmek olmaz. Eğer yönlendiriliyorsunuz sitesinde, o farklı bir sayfası, sizin sortatoz.php sayfa farketmez.

Ayrıca, sortatoz.php "kalıcı değer" bir şey yapmaz. Bu sadece, dosyanın içeriğini okur belleğinde bu içerikleri sıralar, sonra bunları çıktılar. Düşündüğünüz gibi, geri dosyaya sıralanmış girdileri yazmaz.

Iki sayfada kodu neredeyse aynı olduğundan, yerine sadece URL bir değişkeni göndermek ve bu hareket ediyorum.

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

Ve gibi siteye link viewlist.php?sort=asc.

Temel olarak, şu anda, sıralama hakkı yapıyoruz, ama hemen kod hiçbiri viewlist.php yönlendirmek çünkü aşağıda yürütüyor. Her neyse, neden istiyorsun? Sadece yönlendirme olmadan sortatoz.php kalmak olabilir ve mükemmel çalışır.