PHP更改MySQL行顺序

时间:2013-12-27 16:50:22

标签: php mysql

我的菜单中有这个PHP代码,它选择MySQL中的所有链接和名称:

<?php
$url = '';
//select all the top row items
$sql="SELECT * from website_menu where parent_top = '' and parent = '' order by menu_order ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
  //then select all the next rows down (parent_top)
  $current = false;
  $subMenu = '';
  $sql2="SELECT * from website_menu where parent_top = '".$result["sequence"]."' order by menu_order ASC ";
  $rs2=mysql_query($sql2,$conn) or die(mysql_error());
  if(mysql_num_rows($rs2) > 0)
  {
    $subMenu = '<ul>';
    while($result2=mysql_fetch_array($rs2))
    {
        if($_GET["id"] == $result2["link"])
        {
            $current = true;
        }
        ///
        if(substr($result2["link"],0,7) == 'http://')
        {
            $url = '';
        }
        elseif(substr($result2["link"],0,8) == 'https://')
        {
            $url = '';
        }
        else
        {
            $url = $settings["website_url"].'/';
        }
        $subMenu .= '<li><a href="'.$url.''.$result2["link"].'"><span>'.$result2["title"].'</span></a>';
        //
        $sql3="SELECT * from website_menu where parent = '".$result2["sequence"]."' ";
        $rs3=mysql_query($sql3,$conn) or die(mysql_error());
        if(mysql_num_rows($rs3) > 0)
        {
            $subMenu .='<ul>';
            while($result3=mysql_fetch_array($rs3))
            {
                ///
                if(substr($result3["link"],0,7) == 'http://')
                {
                    $url = '';
                }
                elseif(substr($result3["link"],0,8) == 'https://')
                {
                    $url = '';
                }
                else
                {
                    $url = $settings["website_url"].'/';
                }
                $subMenu .='<li><a href="'.$url.''.$result3["link"].'"><span>'.$result3["title"].'</span></a></li>';
                if($_GET["id"] == $result3["link"])
               {
                  $current = true;
               }
            }
            $subMenu .='</ul>';
            $subMenu .='</li>';
        }
        else
        {
            $subMenu .='</li>';
        }
    }
    $subMenu .= '</ul>';
}
    echo '<li';
if($_GET["id"] == $result["link"] || $current)
{
    echo ' class="active"';
}
///
if(substr($result["link"],0,7) == 'http://')
{
    $url = '';
}
elseif(substr($result["link"],0,8) == 'https://')
{
    $url = '';
}
else
{
    $url = $settings["website_url"].'/';
}
echo '><a href="'.$url.''.$result["link"].'"><span>'.$result["title"].'</span></a>', $subMenu, '</li>';
 }
?>

它们都是由名为INT的{​​{1}}列排序,我希望能够通过UP / DOWN链接重新订购

我目前有:

menu_order

但它没有正确重新排序并以正确的方式交换数字

1 个答案:

答案 0 :(得分:2)

不要浪费你的时间这样做,使用链接使用ORDER BY ASC或ORDER BY DESC调用新查询并显示结果。让SQL做所有艰苦的工作。