简体   繁体   English

返回的数据顺序不正确

[英]Incorrect Data Order Returned

I have an MySQL data: 我有一个MySQL数据:

MySQL数据

I try to get fetch them using jquery: 我尝试使用jquery来获取它们:

         $.post(
            '../php/teacheradminfunction.php',
            {'functions':'getBadgeImg','topicSkill':$('#selTeachAdminBadgeDeleteSkill').val()},
            function(data) {
                $.each(data, function( key, value ) {
                    alert( key + ": " + value.badge_path );
                });
            },'JSON'
        );

My PHP script: 我的PHP脚本:

$queryStmt = 'SELECT badge_path FROM badges WHERE badge_skill=:sqlSkill ORDER BY `index` ASC';
        $queryPrepare = $dba_connect->prepare($queryStmt);
        $queryPrepare->execute(array(':sqlSkill'=>$_POST['topicSkill']));
        $queryResult = $queryPrepare->fetchAll(PDO::FETCH_ASSOC);
        $queryPrepare->closeCursor();

        echo json_encode($queryResult);

My problem is that I needed to have the correct order, what I get is: 我的问题是我需要有正确的订单,我得到的是:

[{"badge_path":"..\\image\\badges\\Counting10_g.png"},{"badge_path":"..\\image\\badges\\Counting10_b.png"},{"badge_path":"..\\image\\badges\\Counting10_s.png"}]

Expected result : 预期结果 :

[{"badge_path":"..\\image\\badges\\Counting10_g.png"},{"badge_path":"..\\image\\badges\\Counting10_s.png"},{"badge_path":"..\\image\\badges\\Counting10_b.png"}]

You could also select the Index or "badge_rank" and use this as key:value as JSON instead of an array of objects. 您也可以选择Index或“ badge_rank”并将其用作key:value作为JSON而不是对象数组。

Your easiest way is to change the Index of your badge_path to the correct order. 最简单的方法是将badge_path的索引更改为正确的顺序。

  1. Gold, 金,
  2. Silver, 银,
  3. Bronce 布龙斯

change the index value in database modify the index value for Counting10_s.png and make it 2 and likewise change the index value for Counting10_b.png and make it 3 更改数据库中的索引值修改Counting10_s.png的索引值并将其设为2,同样将Counting10_b.png的索引值更改为3

Since you are using order by index hence the rows are sorted based on the index value. 由于您使用的是按索引排序,因此将根据索引值对行进行排序。 Modifying the index value will give you the desired output 修改索引值将为您提供所需的输出

Give proper index in your database. 在数据库中提供适当的索引。 According to your query it's return correct records. 根据您的查询返回正确的记录。

index    badge_rank
  1       GOLD
  2       SILVER
  3       BRONZE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM