使用$ _get ['sort']排序表,错误意外T_STRING

时间:2013-05-18 09:28:03

标签: php html sorting

我试图通过使用$ _get ['sort']函数点击表格来排序表格

这是我的代码

<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");

//connect to database
//select the database
mysql_select_db("fak_databases");

//submit button
if($_POST['formSubmit'] == "Submit")
{
    $country = $_POST['country'];
}

//query the database
if($country == TRUE) {  
    // query to get all AL records  
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='$country'");  
    if ($_GET['sort'] == 'city')
    {
        $query .= " ORDER BY wipo_applicant1_city";
    }
    elseif ($_GET['sort'] == 'address')
    {
        $query .= " ORDER BY applicant1_addr1";
    }
}  

//fetch the result
Print "<table border cellpadding=3>";
Print "<tr>";
Print "<th><a href="showDB.php?sort=city">City</a></th>";
Print "<th><a href="showDB.php?sort=address">Address</a></th>";
Print "</tr>";

while($row = mysql_fetch_array($query))
{

    Print "<tr>";
    Print "<td>".$row['wipo_applicant1_city'] . "</td>"; 
    Print "<td>".$row['applicant1_addr1'] . " </td></tr>"; 
}

Print "</table>";
?>

但我收到了错误

syntax error, unexpected T_STRING

该行

Print "<th><a href="showDB.php?sort=city">City</a></th>";

任何人都可以解决这个问题吗?

我需要能够点击头表并按字母顺序排序......

我有一个问题,是$ _get ['sort']函数只排序升序??还是升序和降序?如果只是升序排序,那么当我点击表头时,是否可以按升序和降序排序?

感谢

3 个答案:

答案 0 :(得分:1)

原因是你没有转义你的双引号,因此PHP结束了你的字符串中遇到第二个双引号的字符串

Print "<th><a href=\"showDB.php?sort=city\">City</a></th>";
                ---^---               ---^---     

或者在字符串周围使用'引号,你可以在这里安全地使用它,因为你的字符串中没有任何变量

print '<th><a href="showDB.php?sort=city">City</a></th>';

我建议你,不要使用PHP打印HTML,只需将它们分开就像

<table border cellpadding=3>
  <tr>
     <th><a href="showDB.php?sort=city">City</a></th>
     <th><a href="showDB.php?sort=address">Address</a></th>
  </tr>
<?php
  //PHP Code Here
?>
<!-- HTML Here Again -->

答案 1 :(得分:0)

您不能使用双引号。它应该是:

Print '<th><a href="showDB.php?sort=city">City</a></th>';

答案 2 :(得分:0)

试试吧

   <?php
  //connect to server
   $connect = mysql_connect("localhost", "root", "");

 //connect to database
 //select the database
  mysql_select_db("fak_databases");

 //submit button
   if($_POST['formSubmit'] == "Submit")
   {
  $country = $_POST['country'];
   }

  //query the database
   if($country == TRUE) {  
 // query to get all AL records  
     $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE    applicant1_country='$country'");  
if ($_GET['sort'] == 'city')
{
    $query .= " ORDER BY wipo_applicant1_city";
}
elseif ($_GET['sort'] == 'address')
{
    $query .= " ORDER BY applicant1_addr1";
}
 }  

//fetch the result
   Print "<table border cellpadding=3>";
  Print "<tr>";
  Print '<th><a href="showDB.php?sort=city">City</a></th>';
  Print '<th><a href="showDB.php?sort=address">Address</a></th>';
  Print "</tr>";

  while($row = mysql_fetch_array($query))
  {

Print "<tr>";
Print "<td>".$row['wipo_applicant1_city'] . "</td>"; 
Print "<td>".$row['applicant1_addr1'] . " </td></tr>"; 
  }

Print "</table>";
 ?>