我有3个表格users
alerts
和articles
用户可以设置提醒,因此当添加与他的个人资料相匹配的新文章时,他会收到一封电子邮件
我正在尝试编写一个小代码来执行此任务
这是我到目前为止所提出的
$query_users = "SELECT * FROM `users`";
$result_users = $conn->query($query_users);
while ($users = mysqli_fetch_row($result_users)) {
$user_id = $users[0];
$user_email = $users[1];
$query_alerts = "SELECT * FROM `alerts` where user_id='$user_id' and active= '1' ";
$result_alerts = $conn->query($query_alerts);
while ($alerts = mysqli_fetch_row($result_alerts)) {
$category = mysqli_real_escape_string($conn,$alerts[2]);
$keyword = mysqli_real_escape_string($conn,$alerts[3]);
$query_search = "SELECT * FROM `articles` WHERE `category_id` = '$category' `title` LIKE '%$keyword%' ORDER BY `articles`.`id` ASC ";
$result_search = $conn->query($query_search);
$count = $result_search->num_rows;
}
}
现在我想做的是以这种方式存储结果
将每个提醒category
+ keyword
+ count
放入数组中,然后将其放入包含用户id
和email
所以在底部,我可以遍历用户并发送给身份1的用户并发送电子邮件至example@website.com 像这样的表
类别|关键字|文章计数
健康|饮食| 5
商业|银行| 2
我希望你理解我的想法,我很困惑,头晕我甚至不知道在搜索框中键入什么
答案 0 :(得分:1)
在while之外取一个数组,并在执行循环时在数组中添加3个变量。
我在你的代码中添加了两行。
$response = array();
和
$response[] = array
(
'category' => $category,
'keyword' => $keyword,
'count' => $count
);
完整示例
<?php
$query_users = "SELECT * FROM `users`";
$result_users = $conn->query($query_users);
$response = array();
while ($users = mysqli_fetch_row($result_users))
{
$user_id = $users[0];
$user_email = $users[1];
$query_alerts = "SELECT * FROM `alerts` where user_id='$user_id' and active= '1' ";
$result_alerts = $conn->query($query_alerts);
while ($alerts = mysqli_fetch_row($result_alerts))
{
$category = mysqli_real_escape_string($conn,$alerts[2]);
$keyword = mysqli_real_escape_string($conn,$alerts[3]);
$query_search = "SELECT * FROM `articles` WHERE `category_id` = '$category' `title` LIKE '%$keyword%' ORDER BY `articles`.`id` ASC ";
$result_search = $conn->query($query_search);
$count = $result_search->num_rows;
$response[] = array
(
'category' => $category,
'keyword' => $keyword,
'count' => $count
);
}
}
echo "<pre>";
print_r($response);