944,116 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1321
  • PHP RSS
Dec 5th, 2007
0

Dynamic Links Problem

Expand Post »
Hi there guys & gals, this is my first post here will try to be as accurate as possible.

The problem i have seems to me to be quite basic but i cannot for the life of me work out how i can do it, am hoping you good folks can point me in the right direction.

I have data in .txt file format, single line per entry, passed into an array, single line to be displayed per page, what i cannot figure out is how to make a 'next page' and 'previous page' link to modify the var's i am using to correctly get the line in the file.

Here is my current php code active on the page it currently grabs var's from the address line
example: file.php?file_select=ttc01-10&chapter=8 where file_select = filename of the .txt file and chapter = the line to read and displayed.

Any advice, hints or tips would be greatly welcomed:

PHP Syntax (Toggle Plain Text)
  1. $file_select = 'ttc_intro';
  2. if ($HTTP_GET_VARS) $file_select = $HTTP_GET_VARS['file_select'];
  3. if ($HTTP_POST_VARS) $file_select = $HTTP_POST_VARS['file_select'];
  4.  
  5. $chapter = 0;
  6. if ($HTTP_GET_VARS) $chapter = $HTTP_GET_VARS['chapter'];
  7. if ($HTTP_POST_VARS) $chapter = $HTTP_POST_VARS['chapter'];
  8.  
  9. $file_selected = $file_select.'.txt';
  10.  
  11. $lines = file($file_selected);
  12. $line = $lines[$chapter];
  13.  
  14. print '<br />';
  15. print '<table width="685" align="center" cellspacing="1"><tr class="title_colour"><td colspan="2"><div align="center">';
  16.  
  17. print $line;
  18.  
  19. print '</div></td></tr><tr class="title_colour"><td width="338"><div align="center">
  20. <strong>&lt;&lt; Previous Chapter </strong></div></td><td width="340">
  21. <div align="center"><strong>Next Chapter &gt;&gt; </strong></div></td></tr></table>';
  22. print '<br />';

And what i would like is to get two links that update based on the var's to the previous line and the next line in the array.

Take care, and thanks for any replies
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
DaitoTsu is offline Offline
19 posts
since Dec 2007
Dec 5th, 2007
0

Re: Dynamic Links Problem

php Syntax (Toggle Plain Text)
  1. if($chapter!=0){ // if chapter has some value and if its not 0.
  2. $prev_link=$chapter - 1; //set previous link as chapter - 1.
  3. } else {
  4. $prev_link=0; //if chapter = 0, then theres no previous link, make it 0 as well.
  5. }
  6. if($chapter < count($lines)){ //if the chapter is less than the number of lines, next_link is valid.
  7. $next_link=$chapter + 1; //assign next chapter to $next_link
  8. }
  9.  
  10.  
  11. echo "<a href='file.php?chapter=$prev_link'>Previous chapter</a><br>";
  12. echo "<a href='file.php?chapter=$next_link'>Next chapter</a><br>
  13.  
Thats it, i guess. Btw, most of the people dont use vars anymore. Its outdated. You can do something like this.
[inline]
$file_select=(isset($_REQUEST['file_select']))?$_REQUEST['file_select']: "ttc_intro";[/inline] Here you are checking if file_select has been set by any of the methods, $_GET or $_POST. if its set, then assign that value to $file_select, else, assign ttc_intro to $file_select. You can do the same thing while checking for chapters as well..

Cheers,
Naveen
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 6th, 2007
0

Re: Dynamic Links Problem

Why thank you very much good sir

I knew it wouldnt be too difficult to do but i am still a relative newbie to php, i think my php book might be a little out of date now. Thanks for the update on the GET and POST.

Going to implement your code soon, and hopefully that will have solved my problem, again thank you very much for your assistance.

-Daito
Reputation Points: 10
Solved Threads: 1
Newbie Poster
DaitoTsu is offline Offline
19 posts
since Dec 2007
Dec 6th, 2007
0

Re: Dynamic Links Problem

you are welcome.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 6th, 2007
0

Re: Dynamic Links Problem

After a bit of head scratching i worked out i needed to adapt the code a tiny wee bit to get it working but this is defiantly solved.

Where:
PHP Syntax (Toggle Plain Text)
  1. echo "<a href='file.php?chapter=$prev_link'>Previous chapter</a><br>";
  2. echo "<a href='file.php?chapter=$next_link'>Next chapter</a><br>";

Now:
PHP Syntax (Toggle Plain Text)
  1. echo "<a href='file.php?file_select=$file_select&chapter=$prev_link'>Previous chapter</a><br>";
  2. echo "<a href='file.php?file_select=$file_select&chapter=$next_link'>Next chapter</a><br>";

Again i thank you for taking the time to help me out
Last edited by DaitoTsu; Dec 6th, 2007 at 8:04 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
DaitoTsu is offline Offline
19 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: query in mysql
Next Thread in PHP Forum Timeline: Image not getting refreshed automatically





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC