Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多个查询时按字母顺序排序?_Php_Mysql_Database - Fatal编程技术网

Php 多个查询时按字母顺序排序?

Php 多个查询时按字母顺序排序?,php,mysql,database,Php,Mysql,Database,在将数据结果与已将项目放入其“储物柜”的用户进行匹配时,我很难按字母顺序对数据结果进行排序 我有两个疑问;第一个查询在数据库中搜索用户放在“储物柜”中的所有物品,第二个查询提取物品的详细信息,并将其排序到物品的品牌列表中 我觉得有更好的方法可以做到这一点,而不是强制页面为每个项目运行一次查询,但我不确定以最有效的方式编写mySQL的正确方法 我认为解决方案是将所有ID作为一个数组,然后在第二个查询中搜索并排序所有相关品牌 我目前有: //$lockerid is pulled earli

在将数据结果与已将项目放入其“储物柜”的用户进行匹配时,我很难按字母顺序对数据结果进行排序

我有两个疑问;第一个查询在数据库中搜索用户放在“储物柜”中的所有物品,第二个查询提取物品的详细信息,并将其排序到物品的品牌列表中

我觉得有更好的方法可以做到这一点,而不是强制页面为每个项目运行一次查询,但我不确定以最有效的方式编写mySQL的正确方法

我认为解决方案是将所有ID作为一个数组,然后在第二个查询中搜索并排序所有相关品牌

我目前有:

    //$lockerid is pulled earlier in the code based on which locker number is associated with this user

    // Pull all of the items and their ids that are in this users locker
    $userlockerquery= mysql_query("SELECT DISTINCT item_id FROM lockers WHERE user_id = '$profile_userid' AND locker_id ='$lockerid' ");

    while($lockeritems=mysql_fetch_array($userlockerquery)){
            $indi_item=$lockeritems[item_id];
            $lockeritemdetails = mysql_query("SELECT DISTINCT brand FROM inventory WHERE id = '$indi_item' ");
            $brands=mysql_fetch_array($lockeritemdetails );
            $brandname=$brands[brand];
            echo '<div>'.$brandname.'</div>';
    }
/$lockerid在代码中的前面部分根据与此用户关联的储物柜编号进行提取
//提取此用户储物柜中的所有项目及其ID
$userlockerquery=mysql\u query(“从储物柜中选择不同的项目\u id,其中用户\u id='$profile\u userid'和储物柜\u id='$lockerid'”);
而($lockeritems=mysql\u fetch\u数组($userlockerquery)){
$indi_item=$lockeritems[item_id];
$lockeritemdetails=mysql_查询(“从库存中选择不同的品牌,其中id='$indi_item'”);
$brands=mysql\u fetch\u数组($lockeritemdetails);
$brandname=$brands[品牌];
回显“.$brandname.”;
}
虽然所有品牌的结果都会显示出来,但我的问题似乎是,由于每个项目id都会运行一次查询,因此列表结果不能相互通信,因此不能按ASC字母顺序排列,因为每个项目都会运行一次查询

也正因为如此,DISTINCT标志没有任何效果,因为它与任何其他结果都不匹配

例如,我的结果将按ID而不是品牌的顺序以div返回,并重复:

耐克 彪马 彪马 匡威

而不是

匡威 耐克 美洲狮


将orderby标志添加到第二个查询中没有任何帮助,因此我想在这里尝试询问一些想法。如果需要任何其他细节,请告诉我

也许可以试试这门课。看看它是否能满足你的需要。如果不尝试
sql
查询,就很难检查它,但是如果我写得正确,它应该可以工作

