什么是错我的自定义PHP函数 [英] Something is wrong with my custom php function

查看:157
本文介绍了什么是错我的自定义PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我想创建一个自定义功能,将呼应最终用户的iframe中一个网站的网址。

Okay so I am trying to create a custom function that will echo a site url inside an iframe for the end user.

该脚本来检查用户是否已经看到该网站,如果他们已经看到了不更多的显示出来,而是采取其他网站的url从数据库等。

The script has to check whether or not the user has already seen the site and if they have seen it don't display it any more, but take another site url from the database etc.

这就是我想出迄今:

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 则返回一个空数组。

其次,脚本不写网站网址的数据库,因为 $ urlToShow 是一个数组不是一个单一的网址是什么?

Secondly the script doesn't write the site url into the database because the $urlToShow is an array not a single url?

推荐答案

我认为这个问题是在code是在你创建$ siteUrls,$ seenUrls数组的地方。 mysqli_fetch_assoc()函数会给你一个结果行作为关联数组。所以,如果你想改变你的一些code在while循环。
请chnage此

I think the problem is in your code is at the place where you are creating your $siteUrls, $seenUrls arrays. mysqli_fetch_assoc() function will give you a result row as an associative array. So if you want to change some of your code in the while loops. Please chnage this

while($row = mysqli_fetch_assoc($result)) {
        $siteUrls[$index] = $row;
        $index++;
}

while($row = mysqli_fetch_assoc($result)) {
        $siteUrls[$index] = $row['site_url'];
        $index++;
    }

而在第二个while循环也。改变这种

And in the second while loop also. Change this

while($row2 = mysqli_fetch_assoc($result2)) {
        $seenUrls[$index] = $row2;
        $index++;
}

while($row2 = mysqli_fetch_assoc($result2)) {
        $seenUrls[$index] = $row2['site_url'];
        $index++;

}
并尝试

} and try

这篇关于什么是错我的自定义PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