Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

combine append results from two dbquery results

Asked Modified Viewed 3,816 times
B
blueadept
B
  • Junior Member, joined since
  • Contributed 17 posts on the community forums.
  • Started 3 threads in the forums
  • Started this discussions
asked
Junior Member

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
0 replies

6 posts

J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

See http://php.net/manual/en/function.array-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 --
0 replies
T
Tyler
T
Tyler 10
Helping, would be pointing you in the right direction, not doing it all for you.
  • Member, joined since
  • Contributed 198 posts on the community forums.
  • Started 3 threads in the forums
answered
Member

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...
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.
0 replies
B
blueadept
B
  • Junior Member, joined since
  • Contributed 17 posts on the community forums.
  • Started 3 threads in the forums
  • Started this discussions
answered
Junior Member

Quote

JoiNNN wrote:

See http://php.net/manual/en/function.array-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.
0 replies
T
Tyler
T
Tyler 10
Helping, would be pointing you in the right direction, not doing it all for you.
  • Member, joined since
  • Contributed 198 posts on the community forums.
  • Started 3 threads in the forums
answered
Member

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....
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

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.
0 replies
B
blueadept
B
  • Junior Member, joined since
  • Contributed 17 posts on the community forums.
  • Started 3 threads in the forums
  • Started this discussions
answered
Junior Member

Quote

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 15:26,
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 6 posts
  • Votes 0 votes
  • Topic users 4 members

4 participants

B
B
  • Junior Member, joined since
  • Contributed 17 posts on the community forums.
  • Started 3 threads in the forums
  • Started this discussions
J
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
T
T
Tyler 10
Helping, would be pointing you in the right direction, not doing it all for you.
  • Member, joined since
  • Contributed 198 posts on the community forums.
  • Started 3 threads in the forums
K
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet