How to get records from databse in ascending order new record on page refresh each time When records ended start from first record
How to display next website after viewed last time when $_SESSION is reset?
When the session is reset (i.e. browser is closed) you loose that information. If you want to keep it you have to store it somewhere. One option is on server (in a database or in a file) other option is on users machine (a cookie). It is up to you to decide which one is more convenient.
When records end from table websites it gives error,
Notice: Undefined variable: url_id
Notice: Undefined variable: url
Can you find out which line the notices are being issued? You use these variables in many places.
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
When url comes to the end, reset it to the first record again.
if($num > count($whatever))$num = 0;
Sort of thing
diafol
Keep Smiling
10,662 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
if($_SESSION['current_id'] > count($whatever))$_SESSION['current_id'] = 0;
BUT, this will work only with the method I outlined (LIMIT method), as record #0 will not exist for the WHERE clause.
diafol
Keep Smiling
10,662 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
Why do you have a condition userid !=$user_id in your query? I do not get that.
Anyway, just a guess since I am not sure whether I undertood the problem corectly:
// first get the last ID so you know when to switch for the first id again
// please note that DESC has been used
// LIMIT 1 is enough, no need for LIMT 0,1
$query ="SELECT id FROM websites where userid=$user_id ORDER BY id DESC LIMIT 1";
$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";
}
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
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);
?>
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
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);
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13