1,105,263 Community Members

How to get records one by one on page refresh

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

@diafol

Thanks

Your code is also working fine. tested both codes
Thanks for the help

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

TABLE views record last viewed website. (id,user_id,url_id)
How to display next website after viewed last time when $_SESSION is reset?

When records end from table websites it gives error,

Notice: Undefined variable: url_id

Notice: Undefined variable: url

$user_id=$_REQUEST['id'];

if(!isset($_SESSION['current_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 0,1";
} 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 0,1";
}   

$result=mysql_query($query);                        

while ($row=mysql_fetch_assoc($result)) 
{ 
    $url_id=$row["id"];
    $uid=$row["userid"];
    $url=$row["url"];
}

//$_SESSION['current_id'] = $url_id;

$res = mysql_query("SELECT * FROM views where user_id =$user_id");
while ($row_view=mysql_fetch_assoc($res)) 
{
  $viewid = $row_view["id"];
  $view_user_id = $row_view["user_id"];
  $view_url_id = $row_view["url_id"];

}

if (empty($view_url_id)) 
{
    $_SESSION['current_id'] =  $url_id; 
}
else
{
    if($view_url_id < $url_id)
        {
            $_SESSION['current_id'] = $url_id;  
        }
    else 
        {
            $_SESSION['current_id'] = 0;    
        }
}

//display website

$fineurl="http://$url";                                 
$html = Get_Domain_Contents($fineurl);                          
$html = "<base href='{$fineurl}' />" . $html;                
echo $html; 

//update views table

$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); 
}
Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 218 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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.

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

When url comes to the end, reset it to the first record again.

if($num > count($whatever))$num = 0;

Sort of thing

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

@broj1
I have table in database views where last viewed website is updated. Fields of table views are,
id, user_id, url_id

ERROR
When records end from table websites it gives error,

  1. Notice: Undefined variable: url_id
  • line number 38 if($view_url_id < $url_id)
  1. Notice: Undefined variable: url

How to get url next of last viewed, or When url comes to the end, reset it to the first record again.

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

@diafol

I tried your code but little help needed
How to set session using this,

if($num > count($whatever))$num = 0;

$_SESSION['current_id'] = $url_id;

OR

$_SESSION['current_id'] = 0;

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 
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.

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

Thanks
But little problem

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

I am having problems to goto first record on end of records please help me.

How to get record last viewed website and display next website at next time???
There is table views to record last viewed website.

My simple code to redirect to first record on end of records

Database

CREATE TABLE IF NOT EXISTS `websites` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userid` bigint(10) NOT NULL,
`url` varchar(250) NOT NULL,
`active` smallint(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `views` (
`id` mediumint(10) NOT NULL AUTO_INCREMENT,
`user_id` mediumint(10) NOT NULL,
`url_id` mediumint(10) NOT NULL,
PRIMARY KEY (`id`)
)

Add more than 4-5 websites to database websites and test

Get website contents

function Get_Domain_Contents($url){
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;   
}   

display website

$user_id=$_REQUEST['id'];       

if(!isset($_SESSION['current_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 0,1";
} 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 0,1";
}           

$result=mysql_query($query);                        

while ($row=mysql_fetch_assoc($result)) 
{ 
    $url_id=$row["id"];
    $uid=$row["userid"];
    $url=$row["url"];
}   

//$_SESSION['current_id']=$url_id;                      

 if($_SESSION['current_id'] >= count($url_id))
    {
        $_SESSION['current_id'] = 0;    
    }
else
    {
        $_SESSION['current_id']=$url_id;
    }                   

$fineurl="http://$url"  ;                   
$html = Get_Domain_Contents($fineurl);                              
$html = "<base href='{$fineurl}' />" . $html;

echo $html; 
Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 218 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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";
}
Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

@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

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

@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); 
}
Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 218 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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);
?>
Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

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));
Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 218 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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);
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article