Start New Discussion within our Web Development Community

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

When records ended start from first record

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.

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.

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


    $_SESSION['table_start'] = 0;
$this_start = $_SESSION['table_start'];

$sql = "SELECT ... FROM ... ORDER BY ... LIMIT $this_start, 1";

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

// 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.

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)

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.

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

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

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?

Darn, we're back to this. A framebreaker code may have js check whether 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:


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


<img src="" />

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?



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

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

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.

Thanks broj1


Working fine now.

but body tag is getting disturbed.

other sites style is forcely working on body tag

Thanks broj1

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

This article has been dead for over six months. Start a new discussion instead.