【发布时间】:2015-06-02 09:09:12
【问题描述】:
好的,所以我正在尝试创建一个自定义函数,该函数将在 iframe 中为最终用户回显站点 url。
脚本必须检查用户是否已经看过该网站,如果他们已经看过它就不再显示它,而是从数据库中获取另一个网站网址等。
这是我目前的想法:
function get_urls() {
require 'config.php';
global $con;
global $currentUsername;
$con = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
$query = "SELECT site_url FROM sites WHERE site_url IS NOT NULL";
$result = mysqli_query($con, $query);
// Get all the site urls into one array
$siteUrls = array();
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row;
$index++;
}
$query2 = "SELECT site_url FROM views WHERE user = '$currentUsername' AND site_url IS NOT NULL";
$result2 = mysqli_query($con, $query2);
// Get urls the user has already seen into another array
$seenUrls = array();
$index = 0;
while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2;
$index++;
}
// Compare the two arrays and create yet another array of urls to actually show
$urlsToShow = array_diff($siteUrls, $seenUrls);
if (!empty($urlsToShow)) {
// Echo the url to show for the iframe within browse.php and add an entry to the database that the user has seen this site
foreach ($urlsToShow as $urlToShow) {
echo $urlToShow;
$query = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')";
mysqli_query($con, $query);
break;
}
}
// Show the allSeen file when all the ads are seen
else {echo 'includes/allSeen.php';}
mysqli_free_result($result);
mysqli_close($con);
}
我目前发现了两个错误。首先 $siteUrls 和 $seenUrls 都可以,但是当我使用 array_diff 比较两者时,它会返回一个空数组。
其次,脚本不会将站点 url 写入数据库,因为 $urlToShow 是一个数组而不是单个 url?
【问题讨论】: