PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old June 25th, 2002, 09:14 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 25
prev next link headache

Hi,
I am trying to do a prev next link using php. But i seem to be having a problem and i dont know where went wrong. It would be nice if you guys could comment about it.

Please bear with the code below cause i am very new at this.

TIA
Braendan

<?php

//
// Connects to sql server and logs in with username and password and select the required database
//

$connect=@mysql_connect("localhost","username","password") or die ("couldnt connect to sql server");

$db= @mysql_select_db("test") or die ("couldnt select database");

//
// This is where you choose the ammount you want to display in one page
//

$per_page = 5;

//
// Selects all of the data from database
//

$sql_text = ("SELECT * from pdf ORDER BY id DESC");

//
// Sets page number, if no page is specified, it will create page 1
//

if(!$page) {
$page = 1;
}
$prev_page = $page - 10;
$next_page = $page + 10;


$query = @mysql_query($sql_text);

// Sets up specified page

$page_start = ($per_page * $page) - $per_page;

$num_rows = @mysql_num_rows($query);

if($num_rows <= $per_page) {
$num_pages = 1;
}
else if (($num_rows % $per_page) == 0) {
$num_pages = ($num_rows / $per_page);
}
else {
$num_pages = ($num_rows / $per_page) + 1;
}
$num_pages = (int) $num_pages;

if (($page > $num_pages) || ($page < 0)) {
error("You have specified an invalid page number");
}
$sql_text = $sql_text . " LIMIT $page_start, $per_page";
$query = mysql_query($sql_text);

?>


<title>Output here</title>
<font face="Tahoma" size="2"> There are currently<font color="red">
<?php echo "$num_rows"; ?> </font> &nbsp;files in this section<p></font>


<?php

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

echo "<p><font face="Tahoma" size="2">Document ID:".$result['id']."</font><br>n";
echo "<font face="Tahoma" size="2">Title:".$result['title']."</font><br>n";
echo "<font face="Tahoma" size="2">File:</font><a href="$file" target="_blank"><font face="Tahoma" size="2">".$result['file']."</font></a></p>n";

}

// This displays the "Previous" link

if ($prev_page) {
echo "<a href="$PHP_SELF?page=$prev_page"><&nbsp;Prev</a>";
}

// This loops the Pages and displays individual links =

for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo " <a href=$PHP_SELF?page=$i>$i</a>";
} else {
echo " $i ";
}
}

// This displays the "Next" link.

if ($page != $num_pages) {
echo "&nbsp;|<a href="$PHP_SELF?page=$next_page">&nbsp;Next&nbsp;></a>" ;
}

?>

</body>
</html>

Reply With Quote
  #2  
Old June 25th, 2002, 09:19 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 25
RE: prev next link headache

$prev_page = $page - 1;
$next_page = $page + 1;

Hi sorry bout that please ommit the prev post on these to line with the new one above.

TIA,
Braendan

Reply With Quote
  #3  
Old June 25th, 2002, 10:56 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to EvilivE
RE: prev next link headache

Looks fine, what are the error msgs that your getting?

Reply With Quote
  #4  
Old June 26th, 2002, 01:40 AM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to russ Send a message via Yahoo to russ
RE: prev next link headache

Hey, I'm getting prev and next links trouble too, basicly its not showing the next 10 in the row, Its probably something so simple, but I just can't see the problem

