2012-10-19 40 views
1

我有一个数据库,称为forms1和一张桌子命名演示
表中的字段是IDAUTORETITOLOCIT排序行 - MySQL的

我希望我有第一行,使我能够排序查询检索的字母数字值。
对于排序我的意思是
How to sort rows of HTML table that are called from MySQL

我的问题是我的情况调整代码。 这是cerca2.php

<style> 
br {margin-bottom:-10px;} 
</style> 

<form action="cerca2.php" method="post"> 
<b>Nome</b>&nbsp;<input type="text" name="Nome">&nbsp;&nbsp; 
<b>Numero&nbsp;</b><input type="text" name="Numero">&nbsp;&nbsp; 
<b>city&nbsp;</b><input type="text" name="city">&nbsp;&nbsp; 
<input type="Submit"> 
</form> 

<style> 
tr:nth-of-type(odd) { background-color: AZURE; } 
tr:nth-of-type(even) { background-color: CYAN; } 
</style> 
<style> 
tr:hover{background-color:DEEPSKYBLUE;} 
</style> 

<?php 

echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>"; 
echo "<tr style='font-weight: bold;'>"; 
echo "<td width='auto' bgcolor=”#7FFFD4″>&nbsp;<i>ID<i/></td>"; 
echo "<td width='auto' >&nbsp;<i>Nome<i/></td>"; 
echo "<td width='auto' ></td>"; 
echo "<td ></td>"; 
echo "</tr>"; 

define('DB_NAME', 'forms1'); 
define('DB_USER', 'root'); 
define('DB_PASSWORD', ''); 
define('DB_HOST', 'localhost'); 

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 

$db_selected = mysql_select_db(DB_NAME, $link); 

if (!$db_selected) { 
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); 
} 

$Nome = str_replace(' ', '%', $_POST['Nome']); 
$Numero = str_replace(' ', '%', $_POST['Numero']); 
$city = str_replace(' ', '%', $_POST['city']); 

$arNome = str_split($Nome); 
$arNumero = str_split($Numero); 
$arcity = str_split($city); 

$Nome=''; 
foreach ($arNome as $value) 
{ 
    if ($value=='%') continue; 
    $Nome.=$value.'%'; 

} 

$Numero=''; 
foreach ($arNumero as $value) 
{ 
    if ($value=='%') continue; 
    $Numero.=$value.'%'; 

} 

$city=''; 
foreach ($arcity as $value) 
{ 
    if ($value=='%') continue; 
    $city.=$value.'%'; 

} 

$sql = mysql_query("SELECT * FROM demo WHERE Autore LIKE '%$Nome%' AND Titolo LIKE '%$Numero%' AND cit LIKE '%$city%' ORDER BY Autore") or die(mysql_error()); 


$i = 0; while($row=mysql_fetch_array($sql)){ 
     $i++; 
     echo "<tr>"; 
     echo "<td width='auto' bgcolor=”#FF0000 &#8243;>" . "&nbsp;". "<b>" . $i . "&nbsp;". "<b/>". "</td>"; 
     echo "<td width='auto'>" . "&nbsp;" . $row[1] . "&nbsp;" . "</td>"; 
     echo "<td width='auto'>". "</td>"; 
     echo "<td width='auto'>" . "&nbsp;". "<i>" . $row[2] . "<i/>". "&nbsp;" . "</td>";  
     echo "<td width='auto'>" . "&nbsp;". "<i>" . $row[3] . "<i/>". "&nbsp;" . "</td>"; 
     echo "</tr>"; 

} 

mysql_close(); 
?> 
+0

你是什么意思“被查询检索排序的字母数字值”是什么意思?什么价值?您是否尝试对查询返回的结果进行排序?你是否试图在查询本身进行排序(你已经通过Autore排序)? – sgroves

+0

**你是否试图在查询本身进行排序**是的,像这样http://stackoverflow.com/questions/3489783/how-to-sort-rows-of-html-table-that-are-called- from-mysql但是如何为我的情况调整代码? – user143822

回答

1

如果你正在寻找一个JavaScript解决方案tablesorter会做这项工作。方便,容易。

编辑
在最顶部添加:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<script type="text/javascript" src="http://tablesorter.com/__jquery.tablesorter.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ $("table").tablesorter(); }); 
</script> 

​​

+1

我尝试,但我不undestand tablesorter,我不知道它必须包括在我的代码。你能帮助我吗? – user143822

+0

@ user143822检查版本。这会很快完成这项工作。你可以看看[jQuery](http://jquery.com)来更好地理解我所做的。 – Touki

+0

这看起来像一个很好的JavaScript解决方案。 @ user143822如果你想在PHP中这样做,这将涉及更多的工作。你会想让你的表头可点击的链接,设置一个$ _GET参数,你可以按照Alexey Sidorov的回答进行排序。 – sgroves

0

如果你想有一个排序表,我用 “sortable” 之称的JavaScript库 - 它很容易配置和使用。

编辑显示例如HTML:此HTML预计“sortable.js”文件是在同一目录中的HTML文件:

<html> 
    <head> 
    <script type="text/javascript" src="sortable.js"></script> 
    </head> 
    <body> 
    <h1>Test Table</h1> 
    <table id="myTable" class="sortable"> 
     <tr> 
     <th>First Column</th> 
     <th>Second Column</th> 
     <th>Third Column</th> 
     </tr> 

     <tr> 
     <td>1</td> 
     <td>Hello</td> 
     <td>10/01/2012</td> 
     </tr> 

     <tr> 
     <td>2</td> 
     <td>Goodbye!</td> 
     <td>10/04/2011</td> 
     </tr> 

     <tr> 
     <td>3</td> 
     <td>Welcome</td> 
     <td>22/09/1985</td> 
     </tr> 
    </table> 
    </body> 
</html> 
+0

我想排序表,如何能适应我的代码? – user143822

+0

阅读我的答案的链接。它逐步说明如何实施解决方案。如果语言是一个问题,我会说意大利语。 – GarethL

+0

也是我在意大利说,你给我个忙吗?我试图解决你自己,那阿列克谢·西多罗夫的,但它改变不了什么 – user143822

1

您应该包括限制对您的SQL查询,所以你必须不仅要对已打印的数据和隐藏的数据进行排序。

在你的情况下,尝试重新加载页面,并使用$ _GET [“name_sort”]改变你的SQL

if ($_GET['name_sort'] == 'desc') { 
    $sql = mysql_query("SELECT * FROM demo WHERE Autore LIKE '%$Nome%' AND Titolo LIKE '%$Numero%' AND cit LIKE '%$city%' ORDER BY Autore DESC") or die(mysql_error()); 

} else { 

    $sql = mysql_query("SELECT * FROM demo WHERE Autore LIKE '%$Nome%' AND Titolo LIKE '%$Numero%' AND cit LIKE '%$city%' ORDER BY Autore ASC") or die(mysql_error()); 
} 
+0

我试图改变,但没有改变。这里看整个代码http://pastebin.com/cMLnqELL – user143822