Jump to content


Photo

Pagation with mySQL results


  • Please log in to reply
54 replies to this topic

#21 coolaid

coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 16 July 2006 - 02:10 PM

you question is too much for you to take on - is my conclusion.

you should take 1 step at a time. you need to learn how to use mysql_query and all those useful mysql queries first. - not a quick task.

then you can look at pagination scripts and learn to embedd them into your own scripts

#22 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 02:15 PM

Well, I know most basic things... and doesn't it piss you off when your trying to learn something new and they give you a script, that doesn't even work to connect to a database!
<?php 

	@mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER"); 
	@mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB"); 

	$limit		  = 25;				
	$query_count	= "SELECT count(*) FROM table";	 
	$result_count   = mysql_query($query_count);	 
	$totalrows	  = mysql_num_rows($result_count);  

	if(empty($page)){ 
		$page = 1; 
	} 
		 

	$limitvalue = $page * $limit - ($limit);  
	$query  = "SELECT * FROM table LIMIT $limitvalue, $limit";		 
	$result = mysql_query($query) or die("Error: " . mysql_error());  

	if(mysql_num_rows($result) == 0){ 
		echo("Nothing to Display!"); 
	} 

	$bgcolor = "#E0E0E0"; // light gray 

	echo("<table>"); 
	 
	while($row = mysql_fetch_array($result)){ 
		if ($bgcolor == "#E0E0E0"){ 
			$bgcolor = "#FFFFFF"; 
		}else{ 
			$bgcolor = "#E0E0E0"; 
		} 

	echo("<tr bgcolor=".$bgcolor.">n<td>"); 
	echo($row["users"]); 
	echo("</td>n<td>"); 
	echo($row["usersID"]); 
	echo("</td>n</tr>"); 
	} 

	echo("</table>"); 

	if($page != 1){  
		$pageprev = $page--; 
		 
		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
	}else{ 
		echo("PREV".$limit." "); 
	} 

	$numofpages = $totalrows / $limit;  
	 
	for($i = 1; $i <= $numofpages; $i++){ 
		if($i == $page){ 
			echo($i." "); 
		}else{ 
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
		} 
	} 


	if(($totalrows % $limit) != 0){ 
		if($i == $page){ 
			echo($i." "); 
		}else{ 
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
		} 
	} 

	if(($totalrows - ($limit * $page)) > 0){ 
		$pagenext = $page++; 
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
	}else{ 
		echo("NEXT".$limit);  
	} 
	 
	mysql_free_result($result); 

?>

Trying adding your details, and watch the script REJECT THE CONNECTION :@

#23 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 02:18 PM

Hehe, how about you enter yours how you would do it, and ill tell ya whats wrong lol.

Because the script looks fine :)

#24 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 02:29 PM

Fine
<?php 

	@mysql_connect($localhost, $indust, $mypasshere) or die("ERROR--CAN'T CONNECT TO SERVER"); 
	@mysql_select_db($indust_tutorial) or die("ERROR--CAN'T CONNECT TO DB"); 

	$limit		  = 25;				
	$query_count	= "SELECT count(*) FROM table";	 
	$result_count   = mysql_query($query_count);	 
	$totalrows	  = mysql_num_rows($result_count);  

	if(empty($page)){ 
		$page = 1; 
	} 
		 

	$limitvalue = $page * $limit - ($limit);  
	$query  = "SELECT * FROM table LIMIT $limitvalue, $limit";		 
	$result = mysql_query($query) or die("Error: " . mysql_error());  

	if(mysql_num_rows($result) == 0){ 
		echo("Nothing to Display!"); 
	} 

	$bgcolor = "#E0E0E0"; // light gray 

	echo("<table>"); 
	 
	while($row = mysql_fetch_array($result)){ 
		if ($bgcolor == "#E0E0E0"){ 
			$bgcolor = "#FFFFFF"; 
		}else{ 
			$bgcolor = "#E0E0E0"; 
		} 

	echo("<tr bgcolor=".$bgcolor.">n<td>"); 
	echo($row["users"]); 
	echo("</td>n<td>"); 
	echo($row["usersID"]); 
	echo("</td>n</tr>"); 
	} 

	echo("</table>"); 

	if($page != 1){  
		$pageprev = $page--; 
		 
		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
	}else{ 
		echo("PREV".$limit." "); 
	} 

	$numofpages = $totalrows / $limit;  
	 
	for($i = 1; $i <= $numofpages; $i++){ 
		if($i == $page){ 
			echo($i." "); 
		}else{ 
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
		} 
	} 


	if(($totalrows % $limit) != 0){ 
		if($i == $page){ 
			echo($i." "); 
		}else{ 
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
		} 
	} 

	if(($totalrows - ($limit * $page)) > 0){ 
		$pagenext = $page++; 
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
	}else{ 
		echo("NEXT".$limit);  
	} 
	 
	mysql_free_result($result); 

