Jump to content

how to do echo so shows on more page refreshes


perryeb

Recommended Posts

Need help with some whiles, etc., and .php page refreshes.

 

$num; Comes from mysql_num_rows from a database table's regular 'select'. And gets the row id numbers for every row that the id numbers are listed in order.

 

$totalrows comes from another select but is select * instead. And pulls the total rows in the whole table instead there are 49 rows in the whole table.

 

I'm trying to get the page to show 5 table rows at a time like the following, but then when the page is still open on next page refresh to show the next 5 rows and continue that way.

 

This following:

 

 

PHP Code:

<?php

$i=$num;

if

($num == 5);

{echo "The number is " . $num . "<br />";}

do

  {$i = $i + 5;

echo "The number is " . $i . "<br />";

  }

while ($i<$totalrows);

?>

 

Brings up these results:

The row number is 10

The row number is 15

The row number is 20

The row number is 25

The row number is 30

The row number is 35

The row number is 40

The row number is 45

The row number is 50

 

Shows all these on the page at the same time. I'm trying to get these to only show the 5 rows after every page refresh instead. Please let me know how to do that, and whether it can be done without any cookies.

 

I don't know if there is a better way to show the first 5 separate like I have it.

 

Thank you very much for your help.     

 

Link to comment
Share on other sites

Thanks a lot you guys please don't desert me or others to help, I'm not getting any help, and really need to get some help with this. Haven't been able to get help with this for a long time where I tried elsewhere.

 

No I don't need or want any pagination of any kind thanks anyway. I'm not doing more than part of one page. It's only to put 5 rows of data on a page only. At a time, after each page refresh, that's it and that's all. Please let me know your thoughts on my question, without any pagination needed whatsoever. Thank you very much for your help.

Link to comment
Share on other sites

Once I can get these row numbers working on part of a page after page refresh at a time, instead of all these on a page at the same time like it is doing now. Then I'll be able to format any needed html for it thanks.

Link to comment
Share on other sites

In other words I'm trying to get this to show on part of the .php page first time

The row number is 5

 

after a page refresh this replaces that on the same page next:

The row number is 10

 

and so on after next page refreshes, that's as simple my question was about. Am not asking anything hard, just asking for opinions with that first post and question, don't need anything complicated like pagination thanks a lot.

Link to comment
Share on other sites

In other words I'm trying to get this to show on part of the .php page first time

The row number is 5

 

after a page refresh this replaces that on the same page next:

The row number is 10

 

Ahhh. Understood.

 

It would depend on where the number is coming from. Can you post some code?

 

There's ways to increment client side, server side, or in the db itself

 

 

Link to comment
Share on other sites

Thank you so much someone understands me woohooo! I listed it's coming from regular selects, but sounds like  you want more. I'd like to keep all the code in php if possible. Here's the rest other than the bottom echos:

 

$getids = mysql_real_escape_string('id')OR die('Cannot get row ids: ' . mysql_error());   

$selectthesecolumns = "SELECT url, webname, description FROM `business` WHERE `id` >= $getids ORDER BY `id` LIMIT 5";

$pullcolumns = mysql_query($selectthesecolumns) OR die('Error pulling a database column: ' . mysql_error());

$num = mysql_num_rows($pullcolumns); //number of rows is actually stored in pull columns I believe, from rows from select area

$countit =  mysql_query("SELECT * FROM `business`");     

$totalrows = mysql_num_rows($countit) OR die('Error pulling total database rows: ' . mysql_error());

 

$i=$num;

if

($num == 5);

{echo "The number is " . $num . "<br />";}

do

  {$i = $i + 5;

echo "The number is " . $i . "<br />";

  }

while ($i<$totalrows);

Link to comment
Share on other sites

I mean here it is:

 

$getids = mysql_real_escape_string('id')OR die('Cannot get row ids: ' . mysql_error());   

$selectthesecolumns = "SELECT url, webname, description FROM `business` WHERE `id` >= $getids ORDER BY `id` LIMIT 5";

$pullcolumns = mysql_query($selectthesecolumns) OR die('Error pulling a database column: ' . mysql_error());

$num = mysql_num_rows($pullcolumns); //the different rows is actually stored in pullcolumns

$countit =  mysql_query("SELECT * FROM `business`");     

$totalrows = mysql_num_rows($countit) OR die('Error pulling total database rows: ' . mysql_error()); //How many rows total in whole table.

 

$i=$num;

if

($num == 5);

{echo "The number is " . $num . "<br />";}

do

  {$i = $i + 5;

echo "The number is " . $i . "<br />";

  }

while ($i<$totalrows);

Link to comment
Share on other sites

Maybe something like this would work.

session_start();
if(isset($_SESSION['currentrows'])) {
$a=$_SESSION['currentrows'];
}
ELSE {
$a=1;
}
$b=5;	//amount to change
$c = $a + $b;
//add query here//
echo "Show records WHERE id>=$a and id<$c"; 
//add session update after query//
$_SESSION['currentrows']=$c;

Link to comment
Share on other sites

Thank you. You have to understand I don't know a lot about php, the rest I did took me a few weeks.

 

Can you please tell me what you mean by 'current rows' because I don't have any values with that name, could you show me it should be something I already have, or is something like I already have?

Also,  have done a little work with cookies before but not much at all, when you say 'add the query here', and 'add session update after query' I have no idea what those mean. Can you explain those more fully for me what you mean otherwise there will be no way for me to know. Thank you very much anyone for giving me some help I need.

 

 

session_start();

if(isset($_SESSION['currentrows']))

{$a=$_SESSION['currentrows'];}

ELSE {$a=1;}

$b=5; //amount to change

$c = $a + $b; //add query here//

echo "Show records WHERE id>=$a and id<$c";

//add session update after query//

$_SESSION['currentrows']=$c;

Link to comment
Share on other sites

You are just setting a session variable that will hold the value while the page gets refreshed.  You can name it anything.  If you want to use cookies that's fine.  Just store the number where I've got the session lines and update the value.  I'm no expert at any of this but I assume you are getting data from a DB table so it might look like this.  I'm sure setting another session (or cookie) for total records could bring total rows to the top of the page but I will just do a separate count from same table.  Hey sorry for not using your code.

$cntrecords = mysql_query("SELECT COUNT(id) as totalrows FROM myrecords ");
   WHILE($cntr = mysql_fetch_array($cntrecords)){
$totalrows=$cntr['totalrows'];
}  
session_start();
if(isset($_SESSION['currentrows'])) {
$a=$_SESSION['currentrows'];
}
ELSE {
$a=1;
}
$b=5;	//amount to change

$c = $a + $b;
IF ($c<$totalrows){
$c=$c;
}
ELSE{
$c=$totalrows+1;
}
//add query here//

$getrecords = mysql_query("SELECT * FROM myrecords WHERE id>=$a and id<$c ");
   WHILE($gr = mysql_fetch_array($getrecords)){
$rname=$gr['recordname'];
echo "Record Name $rname <br />";
}
//add session update after query//
$_SESSION['currentrows']=$c;

Link to comment
Share on other sites

I added this just like you have it but with cookies instead, since I doubt a lot I'll know what I'm doing with the other.

 

$cntrecords = mysql_query("SELECT COUNT(id) as totalrows FROM `business`")OR die('Error pulling cntrecords: ' . mysql_error()); 

while

($cntr = mysql_fetch_array($cntrecords))

{$totalrows=$cntr['totalrows'];} 

if(isset($_COOKIE['currentrows']))

{$a=$_COOKIE['currentrows'];}

ELSE {$a=1;}$b=5; //amount to change $c = $a + $b;

IF ($c<$totalrows){$c=$c;

 

Nothing at all is appearing on the page. No error messages also. Was total rows in select supposed to be listed another way? Was I supposed to connect it to something I couldn't see you had, or to something I already had? Please let me know thank you very much.

Link to comment
Share on other sites

If you follow the logic, totalrows starts at 1 and then is updated to the variable $c that's updated each time around.  If you would, please try this version using the session.

$cntrecords = mysql_query("SELECT COUNT(id) as totalrows FROM business ");
   WHILE($cntr = mysql_fetch_array($cntrecords)){
$totalrows=$cntr['totalrows'];
}  
session_start();
if(isset($_SESSION['currentrows'])) {
$a=$_SESSION['currentrows'];
}
ELSE {
$a=1;
}
$b=5;	//amount to change

$c = $a + $b;
IF ($c<$totalrows){
$c=$c;
}
ELSE{
$c=$totalrows+1;
}
//add query here//
$getrecords = mysql_query("SELECT url, webname, description FROM business WHERE id>=$a and id<$c ");
   WHILE($gr = mysql_fetch_array($getrecords)){
$pname=$gr['webname'];
$url=$gr['url'];
$desc=$gr['description'];
echo "<p>Page Name <a href='$url'>$pname</a><br />$desc</p>";
}
//add session update after query//
$_SESSION['currentrows']=$c;

Link to comment
Share on other sites

I don't use cookies but I would guess it would look like this.

$cntrecords = mysql_query("SELECT COUNT(id) as totalrows FROM business ");

  WHILE($cntr = mysql_fetch_array($cntrecords)){

$totalrows=$cntr['totalrows'];

setcookie();

if(isset($_COOKIE['currentrows'])) {

$a=$_COOKIE['currentrows'];

}

ELSE {

$a=1;

}

$b=5; //amount to change

 

$c = $a + $b;

IF ($c<$totalrows){

$c=$c;

}

ELSE{

$c=$totalrows+1;

}

//add query here//

$getrecords = mysql_query("SELECT url, webname, description FROM business WHERE id>=$a and id<$c ");

  WHILE($gr = mysql_fetch_array($getrecords)){

$pname=$gr['webname'];

$url=$gr['url'];

$desc=$gr['description'];

echo "<p>Page Name <a href='$url'>$pname</a><br />$desc</p>";

}

//add session update after query//

$_COOKIE['currentrows']=$c;

Link to comment
Share on other sites

It didn't like this setcookie();

So I just removed it.

It's showing the 3rd database row only and when I click on refresh, it's not showing the next 5 rows but still at 1 row only the third.

You see before I had it showing 5 rows, but they were all showing on the page at the same time. Instead of showing 5 rows at a time on each refresh like I was trying to do.

Do you know what is causing this? Or can you put explanations next to your values, so I know you're logic. I'm not good enough at this to know what your trying to do and your logic is without you putting explanations next to them or your knowing what's wrong. Thanks a lot for your help it is extremely appreciated. 

 

Link to comment
Share on other sites

I tried sessions trying to compromise. I figure I can always change to cookies later if you have it working with sessions. Sounds like you like working with sessions, so I can always change later if I need to. Your sessons like you said is working better than the cookies example. But after 3 page refreshes everything disappears with your session one. So your session one looks like it's getting closer. Hopefully you know what that means. Thanks a lot.

Link to comment
Share on other sites

The session one like it is, shows 3 table rows the first time, then the next 2 rows directly after those on next page refresh, then the next 2 rows directly after the last 2 on next page page refresh. The after that do not show, nothing shows on the page after that, there are 49 rows in the table.

 

Something else is happening though. When each table rows shows that I just said. It's printing the whole page 3 times as well. So it's not just sticking the 3 rows on the page. It's throwing 3 pages on top of each other to show the 3 rows. I can send you the basic template I'm using source in a pm if you want. Where you can add your own database column and password if you want. Even though what I first posted is not showing 5 rows at a time on each page refresh but all at once on the page, maybe there are a few ideas in that to combine with what you're doing. Let me know if you have any question on those, if you needed me to tell you something in those I just said more clear. Thank you very much.

 

 

Link to comment
Share on other sites

I assume "id" is an auto-increment value in your database table.  I think though it might be better to just get the starting id number and use limit 5. See how this does.  If "id" is not a sequential number or there are gaps from deleted records then you would need to go about gettting the "Next 5" differently by getting the id of the last record and working from there.

$cntrecords = mysql_query("SELECT COUNT(id) as totalrows FROM business ");
   WHILE($cntr = mysql_fetch_array($cntrecords)){
$totalrows=$cntr['totalrows'];
}  
session_start();
if(isset($_SESSION['currentrows'])) {
$a=$_SESSION['currentrows'];
}
ELSE {
$a=1;
}
$b=5;	//amount to change

$c = $a + $b;
IF ($c<$totalrows){
$c=$c;
}
ELSE{
$c=$totalrows+1;
}
//add query here//
$getrecords = mysql_query("SELECT url, webname, description FROM business WHERE id>=$a limit 5");
   WHILE($gr = mysql_fetch_array($getrecords)){
$pname=$gr['webname'];
$url=$gr['url'];
$desc=$gr['description'];
echo "<p>Page Name <a href='$url'>$pname</a><br />$desc</p>";
}
//add session update after query//
$_SESSION['currentrows']=$c;

Link to comment
Share on other sites

You keep getting closer. I got the table down to 24 rows to make it easier for me to test.

Youre now pulling 5 rows at a time is good.

First time was 1-5 pages overlapping listing the 5 rows. Some of the pulling of 5 rows at a time overlap also. What I mean....

Next page refresh was 4-8 rows.

Next page refresh was 6-10 rows.

Next page refresh was 8-12 rows. So it back up a few rows and then pulls 5 rows from there.

When it gets to the the 8-12 rows it stops on that after all next page refreshes and doesn't keep going.

I'll send that in a pm if that is too hard to explain so you can see what I mean.

Link to comment
Share on other sites

Because id values might vary because of deleted records, it's best not to just add 5 to a number and assume you have a record by that id as we've been doing.  The script below will get the last id shown and then the next id and set it to the session variable.  I added a bunch of comments in the code.

$cntrecords = mysql_query("SELECT COUNT(id) as totalrows FROM business ");
   WHILE($cntr = mysql_fetch_array($cntrecords)){
$totalrows=$cntr['totalrows'];
}  
session_start(); 
///I'm changing the SESSION name to 'firstid' for "First ID" because it represents the first id we're looking for.  The first line below is just saying hey, is there a Session called 'firstid'? If there is a session, the variable $a is set to equal the session amount.  The ELSE statement is saying if Session 'firstid' is not found, set $a=1 
if(isset($_SESSION['firstid'])) {
$a=$_SESSION['firstid'];
}
ELSE {
$a=1;
}
// This variable is not needed any more as we're not adding values to get the next group.  You could keep it to use as your "limit" variable as I did below or hard code the limit to any number.
$b=5;	//Number of Records to show

// I added  the variable $i just for showing result count.  Remove if you wish.
$i=0;
// I've added id to the values to grab and set it to the variable $id.  As I'm using this value after this query, $id will represent the id value of the last record from this query.
$getrecords = mysql_query("SELECT id, url, webname, description FROM business WHERE id>=$a ORDER BY id ASC limit $b");
   WHILE($gr = mysql_fetch_array($getrecords)){
$pname=$gr['webname'];
$url=$gr['url'];
$desc=$gr['description'];
$id=$gr['id'];
$i++;
echo "<p>#$i Page Name <a href='$url'>$pname</a><br />$desc</p>";
}
//Here we're looking for the next id after that last "$id" value we got above.
$nextrecord = mysql_query("SELECT * FROM ".$conf['tbl']['users']." WHERE ID>$id ORDER BY ID ASC limit 1");
   WHILE($nr = mysql_fetch_array($nextrecord)){
$next=$nr['ID'];
}
//And finally we update our session to equal the "next" id to look for//
$_SESSION['firstid']=$next;

Link to comment
Share on other sites

What you asked me before, the first code I listed yes it was by id that was automatic increment, so that if there were deleted records it would still pull any ids. Back to testing your last. If you need to break away to start again another day I understand. Maybe you're up late like I am.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.