Login
Username

Password



Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Navigation
Latest Addons
AD Gallery 23
SyntaxHighlighte... 45
Newsletters v4.03 103
Facebook Like Box 115
Newsletters v4.02 55
Metro 143
Facebook Connect 161
Shoutbox Panel 123
Redactor for PHP... 102
MI Floating Side... 105
Facebook Login/R... 146
Avatar Studio v2.03 175
Relationship Sta... 93
Sexual Orientati... 111
Fisherman 138
Popular Addons
iTheme2 5790
Arise 5779
User Control v1.23 4608
Event Calendar 4036
Photowidget panel 3879
Radio-Theme red2... 3344
Highslide Gallery 3306
CSS/JavaScript D... 3219
Facebook Connect... 2972
Dynamic Menu 2885
Slideshow Lightb... 2712
L-AMANT 2654
Enigma 2628
2Dark 2601
Black 2566
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://github.com/JoiNNN
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
Twitter Style Load More Results with jQuery and Ajax Ideas for Modifications and Requests 3 13-01-2013 23:55
help with dbquery Ideas for Modifications and Requests 4 19-12-2010 12:28
Admin Rights - Unexpected Results Suspected Bugs and Errors 3 28-11-2010 11:21
Search, not displaying all results Suspected Bugs and Errors 1 23-07-2010 17:25
How to combine this script Ideas for Modifications and Requests 2 13-02-2010 12:48
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 byVenue