2

I have a array with some id's I use to select a row set form the database.

The selection goes well but I dont receive my results back in the ordered given array

Inserted array (var_dump)

  0 => string '40201' (length=5)
  1 => string '44089' (length=5)
  2 => string '42106' (length=5)
  3 => string '42740' (length=5)
  4 => string '43812' (length=5)
  5 => string '44331' (length=5)
  6 => string '42109' (length=5)
  7 => string '44147' (length=5)
  8 => string '40464' (length=5)
  9 => string '42108' (length=5)

Output array

array
  0 => 
    array
      'id' => string '40201' (length=5)
  1 => 
    array
      'id' => string '40464' (length=5)
  2 => 
    array
      'id' => string '42106' (length=5)
  3 => 
    array
      'id' => string '42108' (length=5)
  4 => 
    array
      'id' => string '42109' (length=5)
  5 => 
    array
      'id' => string '42740' (length=5)
  6 => 
    array
      'id' => string '43812' (length=5)
  7 => 
    array
      'id' => string '44089' (length=5)
  8 => 
    array
      'id' => string '44147' (length=5)
  9 => 
    array
      'id' => string '44331' (length=5)

My zend query:

    $select = $this->_db
        ->select()
    ->from(array('file' => 'filehosts'), array('id'))
    ->where('file.id IN(?)', $array);

    $result = $this->getAdapter()->fetchAll($select);
    return $result

I think it's the conclusion of the id following up, first one is taken and then he just take the one who he finds first who is in the array right? How can I make this more strict so the query doesn't change the array order?

Thanks in advance. Nick

1
  • Im not familiar with zend, but it looks to me like the SELECT statement is missing an ORDER BY Commented Apr 5, 2012 at 13:28

3 Answers 3

1

Mysql FIELD function will work.

$select = $this->_db->select()
->from(array('file' => 'filehosts'), array('id'))
->where('file.id IN(?)', $array)
->order(new Zend_Db_Expr('FIELD(file.id, ' . implode(',', $array) . ')'));

$result = $this->getAdapter()->fetchAll($select);
return $result
Sign up to request clarification or add additional context in comments.

Comments

0

When you do a query, you can order the results with ORDER BY

SELECT * FROM `file` WHERE `file`.`id` IN $array ORDER BY `file`.`id` ASC

I'm not familiar with zend, but I think ->order('file.id') could do the job.

See Zend documentation

4 Comments

I know the order but as you can see, I created an array who is already ordered (comes form sphinxSearch) so I want to have my results back in an array like I put them in
I'm pretty sure that the IN part of the query ingores the ordering of your array. You could order your results afterwards with ksort() or one of the other array sorting functions of php.
Yes, I am sure now it ignores the order of my array and I think creates his own most efficient way to search the given array. ksort() is not an option because the key is still the same so the value is not in the good order.
Yeah true, so you can use usort in combination with your own (anonymous) function in to sort on id.
0

Think about what the underlying code would be doing.

It's going through them in the file which is almost certainly ordered by id and the testing if it's in your array which is in some compeletely different order.

The order of the items in $array, is totally irrelevant to the request, all that matters is exists or not.

2 Comments

Why is it irrelevant? I get my array results from the sphinxSearch in the good order, with this array I can do 1 query for one page to get the 'search results' This is what I try to reach now, just one step away from the finishing line to print the results (i print now just id to make it easier to see).
It's irrelevant to SQl's in function. To get what you want you'd have to reverse the operation select from array where in Select from Table, which would be a 'erm difficult.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.