Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 4.16 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Combine mysql query with sub query results into one PHP array
  2. while(eventQuery):
  3.  
  4.     if commentQueryResult;
  5.         $array .= commentQueryResult;
  6.  
  7.     if forumPostQueryResult;
  8.         $array .= forumPostQueryResult;
  9.  
  10.     if uploadItemQueryResult;
  11.         $array .= uploadItemQueryResult;
  12.  
  13. endwhile;
  14.  
  15. return $array; // Combined returned results as one array
  16.        
  17. $eventResult = mysql_query(
  18.     'SELECT userevent.event, userevent.eventId, userevent.friendId
  19.     FROM userevent
  20.     WHERE userevent.userId = 1 OR userevent.friendId = 1
  21.     ORDER BY userevent.id DESC
  22.     LIMIT 20'
  23. );
  24.  
  25. while ($eventRow = mysql_fetch_row($eventResult)){
  26.  
  27.     if($eventRow[0] == 1){
  28.  
  29.         $result = mysql_fetch_array(mysql_query("
  30.             SELECT forumRooms.id, forumRooms.title                                                                                      
  31.             FROM forumPosts                                                
  32.             INNER JOIN forumRooms ON forumPosts.`forumTopic` = forumRooms.`id`                                                      
  33.             WHERE forumPosts.id = '$eventRow[1]'"));
  34.     }
  35.     elseif($eventRow[0] == 2){
  36.  
  37.         $result = mysql_fetch_array(mysql_query("
  38.             SELECT game.id, game.uriTitle, game.title                                                          
  39.             FROM usergamecomment
  40.             INNER JOIN game ON usergamecomment.`gameId` = game.id
  41.             WHERE usergamecomment.id = $eventRow[1]"));  
  42.     }
  43.     elseif($eventRow[0] == 4){
  44.  
  45.         $result = mysql_fetch_array(mysql_query("
  46.             SELECT usercomment.comment, UNIX_TIMESTAMP(usercomment.TIME), `user`.id, `user`.username, `user`.activate
  47.             FROM usercomment
  48.             INNER JOIN `user` ON usercomment.`userId` = `user`.id
  49.             WHERE usercomment.id = $eventRow[1]
  50.             AND `user`.activate = 1"));
  51.     }
  52.     elseif($eventRow[0] == 5){
  53.  
  54.         $result = mysql_fetch_array(mysql_query("
  55.             SELECT game.id, game.title, game.uriTitle
  56.             FROM game
  57.             WHERE game.id = $eventRow[1]"));
  58.     }
  59.  
  60. // Combined Results as array
  61. }
  62.        
  63. $result = array();
  64. $q = 'SELECT userevent.event AS userevent_event,
  65.       userevent.eventId AS userevent_eventId,
  66.       userevent.friendId AS userevent_friendId,
  67.       forumRooms.id AS forumRooms_id,
  68.       forumRooms.title AS forumRooms_title,
  69.       game.id AS game_id,
  70.       game.uriTitle AS game_uriTitle,
  71.       game.title AS game_title,
  72.       usercomment.comment AS usercomment_comment,
  73.       UNIX_TIMESTAMP(usercomment.TIME) AS usercomment_time,
  74.       user.id AS user_id,
  75.       user.username AS user_username,
  76.       user.activate AS user_activate,
  77.       g2.id AS game2_id,
  78.       g2.uriTitle AS game2_uriTitle,
  79.       g2.title AS game2_title
  80.  
  81.  
  82.     FROM userevent
  83.     LEFT JOIN forumPosts ON forumPosts.id = userevent.eventId
  84.     LEFT JOIN forumRooms ON forumPosts.forumTopic = forumRooms.id
  85.     LEFT JOIN usergamecomment ON usergamecomment.id = userevent.eventId
  86.     LEFT JOIN game ON usergamecomment.gameId = game.id
  87.     LEFT JOIN usercomment ON usercomment.id = userevent.eventId
  88.     LEFT JOIN user ON usercomment.userId = user.id
  89.     LEFT JOIN game g2 ON userevent.eventId = g2.id
  90.     WHERE (userevent.userId = 1 OR userevent.friendId = 1)
  91.       AND userevent.eventId >= (SELECT userevent.eventId
  92.                 WHERE userevent.userId = 1 OR userevent.friendId = 1
  93.                 ORDER BY userevent.id DESC LIMIT 1,20);';
  94.  
  95. $r = mysql_query($q);
  96.  
  97. while ( $o = mysql_fetch_row($r) ) {
  98.   switch($o['userevent_event']) {
  99.     case 1:
  100.       $result[] = array(
  101.     'id' => $o['forumsRooms_id'],
  102.     'title' => $o['forumsRooms_title'],
  103.       );
  104.       break;
  105.     case 2:
  106.       $result[] = array(
  107.     'id' => $o['game_id'],
  108.     'uriTitle' => $o['game_uriTitle'],
  109.     'title' => $o['game_title'],
  110.       );
  111.       break;
  112.     case 4:
  113.       $result[] = array(
  114.     'comment' => $o['usercomment_comment'],
  115.     'time' => $o['usercomment_time'],
  116.     'id' => $o['user_id'],
  117.     'username' => $o['user_username'],
  118.     'activate' => $o['user_activate'],
  119.       );
  120.       break;
  121.     case 5:
  122.       $result[] = array(
  123.     'id' => $o['game2_id'],
  124.     'uriTitle' => $o['game2_uriTitle'],
  125.     'title' => $o['game2_title'],
  126.       );
  127.       break;
  128.   }
  129. }