|
combine append results from two dbquery results
|
| blueadept |
Posted on 29-01-2013 14:13
|

Junior Member

Posts: 17
Joined: 24/03/2006
|
I want to combine the results from two dbquery results into one in order to do a custom list.
For example:
$result1 = dbquery(SELECT * FROM ".DB_SOMETHING." WHERE some_field = 'something' ORDER BY RAND()" ;
$result2 = dbquery(SELECT * FROM ".DB_SOMETHING." WHERE some_other_field = 'something' && some_field != 'something' ORDER BY some_other_field" ;
I want to append $result2 to the results of $results1 so that $results1 will come out first and then the results from $results2 come out.
I have a while ($data = dbarray($result3)) statement that would display the results but I just cant seem to get it to work.
Sorry this message is rushed because I am heading to work. I hope it makes sense and hope someone can help. Thanks |
| |
|
|
| JoiNNN |
Posted on 29-01-2013 14:24
|

Admin

Posts: 273
Joined: 05/02/2011
|
See http://php.net/ma...merge.php.
You'll have to do something like
$result1 = dbarray(dbquery(SELECT * ...
$result2 = dbarray(dbquery(SELECT * ...
$result3 = array_merge($result1, $result2);
and then you have to check if there are any results like
if (empty($result3)) {
//there ARE NO results
} else {
//there ARE some results
}
-- not tested, dunno if dbarray returns empty arrays if there's no result or how array_merge() acts when merging empty arrays -- |
| |
|
|
| Tyler |
Posted on 29-01-2013 17:36
|

Member

Posts: 160
Joined: 30/11/2011
|
Are these queries related at all? If they are you can simply do a join...
This is just an example i whipped up so its pretty self explanatory...
Codedbquery("SELECT o.*, i.* FROM ".DB_ORDERS." o INNER JOIN ".DB_ITEMS." i ON o.order_item=i.item_id");
o.* - Grabs all fields from orders table row and the same for i.*
If you only need certain fields just do i.item_name
I'm not sure if this will help you but I am assuming this is what you want. But this requires the two tables to be linked somehow. In this case it is order_item matched to item_id....
I hope I helped.
Helping, would be pointing you in the right direction, not doing it all for you.
|
| |
|
|
| blueadept |
Posted on 29-01-2013 23:18
|

Junior Member

Posts: 17
Joined: 24/03/2006
|
JoiNNN wrote:
See http://php.net/ma...merge.php.
You'll have to do something like
$result1 = dbarray(dbquery(SELECT * ...
$result2 = dbarray(dbquery(SELECT * ...
$result3 = array_merge($result1, $result2);
and then you have to check if there are any results like
if (empty($result3)) {
//there ARE NO results
} else {
//there ARE some results
}
-- not tested, dunno if dbarray returns empty arrays if there's no result or how array_merge() acts when merging empty arrays --
Unfortunately I tried the array_merge already but I could not get it to function properly (kept returning no results after the merge (fyi I am on php5 and the array_merge is a little different. You have to have (array) before the $result1 etc). I know I have data from each of the results, but after the merge it is empty.
I thought that I might have been using the array_merge function wrong but from what you wrote, that is basically what I had.
I dont know if the join will work for what I need. Ill have to think about it and see if I can do this from another angle. Thanks for both the suggestions. |
| |
|
|
| Tyler |
Posted on 30-01-2013 00:55
|

Member

Posts: 160
Joined: 30/11/2011
|
To be honest I didn't really understand you...
If I knew and saw maybe what you were doing I could maybe help you more.
Feel free to pm me or post it here.
Honestly joining is the best route. If you haven't really worked it into your project you could save time and be more efficient with joins....
Helping, would be pointing you in the right direction, not doing it all for you.
|
| |
|
|
| KasteR |
Posted on 30-01-2013 01:25
|

Member

Posts: 152
Joined: 14/07/2012
|
Maybe someone can provide an example (sorry I'm unable to at the moment). But what about a UNION QUERY on the SQL side?
Ref: http://www.w3schools.com/sql/sql_union.asp
It would be an alternative, if you're unable to use array_merge as JoiNNN has suggested. This would eliminate the need for a merge, as you're doing this from the query. |
| |
|
|
| blueadept |
Posted on 30-01-2013 13:18
|

Junior Member

Posts: 17
Joined: 24/03/2006
|
KasteR wrote:
Maybe someone can provide an example (sorry I'm unable to at the moment). But what about a UNION QUERY on the SQL side?
Ref: http://www.w3schools.com/sql/sql_union.asp
It would be an alternative, if you're unable to use array_merge as JoiNNN has suggested. This would eliminate the need for a merge, as you're doing this from the query.
You nailed it. I had tried UNION and it did produce the results I wanted, but the problem was the ordering which made it look like it wasnt working correctly.
SQL is not my strongest. I had already tried most of the suggestions but failed. When the things I tried were reiterated by others, it meant that I was on the right track but I must have something wrong.
I figured out my error. I was trying to Oder it by 2 fields and instead of using a , I was using AND which didnt work and was only taking the first order statement.
Thanks for all your help.
Edited by blueadept on 30-01-2013 13:26
|
| |
|