Jump to content


Photo

Pagation with mySQL results


  • Please log in to reply
54 replies to this topic

#41 Matthew.

Matthew.

    Official Spammer .Matt

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

Posted 16 July 2006 - 04:45 PM

Ok, fixed and tested.

<?php
$localhost = "localhost";


$username = "user";  // EDIT THIS
$password = "pass";  // EDIT THIS
$tablename = "table"; // 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);  
	$page = $_GET['page'];

	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-1;
		
		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+1;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");  
	}else{
		echo("NEXT");  
	}
	
	mysql_free_result($result);

?>

Should work fine now.

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


#42 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 17 July 2006 - 02:48 AM

Should work fine now.


It works perfect my friend :) I think thats it lol! After 2 days its finally done :ph34r: , and I thought the code was fine? eh hahaha Cheers for all the help buddy anyway. I must owe you or something :)

One question, you know when making a record in a sql database (which I know how to do lol) can you use simple HTML? like stuff for links, <a href='mylink.com'>my web site</a> so would I be able to insert that into a database?

Edited by Rich69, 17 July 2006 - 02:49 AM.


#43 Matthew.

Matthew.

    Official Spammer .Matt

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

Posted 17 July 2006 - 06:39 AM

You would input an url such as "http://google.com" and output it as
echo '<a href="' . $row['url'] . '">link name</a>';

You may want to make another column for link name, or just use the url as the name. Personally i would do it using an array / serailize but lets not go into that :tiphat: :)

#44 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 17 July 2006 - 06:48 AM

Personally i would do it using an array / serailize but lets not go into that :) :D



Haha yeah I think its best we leave it at that :D , I've learnt alot. I've been studing the final code and looking at http://www.w3schools.com/ for more information on PHP and mySQL. Thanks for all the help, I've moved on alot :D

And atleast our number of posts has gone up quite alot :lol: One last question, how did you get so good at PHP coding :tiphat: because you are quite... amazing.

#45 Matthew.

Matthew.

    Official Spammer .Matt

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

Posted 17 July 2006 - 06:59 AM

rofl. I'm in no way amazing, lots of people are better than me :tiphat: (hi rc69 lol!) and i've been doing it since i was 11 :) (15 now).

Just keep playing with things, then you'll get the hang of it :D

#46 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 17 July 2006 - 07:09 AM

Well your pretty good at it then to say the least. Now last post and the topic can be closed I guess.

I'll just post the code for reference, if anyone else needs to learn/know what I did, I'm sure Matt doesn't want a repeat :D

Here's the final code that Matt edited for my needs (wants). The codes is for a pagation system (page numbering system) that takes records out of a mySQl database.

<?php
$localhost = "localhost";


$username = "user";  // EDIT THIS
$password = "passwordhere";  // EDIT THIS
$tablename = "database_name"; // EDIT THIS



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

	$limit		  = 20;				
	$query_count	= "SELECT * FROM `tablenamehere`";	
	$result_count   = mysql_query($query_count);	
	$totalrows	  = mysql_num_rows($result_count);  
	$page = $_GET['page'];

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

	$limitvalue = $page * $limit - ($limit);  
	$query  = "SELECT * FROM `tablenamehere` 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-1;
		
		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+1;
		  
		echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");  
	}else{
		echo("NEXT");  
	}
	
	mysql_free_result($result);

?>

Obviously go through and make the needed changes for it to work for you :D

Problem sloved, topic closed.

Regards.!
:D
Rich

p.s cheers again Matt :tiphat: :)

Edited by Rich69, 17 July 2006 - 07:10 AM.


#47 Matthew.

Matthew.

    Official Spammer .Matt

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

Posted 17 July 2006 - 07:25 AM

rofl. :tiphat: yes, hail me....worship me :D :)

p.s cheers again Matt


NP, you gave me something to do on a sunday :D
A friendly neighbourhood mod can close this if they want.

Edited by .Matt, 17 July 2006 - 07:26 AM.


#48 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 July 2006 - 03:21 PM

rofl. I'm in no way amazing, lots of people are better than me B) (hi rc69 lol!) and i've been doing it since i was 11 :D (15 now).

Just keep playing with things, then you'll get the hang of it :o

Which one of us is better is mearly a matter of opinion in my opinion. You know some tricks i don't, i know some you don't.

Anyway, in answer to a previous question, you can insert "<a href='mylink.com'>my web site</a>" in to the database just fine. It's a text database, not an html database, so you'd see it just as you inserted, then when you selected and echoed it you would see the link. Of course, if you're listing url's, it would be smarter to have the name/url fields in the database, and then do what matt suggested.

p.s. Matt, i started when i was 16, and i'm now 18 :P

#49 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 18 July 2006 - 03:27 PM

Yeah, I've tryed both ways and I think i prefer Matt's way. I basically started when im 16 :o Well near enough, I'm 16 tomorrow B) but I've been looking at PHP for a couple of months already I know some basic and some advanced, I really need to look into 'learning' it in order :D

Edited by Rich69, 18 July 2006 - 03:32 PM.


#50 Matthew.

Matthew.

    Official Spammer .Matt

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

Posted 18 July 2006 - 03:37 PM

p.s. Matt, i started when i was 16, and i'm now 18 :P



Well foo you lol.

Rich69, you dont need to learn it in order, hell i didnt.....dunno about anyone else... :P

Until a few days ago i didnt know you could update more than one field in an UPDATE query lool :love: :ph34r:

#51 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 18 July 2006 - 03:40 PM

Thats good then because to be honest I never learn anything in a logically order :ph34r: I kind of pick at things I think will be intresting first, and please call me Rich :love: :P

#52 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 July 2006 - 03:41 PM

Can't say as i've done that before, but after having thought i learnt about everything there is to know, i found out about static variables and the fact that you can use commas to echo multiple things, not just periods.

And how do you define the order in which you learn a language?

Edited by rc69, 18 July 2006 - 03:49 PM.


#53 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 18 July 2006 - 03:47 PM

And how do you define the order in which you learn a language?


Well, normally start with the easy things and build your self up. Or thats why people suggest, I didn't mean there was a set order lol. Like http://w3schools.com have a 'logically' order of how they arrange things to learn in a topic.

Edited by Rich69, 18 July 2006 - 03:49 PM.


#54 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 July 2006 - 03:51 PM

I was just being facetious :ph34r:

Edited by rc69, 18 July 2006 - 03:51 PM.


#55 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 18 July 2006 - 03:58 PM

I was just being facetious :ph34r:


Ohh, shhh stop the bullying :love: haha I'm really tired, sorry I found it hard to pick up.

Edited by Rich69, 15 November 2006 - 10:53 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users