php Code:
Original - php Code
  1.  
  2. if($_GET['action'] == 'sortby')
  3. {
  4.  
  5. $result = mysql_query("select * FROM guitardb WHERE band LIKE '".strtoupper($_GET['char'])."%' ORDER BY band ASC LIMIT 10",$db);
  6.  
  7. while($myrow = mysql_fetch_array($result))
  8.  
  9. {
  10. echo "<br>" . $myrow['band'] . " - <a href=?action=view&id=" .$myrow[id]. " class=link>" .$myrow['song']. "</a>";
  11. }
  12.  
  13. if(!isset($start)) $start = 0;
  14.  
  15. $query = ("SELECT * FROM guitardb LIMIT " . $start . ", 10");
  16.  
  17. $result = mysql_query($query);
  18.  
  19.  
  20. $query = "SELECT count(*) as count FROM guitardb";
  21. $result = mysql_query($query);
  22. $row = mysql_fetch_array($result);
  23. $numrows = $row['count'];
  24.  
  25. echo("<p>");
  26.  
  27. if($start > 0)
  28.    echo("<a href=?action=sortby&char=" .strtoupper($_GET['char']). "&start=" . ($start - 10) . " class=link>Previous</a> ");
  29.  
  30. if($numrows > ($start + 10))
  31.    echo("<a href=?action=sortby&char=" .strtoupper($_GET['char']). "&start=" . ($start + 10) . " class=link>Next</a>");
  32.  
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. elseif($_GET['action'] == 'view')
  41. {
  42.  
  43. $result = mysql_query("SELECT * FROM guitardb WHERE id=$id",$db);
  44.  
  45. $myrow = mysql_fetch_array($result);
  46.  
  47. {
  48. echo( "<pre>"  );
  49.  
  50. echo( $myrow['tab'] );
  51.  
  52. echo( "</pre>" );
  53. }
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. else
  66.  
  67. {
  68.  
  69. $result = mysql_query("SELECT * FROM guitardb ORDER BY id desc LIMIT 50",$db);
  70.  
  71. while($myrow = mysql_fetch_array($result))
  72.  
  73. {
  74.  
  75. echo "<br>" . $myrow['band'] . " - <a href=?action=view&id=" .$myrow[id]. " class=link>" .$myrow['song']. "</a>";
  76.  
  77. }
  78.  
  79. }

Reply With Quote
  #5  
Old June 26th, 2002, 03:37 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via Yahoo to EvilivE
RE: prev next link headache

Russ -> it looks like the problem is in your if($_GET['action'] == 'sortby') statments. The first thing I noticed is that you never do anything with:

php Code:
Original - php Code
  1.  
  2. $query = ("SELECT * FROM guitardb LIMIT " . $start . ", 10");
  3. $result = mysql_query($query);


Ok, I see what your trying to do.

replace:
php Code:
Original - php Code
  1.  
  2. $query = "SELECT count(*) as count FROM guitardb";
  3. $result = mysql_query($query);
  4. $row = mysql_fetch_array($result);
  5. $numrows = $row['count'];

with unique variable names.

Also, change:
php Code:
Original - php Code
  1.  
  2. $query = ("SELECT * FROM guitardb LIMIT " . $start . ", 10");


to"
php Code:
Original - php Code
  1.  
  2. $query = ("SELECT * FROM guitardb LIMIT " . $start . ", ".($start + 10).");


Hope that helps.

Reply With Quote
  #6  
Old June 26th, 2002, 08:20 PM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to russ Send a message via Yahoo to russ
RE: RE: prev next link headache


Quote:
php Code:
Original - php Code
  1.  
  2. $query = "SELECT count(*) as count FROM guitardb";
  3. $result = mysql_query($query);
  4. $row = mysql_fetch_array($result);
  5. $numrows = $row['count'];

with unique variable names.



I'm sorry, but I have no idea what your talking about

Reply With Quote
  #7  
Old June 27th, 2002, 09:46 PM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 3
RE: prev next link headache

Code with comments (I reformatted a bit, sorry):
php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. if($_GET['action']=='sortby'){
  5.   $result=mysql_query("select * FROM guitardb WHERE band LIKE '".strtoupper($_GET['char'])."%' ORDER BY band ASC LIMIT 10",$db);
  6.   while($myrow=mysql_fetch_array($result)){
  7.     echo "<br>".$myrow['band']." - <a href=?action=view&id=".$myrow[id]." class=link>".$myrow['song']."</a>";
  8.   }
  9.   //try:
  10.   //if(isset($_GET['start'])){$start=$_GET['start'];}
  11.   //else{$start=0;}
  12.   if(!isset($start)){$start=0;}
  13.  
  14.   $query=("SELECT * FROM guitardb LIMIT ".$start.", 10");
  15.   $result=mysql_query($query);
  16.   //the above query is never actually used for anything
  17.   //because you immediately use the same $result identifier
  18.   //in the query below.
  19.  
  20.   $query="SELECT count(*) as count FROM guitardb";
  21.   //change this $result variable to $result2 in order to
  22.   //keep the results of the above query
  23.   $result=mysql_query($query);
  24.   $row=mysql_fetch_array($result);
  25.   $numrows=$row['count'];
  26.  
  27.   //you start a paragraph, but never close it
  28.   //this is more of an HTML gripe than anything.
  29.   echo "<p>";
  30.   if($start>0){
  31.     echo "<a href=?action=sortby&char=".strtoupper($_GET['char'])."&start=".($start - 10)." class=link>Previous</a> ";
  32.   }
  33.  
  34.   //perhaps try:
  35.   //if(($numrows-$start-10)>10){
  36.   //  echo "<a href=?action=sortby&char=".strtoupper($_GET['char'])."&start=".($start + 10)." class=link>Next</a>";
  37.   //}else{
  38.   //  echo "<a href=?action=sortby&char=".strtoupper($_GET['char'])."&start=".($start)." class=link>Next</a>";
  39.   //}
  40.   if($numrows>($start+10)){
  41.     echo "<a href=?action=sortby&char=".strtoupper($_GET['char'])."&start=".($start + 10)." class=link>Next</a>";
  42.   }
  43. }elseif($_GET['action']=='view'){
  44.   $result=mysql_query("SELECT * FROM guitardb WHERE id=$id",$db);
  45.   $myrow=mysql_fetch_array($result);
  46.  
  47.   //why is this blocked?
  48.   //I am assuming a while loop should be here...
  49.   //and the above fetch_array should be part of it?
  50.   {
  51.     echo "<pre>";
  52.     echo $myrow['tab'];
  53.     echo "</pre>";
  54.   }
  55.   //above output compressed...no biggie...
  56.   //echo "<pre>".$myrow['tab']."</pre>";
  57.  
  58. }else{
  59.   $result=mysql_query("SELECT * FROM guitardb ORDER BY id desc LIMIT 50",$db);
  60.   while($myrow = mysql_fetch_array($result)){
  61.     echo "<br>".$myrow['band']." - <a href=?action=view&id=".$myrow[id]." class=link>".$myrow['song']."</a>";
  62.   }
  63. }
  64. ?>



Cheers,
Keith.

Reply With Quote
  #8  
Old June 28th, 2002, 03:04 AM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to russ Send a message via Yahoo to russ
RE: prev next link headache

Thanks man for your help, actually with your extra comments on the side it helped me understand php alot more, I found the problem in the end, basicly i changed this

php Code:
Original - php Code
  1.  
  2. if($_GET['action']=='sortby'){
  3.  
  4.   $result=mysql_query("select * FROM guitardb WHERE band LIKE '".strtoupper($_GET['char'])."%' ORDER BY band ASC LIMIT 10",$db)


to this

php Code:
Original - php Code
  1.  
  2. if($_GET['action']=='sortby'){
  3.  
  4.   $result=mysql_query("select * FROM guitardb WHERE band LIKE '".strtoupper($_GET['char'])."%' ORDER BY band ASC LIMIT " .$_GET['start'] . ", 10",$db)


Altho I do get Prev. and Next Links on everypage.
but as I got that kinda worked out, can i get rid of this section of code?

php Code:
Original - php Code
  1.  
  2. $query = ("SELECT * FROM guitardb LIMIT " . $start . ", 10");
  3.  
  4.  
  5.  
  6. $result = mysql_query($query);
  7.  
  8.  
  9.  
  10.  
  11.  
  12. $query = "SELECT count(*) as count FROM guitardb";
  13.  
  14. $result = mysql_query($query);
  15.  
  16. $row = mysql_fetch_array($result);
  17.  
  18. $numrows = $row['count'];

Reply With Quote
  #9  
Old June 28th, 2002, 04:10 AM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 3
RE: prev next link headache

More code with a few tweaks...
php Code:
Original - php Code
  1.  
  2. <?php
  3. if($_GET['action']=='sortby'){
  4.   if(isset($_GET['start'])){$start=$_GET['start'];}else{$start=0;}
  5.     $result=mysql_query("select * FROM guitardb WHERE band LIKE '".strtoupper($_GET['char'])."%' ORDER BY band ASC LIMIT " .$start.", 10",$db);
  6.   while($myrow=mysql_fetch_array($result)){echo "<br/>".$myrow['band']." - <a href=?action=view&id=".$myrow[id]." class=link>".$myrow['song']."</a>";}
  7.  
  8.   $query="SELECT count(*) as count FROM guitardb";
  9.   $result=mysql_query($query);
  10.   $row=mysql_fetch_array($result);
  11.   $numrows=$row['count'];
  12.  
  13.   //you start a paragraph, but never close it
  14.   //this is more of an HTML gripe than anything.
  15.   echo "<p>";
  16.   if($start>0){echo "<a href=?action=sortby&char=".strtoupper($_GET['char'])."&start=".($start - 10)." class=link>Previous</a> ";}
  17.  
  18.   //perhaps try:
  19.     $start_val=(($numrows-$start-10)>10)?($start+10):$start;
  20.     echo "<a href=?action=sortby&char=".strtoupper($_GET['char'])."&start=$start_val class=link>Next</a>";
  21.     echo '</p>';
  22. }
  23. ?>

I think the next prev links will work, but what happens when $start=95 out of 100 records and you go $start+10 and run the query for the next batch?

Also, based on the partial code you wrote, I would keep the $_GET['start'] check in, but move it to the top like I did in this example, or you risk the query bombing if start wasn't set.

Mind you, with that logic, you should also check for $_GET['char'] and default it as well if someone removes it from the URL. Or display an error message if it isn't passed in, etc.

Test your forward previous links all the way to the end of your recordset (and make sure the total number of records isn't a multiple of 10. If it works, the all is good

I think I left checking the start and end of the recordset out of my code...but I am too tired to look right now

Cheers,
Keith

Reply With Quote
  #10  
Old June 29th, 2002, 11:20 PM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to russ Send a message via Yahoo to russ
RE: prev next link headache

Thank you man, it works now, but I still get prev and next on everypage, but thats a minor fault. thanks again

Reply With Quote
  #11  
Old August 23rd, 2002, 08:32 PM
russ russ is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 129 russ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to russ Send a message via Yahoo to russ
RE: prev next link headache

okay, I worked out why I have a prev and next on each page, it seems that it counts how many rows there are in the table instead of counting just the rows being display, ie, if I display just the rows starting with A, it shows just the A's, but it counts all the rows, any way around this? also how can I have a displaying 5 of 19 reviews or something like that?

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > prev next link headache


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek