微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用PHP对MongoDB记录进行排序?

如何解决如何使用PHP对MongoDB记录进行排序?

我试图使用MongoDB和PHP创建一个简单的Web应用程序。我创建了一个表并将数据成功插入其中。现在,我尝试按asc / desc和字母顺序对数据进行排序。但是这里只排序前2个记录。我尝试了许多方法,但徒劳无功。请帮我!!这是我的代码

<form method="post">

        <table style="width:180px" border="solid" overflow="hidden" >
          <tr>
            <th>_id</th>
            <th>Author</th>
            <th>Subject</th>
            <th><button type="submit" name="notes_sort">sort</button>Title</th>
            <th>Notes</th>
            <th> <button type="submit" name="diff_ascend">Asc</button><button type="submit" 
    name="diff_descend">Desc</button> Difficulty</th>
            <th>Related Topics</th>
            <th>Tags</th>
            <th>Date</th>
            <th>Action</th>
          </tr>
        

        <?PHP
            date_default_timezone_set("Asia/Kolkata");
            $current_date = date('d/m/Y'); 
            $options = array(
                "sort" => array("_id" => -1),);
            
            $data = $collection->find(array('date' => $current_date),$options);

            if(isset($_POST['diff_ascend'])){
                $options = array(
                "sort" => array("difficulty" => 1),);
                $data = $collection->find(array('date' => $current_date),$options);
            }

            if(isset($_POST['diff_descend'])){
                $options = array(
                "sort" => array("difficulty" => -1),$options);
            }
            
            if(isset($_POST['notes_sort'])){
                $options = array(
                "sort" => array("notes" => -1),$options);
            }
            
                        
            foreach($data as $document){
         ?> 
                <tr>
                <td><?PHP echo $document['_id'] ?></td>
                <td><?PHP echo $document['author'] ?></td> 
                <td><?PHP echo $document["subject"] ?></td>
                <td><?PHP echo $document["title"] ?></td>
                <td><?PHP echo $document["notes"] ?></td> 
                <td><?PHP echo $document["difficulty"] ?></td>
                <td><?PHP echo $document["rel_topic"] ?></td> 
                <td><?PHP echo $document["tags"]["tag4"].",".$document["tags"]["tag3"].",".$document["tags"]["tag2"].",".$document["tags"]["tag1"]?></td> 
                <td><?PHP echo $document["date"] ?></td> 
               <td><a href='delete.PHP?id=<?PHP echo $document["_id"];?>' >Delete</a>  <a 
          href='update.PHP?id=<?PHP echo $document["_id"];?>' name='update'>
               Update</a></td>
               
                </tr>
        </table>
    </form> 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。