mysql - Something is wrong with my custom php function

640

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

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.

Here's what I have come up with so far:

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);
}

I have currently found two errors with this. First the $siteUrls and $seenUrls are all okay, but when I compare the two usingarray_diff then it returns an empty array.

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

161

Answer

Solution:

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++;
}

To

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

And in the second while loop also. Change this

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

To

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

} and try

591

Answer

Solution:

i would not run two queries and try to merge the result array. you can use mysql itself to just return the sites that have not been seen yet:

SELECT sites.site_url FROM sites
LEFT JOIN views ON views.site_url=sites.site_url AND views.user='$currentUsername'
WHERE sites.site_url IS NOT NULL AND views.site_url IS NULL

This will return only site_urls from sites, that have no entry in the views table. TheLEFT JOIN will join the two tables and for every non-matching row there will be NULL values in the views.site_url, so that is why i am checking forIS NULL.

Your saving of $urlToShow should work, if you set to $row field content and not $row itself as suggested, but if you want to check what is in the variable, don't useecho use this:

print_r($urlToShow);

If the variable is an array, you will see it's content then.

672

Answer

Solution:

@Azeez Kallayi - you don't need to index array manually.

$seenUrls[] = $row2['site_url'];

In addition, you can fetch all result

$rows = mysqli_fetch_all($result2,MYSQLI_ASSOC);
foreach($rows as $row){
echo $row['site_url'];
}

People are also looking for solutions to the problem: php - How do I pass a variable from the controller to the view in Codeigniter?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.