@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 ";

while ($row_v=mysql_fetch_assoc($result_v)) 


// 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)";
    $update_query="UPDATE views SET  url_id=$url_id WHERE user_id=$user_id";

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


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;

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

Edited by broj1


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);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.