We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,539 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to get records one by one on page refresh

How to get records from databse in ascending order new record on page refresh each time

When records ended start from first record

5
Contributors
34
Replies
1 Week
Discussion Span
5 Months Ago
Last Updated
35
Views
vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Random Query In Ascending Order

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Do you have a primary key or unique field in the table and what type it is? This is cruical for some control of which record to access.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

primary key for field id

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Use a session variable to keep track of the last viewed record between sessions.

Then use MySQL LIMIT clause to set an offset and limit the results to one record. MySQL Select (limit) reference

When you end up with an empty result set, reset back to record one.

madCoder
Junior Poster
165 posts since Feb 2010
Reputation Points: 31
Solved Threads: 45
Skill Endorsements: 0

Something like this maybe. It's a little nasty - and perhaps only increment on success from DB.

session_start();

if(!isset($_SESSION['table_start'])){
    $_SESSION['table_start'] = 0;
}else{
    $_SESSION['table_start']++;
}
$this_start = $_SESSION['table_start'];


$sql = "SELECT ... FROM ... ORDER BY ... LIMIT $this_start, 1";
diafol
Keep Smiling
Moderator
10,611 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

OK, you forgot to mention what type it is. So if it is an integer and autoincreased by the DB then the id fields probably increase as you add records and you can use this approach:

whenever you read and display the record save the id in the session. If there is no id in the session you are displaying the first record otherwise you just go for the next record. The rest is documended in the code below:

// start the session
session_start();

// if this is the first run of your script the id has not been stored in the session yet
if(!isset($_SESSION['current_id'])) {

    // this query will select the record with the lowest id, presumably the first record
    $query = 'SELECT * FROM table_name ORDER BY id ASC LIMIT 1';

} else {

    // this query will read the record just after the current id
    $query = 'SELECT * FROM table_name WHERE id > {$_SESSION['current_id']} ORDER BY id ASC LIMIT 1';
}

// read the row, display it and store the current id in the session
...
$_SESSION['current_id'] = $row['id']

More precise solution would be to have a field with timestamp so you could be always sure that the displayed records follow correctly each other.

Hopefully this is what you wanted to know.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

I used ORDER BY RAND(), id ASC LIMIT 0,1

But it displays random records

I want serially like record_1 , record_2, record_3, record_4, record_5

one record at a time (displaying all records equally)

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Why are you ordering by RAND() if you want ordered results? The way I see it you have a choice with regard to modifying the WHERE or modifying the LIMIT clause.

As broj states - WHERE `id` > $var LIMIT 0,1
or - ORDER BY id ASC LIMIT $var,1

Not sure which would be the fastest. I think broj may have it - but you'd need to test.

diafol
Keep Smiling
Moderator
10,611 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

without, RAND(), only one and same record is displayed. it is not going from record_1 to record_2

Only record_1 is displayed all the time

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Did you try my code? Or broj1's?

diafol
Keep Smiling
Moderator
10,611 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

I used both codes, but same problem,
my database fields are,
id, userid, url, active
and query is,

$sql="SELECT * FROM websites WHERE userid=$id AND active=1 ORDER BY id ASC LIMIT 1"; 

I want to display one url at a time. my curl function converts that url into website.
thus I display external website on my site

I am doing this because framebreakers stops website displaying in iframe tag and redirect it from my site to original site

Help me to display one url at a time

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Now I get what you want. You do not want to display a contents of one record on one page but a page with a URL which is stored in a record. Is that true? Well, if it is the best way I know would be using iframes which you do. But if the framebreakers on those pages redirect ot original page then as far as I am aware you loose control. I am not aware of a way how to avoid this (actually have no much experience in it). Maybe someone else on this forum knows more about that?

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

Darn, we're back to this. A framebreaker code may have js check whether window.top is the same as window.self. If not then, a redirect ensues. So iframe content can't really be changed. Other sites may have X-Frame-Options: SAMEORIGIN as a response header. Again you're stuffed.

So cURL may be required in order to place the site into a div container. But as mentioned on a previous thread, 'domain-less' file references will try to access your server for the relevant files. The only way I can think of to circumvent this is to replace all file references to include the host domain:

e.g.

<img src="images/myimg.png" /> 

to

<img src="http://www.example.com/images/myimg.png" />

A preg replace may be able to make wholesale changes to the string before you echo it out.

Also as previously mentioned, the CSS and JS files, if you can get them resolved, may cause clashes with your own site resources. Additonally, if you have certain formatting, but the remote css file doesn't, you may find that your 'embedded' site looks totally different to the original. I can't think of a suitable workaround for this. DO these 'embedded sites' need to be functional? Could you not use images?

Headache.

diafol
Keep Smiling
Moderator
10,611 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

Thanks

My cURL code is working fine. It displays website inside DIV even framebreaker exists.

I'm having problem with mysql query.

$sql="SELECT * FROM websites WHERE userid=$id AND active=1 ORDER BY id ASC LIMIT 1";

I need help to display one record at a time and next one record on pagerefresh.

my query without rand() displays only one record all the time and with rand() displaying all records but not equally

I want to display all records equally one by one

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

if you want to display one by one in sequece like 1,2,3
then i will suggest you to find next

intitially pass 0 as id

$id=0;//this must be not part of fetch script, it may be passed in post or get

now search next maximum active id from pass id

$nextid="select max(id) from websites where active=1 and userid=$id";
$query="select * from websites where active=1 and userid=$nextid";

now run query and return result to your div, and also save nextid in some another div or hidden field

so next time page refreshed, pass that hidden field your fetch script as id

I hope i explained it well

urtrivedi
Posting Virtuoso
1,714 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

This thread isn't solved yet?

My suggestion, as better explained/implemented by @diafol, should have worked with just a bit of tweaking. (You'll need to replace his placeholder sql with a query that retrieves the right fields, etc.)

@vizz, per your own description, you want new content to appear (per user), every time the page is refreshed (assuming the refresh button of the BROWSER, or a JavaScript refresh, or a META tag refresh.) So no form is being submitted and no link is being clicked on. Therefore, the ONLY way to persist information between reloads is with $_SESSION (How PHP Sessions Work and How to Use Them).

NOTE: Sessions require cookies be enabled so make sure you haven't disabled cookies in your web browser's settings.

madCoder
Junior Poster
165 posts since Feb 2010
Reputation Points: 31
Solved Threads: 45
Skill Endorsements: 0

Thanks broj1

$_SESSION

Working fine now.

but body tag is getting disturbed.

other sites style is forcely working on body tag

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

other sites style is forcely working on body tag

said so.

diafol
Keep Smiling
Moderator
10,611 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

Thanks broj1

No worries. But I think I have contributed the least in this thread :-).

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page rendered in 0.1240 seconds using 2.79MB