php - combining two different queries into one merged foreach -


i'll try , explain best can. have 2 queries pulling data mysql, 1 pulls data on park reviews, other pulls data on ride reviews. both queries sorted review date.

what want on web page 1 list of reviews sorted review date park reviews , ride reviews mixed in 1 list.

almost 2 foreach loops running, 1 each query taking in turns based on date.

these 2 queries: rides

$query4= 'select review_id, review, tpf_reviews_rides.user, date_format(date_ridden, "%d %m %y") date_ridden, date_format(review_date, "%d %m %y") review_date, tpf_rides.name ride_name, rating, tpf_parks.name  park_name, country tpf_reviews_rides    inner join tpf_rides on tpf_reviews_rides.ride_id = tpf_rides.ride_id inner join tpf_ratings_rides on tpf_reviews_rides.rating_link = tpf_ratings_rides.rating_id   inner join tpf_parks on tpf_reviews_rides.park_id = tpf_parks.park_id  order review_date desc, review_id desc limit '.$start_from.' , '.$limit.''; 

for parks:

$query5= 'select review_id, review, tpf_reviews_parks.user, date_format(date_visited, "%d %m %y") date_visited, date_format(review_date, "%d %m %y") review_date, rating, tpf_parks.name, country tpf_reviews_parks    inner join tpf_ratings_parks on tpf_reviews_parks.rating_link = tpf_ratings_parks.rating_id   inner join tpf_parks on tpf_reviews_parks.park_id = tpf_parks.park_id  order review_date desc, review_id desc limit '.$start_from.' , '.$limit.''; 

and how separately display results: rides:

foreach ($res4 $row4) {              printf('             <h2 style="display:inline;">%s</h2> <h3 style="display:inline;">- %s, %s</h3>             <h3>rating - %sstars</h3>             <h4>submitted %s on %s</h4>             <p>%s</p>' . php_eol, $row4['ride_name'], $row4['park_name'], $row4['country'], $row4['rating'], (htmlspecialchars($row4['user'], ent_quotes, 'utf-8')), $row4['review_date'], nl2br(htmlspecialchars($row4['review'], ent_quotes, 'utf-8')));              if(!empty($row4['date_ridden'])){             printf('<p>date ridden: %s</p>' . php_eol, $row4['date_ridden']);             }              printf('<a class="bloglink" href="#top">back top</a>             <hr>');          }       parks:  foreach ($res5 $row5) {      printf('     <h2 style="display:inline;">%s</h2> <h3 style="display:inline;">- %s</h3>     <h3>rating - %sstars</h3>     <h4>submitted %s on %s</h4>     <p>%s</p>' . php_eol, $row5['name'], $row5['country'], $row5['rating'], (htmlspecialchars($row5['user'], ent_quotes, 'utf-8')), $row5['review_date'], nl2br(htmlspecialchars($row5['review'], ent_quotes, 'utf-8')));      if(!empty($row4['date_ridden'])){     printf('<p>date visited: %s</p>' . php_eol, $row4['date_visited']);     }      printf('<a class="bloglink" href="#top">back top</a>     <hr>');  } 

to make trickier page paginated. looking @ it, because 2 queries similar if there way combine @ query level best , keep paginate code in tact. have suggestions on how can achieve this?

thanks

you can use union merge 2 select statements.

note if tables similar, may better merge them (not in query, actual tables). not see fields not same in tpf_reviews_rides compared tpf_reviews_parks. add column denoting review type (ride or park) , should set. simplify queries , processing logic.

select     type,     review_id,     review,     user,     date_event,     review_date,     ride_name,     rating,     park_name,     country (     select         'ride' type,         review_id,         review,         tpf_reviews_rides.user,         date_format(date_ridden, "%d %m %y") date_event,         date_format(review_date, "%d %m %y") review_date,         tpf_rides.name ride_name,         rating,         tpf_parks.name park_name,         country             tpf_reviews_rides           inner join tpf_rides on tpf_reviews_rides.ride_id = tpf_rides.ride_id         inner join tpf_ratings_rides on tpf_reviews_rides.rating_link = tpf_ratings_rides.rating_id           inner join tpf_parks on tpf_reviews_rides.park_id = tpf_parks.park_id      union      select         'park' type,         review_id,         review,         tpf_reviews_parks.user,         date_format(date_visited, "%d %m %y") date_event,         date_format(review_date, "%d %m %y") review_date,         null ride_name,         rating,         tpf_parks.name park_name,         country             tpf_reviews_parks           inner join tpf_ratings_parks on tpf_reviews_parks.rating_link = tpf_ratings_parks.rating_id           inner join tpf_parks on tpf_reviews_parks.park_id = tpf_parks.park_id ) reviews order     review_date desc,     review_id desc limit ' . $start_from . ', ' . $limit; 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -