Login
Username

Password



Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Navigation
Follow us on Facebook
Follow us on Google+
View Thread
Official Home of PHP-Fusion » General Addon and Modification Support » Code Snippet and functions
Who is here? 1 guest(s)
 Print Thread
combine append results from two dbquery results
blueadept
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()"Wink;

$result2 = dbquery(SELECT * FROM ".DB_SOMETHING." WHERE some_other_field = 'something' && some_field != 'something' ORDER BY some_other_field"Wink;

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
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 --
 
http://www.php-fusion.co.uk
Tyler
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...
Download source  Code
dbquery("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
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
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
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.
 
http://kaster.us
blueadept
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
 
Jump to Forum:
Similar Threads
Thread Forum Replies Last Post
fun with dbquery, a new function Code Snippet and functions 1 08-03-2014 15:20
Getting results from DB Modifications and Requests 3 07-03-2014 22:11
2013 Logo competition results General Discussion 14 10-01-2014 09:32
How to print user-friendly message on dbquery error Panels and Infusions 2 11-08-2013 00:55
Twitter Style Load More Results with jQuery and Ajax Modifications and Requests 3 13-01-2013 23:55
Official Home of PHP-Fusion uses cookies. Some may already have been set. Read more about our Cookies here.
Please click the button I Consent Cookies to hide this bar and accept our cookies. If you continue to use the site with no action taken, we'll assume that you consent our cookies anyway.
Cookiebar Panel fromVenue