@broj1 thanks

userid !=$user_id logged in user will not see website submitted by own.

Logged in user will see websites only submitted by others

@broj1 not working

last id ($last_id) is taken from table 'views'. There is only one record per user in table 'views'.

Please try with database posted in last post

Modified code

// first get the last ID so you know when to switch for the first id again    
// LIMIT 1 is enough, no need for LIMT 0,1    
$query_v="SELECT * FROM views where user_id = $user_id ";
$result_v=mysql_query($query_v);                        

while ($row_v=mysql_fetch_assoc($result_v)) 
{ 
    $url_id_v=$row_v["id"];
    $user_id_v=$row_v["user_id"];
    $last_id=$row_v["url_id"];
}

if(empty($last_id))
    {
        $last_id=0;
    }
else
    {
        $last_id=$last_id;
    }

// if $_SESSION['current_id'] does not exist or if it is equal to last id
// then read the first record
if(!isset($_SESSION['current_id']) or $_SESSION['current_id'] == $last_id)
{
    // this query will select the record with the lowest id, presumably the first record
    $query ="SELECT * FROM websites where userid != $user_id ORDER BY id ASC LIMIT 1";
// in other cases read next record
} else {
    // this query will read the record just after the current id
    $query = "SELECT * FROM websites WHERE id > {$_SESSION['current_id']} AND userid != $user_id ORDER BY id ASC LIMIT 1";
}       

code to insert or update id of last viewed website

$ft_update = mysql_query("SELECT * FROM views where user_id =$user_id");
$fetch_update = mysql_num_rows($ft_update);

if($fetch_update == 0)
{
    $insert_query="INSERT INTO views (user_id, url_id) VALUES ($user_id, $url_id)";
    $insert=mysql_query($insert_query); 
}
else    
{       
    $update_query="UPDATE views SET  url_id=$url_id WHERE user_id=$user_id";
    $update=mysql_query($update_query); 
}

I tested the code, hopefuly got to grips with it and came up with my solution with no need for session. See the comments.

<?php
// ****** ONLY FOR TESTING

include 'db_connect.php';

// you probably get this from GET
$user_id = 1;

// ******

// get the ID of url the user saw the last
$q = "SELECT url_id FROM views WHERE user_id=$user_id";
$res = mysql_query($q);
$row = mysql_fetch_assoc($res);

// if there is a record for this user assign it to $previous_id
if(isset($row['url_id'])) {

    $previous_id = $row['url_id'];

// if there are no records for this user assign 0 to $previous_id
// which means the user has not viewed any websites yet
} else {

    $previous_id = 0;
}

// this array will hold all the url IDs that can be displayed to the user
$websites_array = array();

// get all the IDs that can be shown to the user
$q = "SELECT id, url FROM websites WHERE userid != $user_id ORDER BY id";
$res = mysql_query($q);
// fill the $websites_array with IDs
while($row = mysql_fetch_assoc($res)) {

    $id = $row['id'];
    $websites_array[$id] = $row['url'];
}

// if there are no urls in the database, do something
if(!isset($websites_array) || empty($websites_array)) {

    die('No web sites to show!');
}

// the last ID in the array (to know when to start from the beginning)
$websites_array_last_id = end(array_keys($websites_array));

// if previous ID is the last ID in the array or is not existing
// start with the forst URL in the array
if($websites_array_last_id == $previous_id || $previous_id == 0) {

    $current_url_id = current(array_keys($websites_array));

// otherwise loop from the array and when you find higher ID, break out
} else {

    foreach($websites_array as $url_id => $url) {

        if($url_id > $previous_id) {

            $current_url_id = $url_id;
            break;
        };
    }
}

// display the current URL
echo "URL to be displayed: {$websites_array[$current_url_id]}";

// update the views table
$q = "UPDATE views SET url_id=$current_url_id WHERE user_id=$user_id";
$res = mysql_query($q);
?>

Thanks a lot for help

It's working

But It is displaying error,
Strict Standards: Only variables should be passed by reference, for following code line number 47,48

// the last ID in the array (to know when to start from the beginning)
$websites_array_last_id = end(array_keys($websites_array));

It is a warning not an error, the script gets executed anyway but it just complaining that the coding does not follow the strict rules. I think if you change it to the code below you should be fine. In the production version turn off the display of warnings and errors.

$temp = array_keys($websites_array);
$websites_array_last_id = end($temp);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.