我根据用户关注的用户构建数据流。虽然我有一个工作原型,代码似乎不必要的丑陋与多个循环与嵌套在其中的进一步查询。我想知道我是否可以得到任何关于简化这个的建议,可能在sql查询本身内处理更多?使用一些代码:(我已经将它修剪了一点并删除了一些检索到的行。)
$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"
);
}
}
}
我最初想过在初始while循环中使用三个查询的联合,但是从查询构建的每个数组必须显示&#34; type&#34; =&gt;&#34;&#34;字段,这非常重要,因为项目在流中的显示方式取决于此。
我之所以有一个主要原因,我认为必须有一种方法,因为在第二个循环中嵌套一个查询而while循环会引发一个错误的恶臭,因此在行数量上使用for循环。但是这段代码感觉不对。它感觉很难看,而且非常重复,虽然它有效但我觉得它不应该被解决。
运行此代码块后,所有3个数组都合并为一个多维数组,如下所示:
Array
(
[0] => Array
(
[fullAddress] => London, England
[type] => do
)
[1] => Array
(
[fullAddress] => Portsmouth, England
[type] => done
)
[2] => Array
(
[fullAddress] => Paris, France
[type] => story
)
)
有没有更好的方法来做到这一点,这是在这种情况下构建多维数组的最佳方法吗?
答案 0 :(得分:0)
我会在数据库中为followed_users
或其他内容创建一个表格,您可以将用户链接到他们关注的用户ID,然后在该表格中查询ID列表&#39 ; s并运行您的下一个查询以获取您正在提取的信息(或者,如果数据很小,则将该数据存储到表中
=======================================
| id | user_id | following_id | type |
=======================================
| 1 | 101 | 12 | story |
| 2 | 108 | 15 | story |
| 3 | 108 | 16 | do |
| 4 | 108 | 19 | done |
=======================================
甚至可以添加地址,如果这就是你要拉的那些