?>

thats my detials, obviously my password isn't that but thats my login username and that the database name... so any ideas now?

#25 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 02:40 PM

Yep, lol.

You dont have the dollar sign at the front when just entering text, otherwise it makes it a varible which is missing.

try this:

<?php
$localhost = "localhost";


$username = "yourusernamehere";  // EDIT THIS
$password = "yourpasswordhere";  // EDIT THIS

# Dont touch below now :D

	@mysql_connect($localhost, $username, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
	@mysql_select_db($indust_tutorial) or die("ERROR--CAN'T CONNECT TO DB");

	$limit		  = 25;				
	$query_count	= "SELECT count(*) FROM table";	
	$result_count   = mysql_query($query_count);	
	$totalrows	  = mysql_num_rows($result_count);  

	if(empty($page)){
		$page = 1;
	}
		

	$limitvalue = $page * $limit - ($limit);  
	$query  = "SELECT * FROM table LIMIT $limitvalue, $limit";		
	$result = mysql_query($query) or die("Error: " . mysql_error());  

	if(mysql_num_rows($result) == 0){
		echo("Nothing to Display!");
	}

	$bgcolor = "#E0E0E0"; // light gray

	echo("<table>");
	
	while($row = mysql_fetch_array($result)){
		if ($bgcolor == "#E0E0E0"){
			$bgcolor = "#FFFFFF";
		}else{
			$bgcolor = "#E0E0E0";
		}

	echo("<tr bgcolor=".$bgcolor.">n<td>");
	echo($row["users"]);
	echo("</td>n<td>");
	echo($row["usersID"]);
	echo("</td>n</tr>");
	}

	echo("</table>");

	if($page != 1){  
		$pageprev = $page--;
		
		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
	}else{
		echo("PREV".$limit." ");
	}

	$numofpages = $totalrows / $limit;  
	
	for($i = 1; $i <= $numofpages; $i++){
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
		}
	}


	if(($totalrows % $limit) != 0){
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
		}
	}

	if(($totalrows - ($limit * $page)) > 0){
		$pagenext = $page++;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
	}else{
		echo("NEXT".$limit);  
	}
	
	mysql_free_result($result);

?>


#26 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 02:47 PM

Yeah thats right, that now connects to the server... butttt the name of the database is correct (indust_tutorial) but even so it won't allow a connection to the database. But server connection is gwudd :lol:

Edited by Rich69, 16 July 2006 - 02:49 PM.


#27 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 02:53 PM

oh crap, forgot the table name....

<?php
$localhost = "localhost";


$username = "yourusernamehere";  // EDIT THIS
$password = "yourpasswordhere";  // EDIT THIS
$tablename = "indust_tutorial" // EDIT THIS


# Dont touch below now :D

	@mysql_connect($localhost, $username, $password) or die(mysql_error());
	@mysql_select_db($tablename) or die(mysql_error());

	$limit		  = 25;				
	$query_count	= "SELECT count(*) FROM table";	
	$result_count   = mysql_query($query_count);	
	$totalrows	  = mysql_num_rows($result_count);  

	if(empty($page)){
		$page = 1;
	}
		

	$limitvalue = $page * $limit - ($limit);  
	$query  = "SELECT * FROM table LIMIT $limitvalue, $limit";		
	$result = mysql_query($query) or die("Error: " . mysql_error());  

	if(mysql_num_rows($result) == 0){
		echo("Nothing to Display!");
	}

	$bgcolor = "#E0E0E0"; // light gray

	echo("<table>");
	
	while($row = mysql_fetch_array($result)){
		if ($bgcolor == "#E0E0E0"){
			$bgcolor = "#FFFFFF";
		}else{
			$bgcolor = "#E0E0E0";
		}

	echo("<tr bgcolor=".$bgcolor.">n<td>");
	echo($row["users"]);
	echo("</td>n<td>");
	echo($row["usersID"]);
	echo("</td>n</tr>");
	}

	echo("</table>");

	if($page != 1){  
		$pageprev = $page--;
		
		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
	}else{
		echo("PREV".$limit." ");
	}

	$numofpages = $totalrows / $limit;  
	
	for($i = 1; $i <= $numofpages; $i++){
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
		}
	}


	if(($totalrows % $limit) != 0){
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
		}
	}

	if(($totalrows - ($limit * $page)) > 0){
		$pagenext = $page++;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
	}else{
		echo("NEXT".$limit);  
	}
	
	mysql_free_result($result);

?>


Sigh. :lol:

#28 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 02:59 PM

Haha now we have a parse error

"Parse error: syntax error, unexpected '@' in ***************************************** on line 12"

I stared out the path to the file :lol: If you was wondering what the stars are all about.

But yes, there's now a parse error and line 12 is:

@mysql_connect($localhost, $username, $password) or die(mysql_error());

for reference.

Edited by Rich69, 16 July 2006 - 03:04 PM.


#29 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 03:03 PM

**** it :lol:

Replace:

$tablename = "indust_tutorial" // EDIT THIS

With:

$tablename = "indust_tutorial"; // EDIT THIS

Im very tired :P

#30 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 03:17 PM

haha yeah me to, and there's another error :lol:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ****************************on line 18
Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'table LIMIT 0, 25' at line 1


:P

#31 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 03:27 PM

Add backticks ` around the table..

e.g

`tablename`


#32 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 03:50 PM

Ok now were getting some were!! Check this out http://www.r-industries.net/show.php But! when i add over 20 records it just stays the same like that one the page number to go to for more records, it doesn't display a new page. Here the code of have so far
<?php
$localhost = "localhost";


$username = "indust";  // EDIT THIS
$password = "*******";  // EDIT THIS
$tablename = "indust_tutorial"; // EDIT THIS


# Dont touch below now :D

	@mysql_connect($localhost, $username, $password) or die(mysql_error());
	@mysql_select_db($tablename) or die(mysql_error());

	$limit		  = 20;				
	$query_count	= "SELECT count(*) FROM `tutorial`";	
	$result_count   = mysql_query($query_count);	
	$totalrows	  = mysql_num_rows($result_count);  

	if(empty($page)){
		$page = 1;
	}
		

	$limitvalue = $page * $limit - ($limit);  
	$query  = "SELECT * FROM `tutorial` LIMIT $limitvalue, $limit";		
	$result = mysql_query($query) or die("Error: " . mysql_error());  

	if(mysql_num_rows($result) == 0){
		echo("Nothing to Display!");
	}

	$bgcolor = "#E0E0E0"; // light gray

	echo("<table>");
	
	while($row = mysql_fetch_array($result)){
		if ($bgcolor == "#E0E0E0"){
			$bgcolor = "#FFFFFF";
		}else{
			$bgcolor = "#E0E0E0";
		}

	echo("<tr bgcolor=".$bgcolor."><td>");
	echo($row["name"]);
	echo("</td><td>");
	echo($row["url"]);
	echo("</td></tr>");
	}

	echo("</table>");

	if($page != 1){  
		$pageprev = $page--;
		
		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
	}else{
		echo("PREV".$limit." ");
	}

	$numofpages = $totalrows / $limit;  
	
	for($i = 1; $i <= $numofpages; $i++){
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
		}
	}


	if(($totalrows % $limit) != 0){
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
		}
	}

	if(($totalrows - ($limit * $page)) > 0){
		$pagenext = $page++;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
	}else{
		echo("NEXT".$limit);  
	}
	
	mysql_free_result($result);

?>

Im so so close :P :lol:

Shit I kept my password in by misstake :P

Edited by Rich69, 16 July 2006 - 03:55 PM.


#33 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 04:05 PM

Ok, this took me a while to spot but replace:

$query_count	= "SELECT count(*) FROM `tutorials`";

With..

$query_count	= "SELECT * FROM `tutorials`";


#34 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 04:11 PM

Ok but its still not doing anything when i add a new record, and i now get this error aswell as the records showing up.

"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/indust/public_html/show.php on line 18"

Check the link if you don't get me, http://r-industries.net/show.php

:wacko:

#35 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 04:16 PM

AHAHAHHAHAH, sorry, when i tried it on localhost, i changed the table name.

Sorry, my fault.

$query_count	= "SELECT * FROM `tutorial`";

Replace it with that.

#36 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 04:23 PM

Haha no problem and I think its actually working!! All i want now is for it to be Prev 1 2 3 4 Next instead of

having the 20 either side like so: PREV20 1 2 NEXT20

butttt you can do that tomorrow because I'm going off now, and im not sure if the numbering works correctly I'll add some more records and you can try it http://r-industries.net/show.php

#37 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 04:25 PM

It worked fine on localhost :lol:

ps. to remove what you want...

Replace:
if($page != 1){  
		$pageprev = $page--;
		
		echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> ");  
	}else{
		echo("PREV".$limit." ");
	}

with...

if($page != 1){  
		$pageprev = $page--;
		
		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV</a> ");  
	}else{
		echo("PREV");
	}


aannnddd...
if(($totalrows - ($limit * $page)) > 0){
		$pagenext = $page++;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
	}else{
		echo("NEXT".$limit);  
	}

with.....

if(($totalrows - ($limit * $page)) > 0){
		$pagenext = $page++;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");  
	}else{
		echo("NEXT");  
	}

ps. i fixed a small bug, one of the urls had "&" in where it should have been "?".

Edited by .Matt, 16 July 2006 - 04:26 PM.


#38 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 04:32 PM

Ok thats took the 20 away cheers but seriosuly check this out http://r-industries.net/show.php

click next, it doesn't do anything :lol:

#39 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 16 July 2006 - 04:34 PM

ok, canyou post the code your using please, so many things have changed ive lost track.

#40 Vandalised

Vandalised

    Jedi In Training

  • Members
  • PipPip
  • 307 posts
  • Gender:Male
  • Location:UK
  • Interests:Most sports, football, cricket, hockey, tennis, golf. Web design, graphic design (photoshop) Socialising with friends, going to clubs, bars, concerts.

Posted 16 July 2006 - 04:38 PM

Yeah sure
<?php

$localhost = "localhost";





$username = "indust";  // EDIT THIS

$password = "++++++++";  // EDIT THIS

$tablename = "indust_tutorial"; // EDIT THIS





# Dont touch below now :D



	@mysql_connect($localhost, $username, $password) or die(mysql_error());

	@mysql_select_db($tablename) or die(mysql_error());



	$limit		  = 20;				

	$query_count	= "SELECT * FROM `tutorial`";	

	$result_count   = mysql_query($query_count);	

	$totalrows	  = mysql_num_rows($result_count);  



	if(empty($page)){

		$page = 1;

	}

		



	$limitvalue = $page * $limit - ($limit);  

	$query  = "SELECT * FROM `tutorial` LIMIT $limitvalue, $limit";		

	$result = mysql_query($query) or die("Error: " . mysql_error());  



	if(mysql_num_rows($result) == 0){

		echo("Nothing to Display!");

	}



	$bgcolor = "#E0E0E0"; // light gray



	echo("<table>");

	

	while($row = mysql_fetch_array($result)){

		if ($bgcolor == "#E0E0E0"){

			$bgcolor = "#FFFFFF";

		}else{

			$bgcolor = "#E0E0E0";

		}



	echo("<tr bgcolor=".$bgcolor."><td>");

	echo($row["name"]);

	echo("</td><td>");

	echo($row["url"]);

	echo("</td></tr>");

	}



	echo("</table>");



	if($page != 1){  

		$pageprev = $page--;

		

		echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV</a> ");  

	}else{

		echo("PREV");

	}



	$numofpages = $totalrows / $limit;  

	

	for($i = 1; $i <= $numofpages; $i++){

		if($i == $page){

			echo($i." ");

		}else{

			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

		}

	}





	if(($totalrows % $limit) != 0){

		if($i == $page){

			echo($i." ");

		}else{

			echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

		}

	}



	if(($totalrows - ($limit * $page)) > 0){

		$pagenext = $page++;

		  

		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");  

	}else{

		echo("NEXT");  

	}

	

	mysql_free_result($result);



?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users