Pagation with mySQL results
#21
Posted 16 July 2006 - 02:10 PM
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
Posted 16 July 2006 - 02:15 PM
<?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
Posted 16 July 2006 - 02:18 PM
Because the script looks fine
#24
Posted 16 July 2006 - 02:29 PM
<?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
Posted 16 July 2006 - 02:40 PM
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
Posted 16 July 2006 - 02:47 PM
Edited by Rich69, 16 July 2006 - 02:49 PM.
#27
Posted 16 July 2006 - 02:53 PM
<?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.
#28
Posted 16 July 2006 - 02:59 PM
"Parse error: syntax error, unexpected '@' in ***************************************** on line 12"
I stared out the path to the file 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
Posted 16 July 2006 - 03:03 PM
Replace:
$tablename = "indust_tutorial" // EDIT THIS
With:
$tablename = "indust_tutorial"; // EDIT THIS
Im very tired
#30
Posted 16 July 2006 - 03:17 PM
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
#31
Posted 16 July 2006 - 03:27 PM
e.g
`tablename`
#32
Posted 16 July 2006 - 03:50 PM
<?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
Shit I kept my password in by misstake
Edited by Rich69, 16 July 2006 - 03:55 PM.
#33
Posted 16 July 2006 - 04:05 PM
$query_count = "SELECT count(*) FROM `tutorials`";
With..
$query_count = "SELECT * FROM `tutorials`";
#34
Posted 16 July 2006 - 04:11 PM
"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
Posted 16 July 2006 - 04:16 PM
Sorry, my fault.
$query_count = "SELECT * FROM `tutorial`";
Replace it with that.
#36
Posted 16 July 2006 - 04:23 PM
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
Posted 16 July 2006 - 04:25 PM
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
Posted 16 July 2006 - 04:32 PM
click next, it doesn't do anything
#39
Posted 16 July 2006 - 04:34 PM
#40
Posted 16 July 2006 - 04:38 PM
<?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