class MyLocker
        {
            // Protected means that you can't use this variable outside of the functions/class
            // so you can not use $myLocker->_array; It will throw an error
            protected $_array;
            // Construct is basically used as an auto-function. It will execute automatically
            // when you create a new instance of the class so as soon as you do this:
            // $myLocker = new MyLocker($_locker); you initiate the __construct
            // When you label as public, you allow it to be used outside of itself
            public function __construct($_array)
                {
                    // When you set this variable, it is now open to use in all
                    // other functions in this class.
                    $this->_array = $_array;
                }

            // This is the method that will do everything
            public  function LockerContents()
                {
                    // Loop through query. Since the $_array was set in the __construct
                    // it is available in this function as $this->_array
                    while($lockeritems = mysql_fetch_array($this->_array)){
                            // $brand is something we want to use in other functions but not
                            // outside the class so it is set here for use in the Fetch() function
                            $this->brand    =   $lockeritems['item_id'];
                            // We ant to use our Fetch() function to return our brand
                            $_brand         =   $this->Fetch();
                            // If brand available, set it to an array
                            if(!empty($_brand))
                                $array[]    =   $_brand;
                        }
                    if(isset($array)) { 
                            // Sort the array
                            asort($array);
                            // Finally, we use the Display() function for the final output
                            $this->Display($array);
                        }
                    else { ?>
                            <div>Locker is empty.</div><?php
                        }
                }
            // Establish this as an in-class variable
            protected   $brand;
            // Establish this as a public function incase we want to use it by itself
            // To do so you would write $myLocker->Fetch(); outside of the class.
            // Since you need $brand for this function to work, you would need to turn
            // $brand from "protected" to "public" and write $myLocker->brand = 'whatever';
            // before you run the $myLocker->Fetch();
            public  function Fetch()
                {
                    $query  =   mysql_query("SELECT DISTINCT brand FROM inventory WHERE id = '".$this->brand."'");
                    $brands =   mysql_fetch_array($query);
                    // Return brand
                    return  (isset($brands['brand']))? $brands['brand']:"";
                }

            protected   function Display($array)
                {
                    if(is_array($array)) {
                            foreach($array as $object) { ?>
                                <div><?php echo $object; ?></div><?php
                                }
                        }
                }
        }

    // You should be using mysqli_ or PDO for your db connections/functions.
    $_locker    =   mysql_query("SELECT DISTINCT item_id FROM lockers WHERE user_id = '$profile_userid' AND locker_id ='$lockerid' ");

    // If there are more than 0 rows, create locker.
    if(mysql_num_rows($_locker) > 0) {
            // Create new instance of the locker app
            $myLocker   =   new MyLocker($_locker);
            // Display the results
            $myLocker->LockerContents();
        }
class MyLocker
{
//受保护意味着您不能在函数/类之外使用此变量
//因此,您不能使用$myLocker->\u数组;它将抛出一个错误
受保护的$\u数组;
//构造基本上用作自动函数。它将自动执行
//创建类的新实例时,请在执行此操作后立即执行以下操作:
//$myLocker=newmylocker($\u-locker);您可以启动\u构造
//当您标记为public时,您允许它在自身之外使用
公共函数构造($\数组)
{
//当您设置此变量时,它现在已打开,可在所有应用程序中使用
//这个类中的其他函数。
$this->\u数组=$\u数组;
}
//这是做任何事情的方法
公共功能锁内容()
{
//循环查询。因为$\u数组是在\uu构造中设置的
//它在该函数中作为$this->\u数组提供
而($lockeritems=mysql\u fetch\u数组($this->\u数组)){
//$brand是我们希望在其他功能中使用的东西,但不是
//在类之外,因此在此处设置它以用于Fetch()函数
$this->brand=$lockeritems['item_id'];
//我们希望使用Fetch()函数返回我们的品牌
$\u brand=$this->Fetch();
//如果品牌可用,请将其设置为阵列
如果(!空($\品牌))
$array[]=$\u品牌;
}
if(isset($array)){
//对数组进行排序
asort($阵列);
//最后,我们使用Display()函数作为最终输出
$this->Display($array);
}
还有{?>

储物柜是空的。非常感谢您把它写出来;我今天会尝试实现它,然后回复。您是否曾经使用过它,或者它没有满足您的需要??Hi Rasclatt,我还没有机会实现它;我对PHP的类方面不熟悉,所以我打算读一读,然后研究一下您的代码以进行测试e它是如何工作的,在与我的其他人一起实现之前。为了快速了解它,我想把一个类想象成一个应用程序。它是一个包含大量函数的容器,与任何其他函数(在大多数情况下)没有什么不同。基本上,要使用一个类,你必须创建一个实例(使用“new”)一旦你这样做了,你就可以引用你的应用程序(类)中的所有方法通过您分配给应用程序的变量。在本例中,我使用了
$myLocker
,因此要访问它的函数,您可以使用指针
->
so
$Class->Function
。它们非常强大。我会记得更好,以便在您尝试使用它时更容易理解。