Running multiple queries in loop, and building multidimensional array

心已入冬 提交于 2020-01-06 09:03:34

问题


I'm building a stream of data based on users a user is following. While I have a working prototype, the code seems unnecessarily ugly with multiple loops with further queries nested in them. I was wondering if I could get any advice on simplifying this, possible handling more within the sql query itself? On with some code: (I've trimmed this down a little and removed some of the rows retrieved).

$init = $conn->prepare("SELECT followerid FROM following WHERE userid=?");
$init->bind_param("s", $userid);
$init->execute();
$init->bind_result($idq);
$init->store_result();
$num = $init->num_rows();
  while($init->fetch()) {

    // get all to be done by each user
    $stmt = $conn->prepare("SELECT activityId FROM done WHERE userId=? ORDER BY number DESC");
    $stmt->bind_param("s", $idq);
    $stmt->execute();
    $stmt->bind_result($aiddo);
    $stmt->store_result();
    $num_do = $stmt->num_rows;
      while($stmt->fetch()) {
        $activityId_do[] = $aiddo;
      }

    // get location information
    for($i=0; $i<=$num_do; $i++) {
      $act_done = $conn->prepare("SELECT fullAddress FROM `activity` WHERE (id=?)");
      $act_done->bind_param("s",$activityId_do[$i]);
      $act_done->execute();
      $act_done->bind_result($fullAddress);
        while($act_done->fetch()) {
          $do_array[] = array(
            "fullAddress"=>$fullAddress,
            "type"=>"do"
          );

        }
    }

    //get all done by each user
    $stmt = $conn->prepare("SELECT activityId FROM done WHERE userId=? ORDER BY number DESC");
    $stmt->bind_param("s", $idq);
    $stmt->execute();
    $stmt->bind_result($aid);
    $stmt->store_result();
    $num_done = $stmt->num_rows;
      while($stmt->fetch()) {
        $activityId[] = $aid;
    }

    for($i=0; $i<=$num_done; $i++) {
      $act_done = $conn->prepare("SELECT fullAddress FROM `activity` WHERE (id=?)");
      $act_done->bind_param("s",$activityId[$i]);
      $act_done->execute();
      $act_done->bind_result($fullAddress);
        while($act_done->fetch()) {
          $done_array[] = array(
            "fullAddress"=>$fullAddress,
            "type"=>"done"
          );

        }
    }

    //get all stories by each user
    $stmt = $conn->prepare("SELECT activityId FROM story WHERE userId=? ORDER BY number DESC");
    $stmt->bind_param("s", $idq);
    $stmt->execute();
    $stmt->bind_result($aidst);
    $stmt->store_result();
    $num_story = $stmt->num_rows;
      while($stmt->fetch()) {
        $activityId_story[] = $aidst;
      }

    for($i=0; $i<=$num_story; $i++) {
      $act_done = $conn->prepare("SELECT fullAddress FROM `activity` WHERE (id=?)");
      $act_done->bind_param("s",$activityId_story[$i]);
      $act_done->execute();
      $act_done->bind_result($fullAddress);
        while($act_done->fetch()) {
          $story_array[] = array(
            "fullAddress"=>$fullAddress,
            "type"=>"story"
          );

        }
    }

}

I originally thought about using a union on the three queries inside the initial while loop, but each array built from the query MUST display the "type"=>"" field, this is hugely important as the way items are displayed in the stream depend on this.

One of the main reasons I was lead to believe there must be a batter way as nesting a query in the second while loop kicked up a stink of errors, thus the use of the for loop on the amount of of rows. But this code just doesn't feel right. It feels ugly and really repetitive, and although it works, I feel it shouldn't be settled for.

After this block of code is run, all 3 arrays are merged into one multidimensional array, like so:

Array
(
[0] => Array
    (
        [fullAddress] => London, England
        [type] => do
    )

[1] => Array
    (
        [fullAddress] => Portsmouth, England
        [type] => done
    )

[2] => Array
    (
        [fullAddress] => Paris, France
        [type] => story
    )

) 

Is there a better way to do this and is this the best way to build the multi-dimensional array in this case?


回答1:


I would create a table in the database for followed_users or something, where you link your user to the id's of users they are following, then you query that table for the list of id's and run your next query to get the information you are pulling (or, if the data is small, store that data as well into the table

=======================================
| id | user_id | following_id | type  |
=======================================
|  1 |     101 |           12 | story |
|  2 |     108 |           15 | story |
|  3 |     108 |           16 |    do |
|  4 |     108 |           19 |  done |
=======================================

maybe even add in the address, if that is all you are pulling



来源:https://stackoverflow.com/questions/25327949/running-multiple-queries-in-loop-and-building-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!