1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Not ordered correctly

Discussion in 'PHP' started by basketmen, May 3, 2015.

  1. #1
    Hi guys, i have latest mysql version (5.5)

    this is the screenshot of groupid field
    [​IMG]

    i didnt touch anything yet, but some cells are not ordered correctly like this
    but if i click groupid name in the top, it will ordered correctly like this
    [​IMG]




    SEMrush
    below php code output is like first screenshot above, that are not ordered correctly
    please help how to make the output ordered correctly, like second screenshot above,
    maybe add code like this : order by id asc
    but where the right place to put it below?

    
    $group_ids=explode(" ",$options['groupchoice_ids']);
    $groupsql="SELECT id, title FROM " . TABLE_PREFIX . "thegroup WHERE";
    $first=true;
    foreach($group_ids as $value){
    if(!$first){
    $groupsql=$groupsql." OR ";
    }
    else{
    $first=false;
    }
    $groupsql=$groupsql." id = '".$value."' ";
    }
    
    $kh_optionsgroup='<select name = "accounttype">';
    
    
    
    $checksec = $db->query_read($groupsql);
      if ($db->num_rows($checksec))
      {
       
      while($lboard = $db->fetch_array($checksec))
      {
      $kh_optionsgroup=$kh_optionsgroup."<option value ='".$lboard['id']."'>".$lboard['title']."</option>";
    
      }   
      }
    $verifystring='$human_verify';
    $kh_optionsgroup=$kh_optionsgroup."</select>";
    
    
    PHP:
     
    Solved! View solution.
    basketmen, May 3, 2015 IP
    SEMrush
  2. #2
    line 13, add
    $groupsql .= ' ORDER BY id ASC';
     
    EricBruggema, May 3, 2015 IP
    basketmen likes this.
  3. billzo

    billzo Well-Known Member

    Messages:
    961
    Likes Received:
    278
    Best Answers:
    15
    Trophy Points:
    113
    #3
    Explicitly putting an ORDER BY clause on an SQL string is good practice. MySQL does not always keep things in its database in the order you insert them or in the order you want them to be. For example, if you delete a row, MySQL does not reshuffle everything to eliminate the space occupied by the deleted row. It can in the future stick other data in the spot where you deleted something which will result in data being out of order.

    Always use ORDER BY if you expect data to be in some sort of order. (As given in the previous post.)
     
    Last edited: May 4, 2015
    billzo, May 4, 2015 IP
    basketmen likes this.