排序sql查询&amp;每次我从 <th> 进行查询或排序时,ID 都会重置并按数字顺序开始.标签 [英] Sort sql query &amp; ID reset and start in numerical order every time I make a query or sort from &lt;th&gt; tag

查看:40
本文介绍了排序sql查询&amp;每次我从 <th> 进行查询或排序时,ID 都会重置并按数字顺序开始.标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 forms1 的数据库和一个名为 demo
的表表中的字段为IDAutoreTitolocit

I have a database called forms1 and a table named demo
The fields in the table are ID, Autore, Titolo, cit

我希望我有第一行,让我能够对查询检索到的字母数字值进行排序,例如

如何修改我的代码?这是cerca2.php:

I wish I had a first line that gives me the ability to sort alphanumeric values ​​that are retrieved by the query, such as

How can i modify my code? This is 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<img src="images/smilies/biggrin.gif" border="0" alt="">EEPSKYBLUE;}
</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&#8243;>&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());

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

    echo "<tr>";
    echo "<td width='auto' bgcolor="#FF0000 &#8243;>" . "&nbsp;". "<b>"  . $row[0] . "&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();
?> 

当我点击时,我会点击Nome和其他th(ID除外)来重新排列字母数字AZ或ZA的值,反之亦然.目前这是我调用查询时的结果页面(查看上面的代码):

I would click on Nome and other th (except ID) to reorder the values ​​alphanumeric AZ or ZA viceversa when i click. At the moment this is my result page (look code above) when i call a query:

我也愿意 - 但我不知道该怎么做 - ID 值总是从 1 开始,并根据每次我进行查询或排序时检索到的 x 值的数量以数字顺序继续th 标签.
相反,在我的例子中,它总是依赖于它所代表的值.从图中可以看出,数字以随机顺序出现.在这里,我不想要这个.

I would, too - but I do not know how to do - that the ID value always start from 1 and continue in numerical order according to the number of x values ​​retrieved every time I make a query or sort from th tag.
In my case, instead, is always dependent on the value it represents. You see it from the picture that the numbers appear in random order. Here, I do not want this.

推荐答案

试试这个代码部分:

    $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>";

}

这篇关于排序sql查询&amp;每次我从 <th> 进行查询或排序时,ID 都会重置并按数字顺序开始.标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