LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-22-2003, 03:30 PM   #1
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
PHP - dynamic content


I want to somehow use PHP to load the same page over and over but with different content. For example, if a user clicks "next book", the same page will load, but they will be able to see the info for the next book stored in my file.
i was trying to use a hidden field - but it didn't seem to like it.

Any ideas??
 
Old 11-22-2003, 08:39 PM   #2
hadding
Member
 
Registered: Oct 2003
Distribution: Redhat 9
Posts: 40

Rep: Reputation: 15
If you set the action for $_SERVER['PHP_SELF'] you can do that.

Last edited by hadding; 11-22-2003 at 08:41 PM.
 
Old 11-22-2003, 09:36 PM   #3
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
I believe putting __FILE__ in the action of a form will achieve the same thing (__FILE__ holds the name of the current file, complete path)
Or you can try using header("location: the_url_") to redirect to the same file.
 
Old 11-22-2003, 09:48 PM   #4
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by coolman0stress
I believe putting __FILE__ in the action of a form will achieve the same thing (__FILE__ holds the name of the current file, complete path)
Or you can try using header("location: the_url_") to redirect to the same file.
1) If I put the file in the action of a form, wouldn't I need a "Submit" button to cause the action to take place? I just want them to be able to click a link to have the same effect.

2) Is "header()" some php function I don't know about? Where would I invoke it??

Sorry if these are dumb questions!!
 
Old 11-24-2003, 04:04 AM   #5
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
1) How about using the page's url as the href part of your link?

2) header basically encapsulates all the stuff that gets sent to a browser from the server before the actual content. It includes headers, mime types (what type of document it is), cookies, redirects, etc... More info

Though in your case i think 1 would be the easiest choice.

Last edited by coolman0stress; 11-24-2003 at 04:05 AM.
 
Old 11-24-2003, 07:47 AM   #6
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by coolman0stress
1) How about using the page's url as the href part of your link?

2) header basically encapsulates all the stuff that gets sent to a browser from the server before the actual content. It includes headers, mime types (what type of document it is), cookies, redirects, etc... More info

Though in your case i think 1 would be the easiest choice.
Coolman,

1) is what I was trying to do - I agree with you, it seems like the easiest solution.

My question is this: will I be able to pass parameters to the new page just by using an href??

For example, I have a file where each line is a new book and contains information about that book to be displayed.
If a user is viewing a book, and wants to see the next one in the list, he simply clicks "next", and then I want to display the info for the next book (on the same page, overwriting the previous data).

So I'm thinking I will need to keep some sort of counter or something (I can worry about thos details), but will I be able to access that $counter variable from another document once it is loaded??

Hope my question is clear!!

Thanks so much.
 
Old 11-24-2003, 01:35 PM   #7
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Basically what you need to do is pass an extra variable through the url about the state of your application, so what page should be displayed next.

Say your php file is called "book.php". The first time you load it, you want to for example display the first book. That's the default behaviour. Nothing was passed to the file.

Now on that page you create a link, "Next", where you take the current book # and increment it by one and pass this number through the url to the same script. Something like this:
href="book.php?book_number=1"...

Now what you have to do is when your script loads, check for the variable "book_number", if it was not set, then this is the first time the user used the book, so you display the default page. But if it was set to something, use that number to determine which book to display and increment the number again for the link.

Something like that
 
Old 11-24-2003, 02:03 PM   #8
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
You are my saviour. That makes a lot of sense. Thanks for your reply. I'll get back to you once I have it implemented.

Thanks again
 
Old 11-24-2003, 02:35 PM   #9
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Sweet, can't wait to see what you come up with
 
Old 11-24-2003, 03:04 PM   #10
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
How do I check if a variable exists if it hasn't been initialized yet?

Also,
if I have <a href="next.php?booknum=1">

then won't "1" always get passed no matter what? How would I increment it and then pass the incremented value?? It would also have to decrement if a user clicked the "previous" link.

Last edited by jacksmash; 11-24-2003 at 03:10 PM.
 
Old 11-24-2003, 03:42 PM   #11
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
You're right, you need to find a way to retrieve the value that was passed to the script. There are a couple of ways of doing it.

The most versatile method is to use $_REQUEST['varname'] which will return the value of the variable. I've just started learning php myself, but i found $_REQUEST to be the best, since it doesn't matter in what form the variable was passed (wether it was through get, post or cookie).

Here's an example:
PHP Code:
<?php
 $num
$_REQUEST['num']; // get number that might have been passed
 
echo "Num: "$num"<br/>"// display current number
 
echo "<a href="$_SERVER['php_self'], "?num=", ++$num">Next</a>"// create the next link by incrementing the current num
?>
 
Old 11-25-2003, 03:43 PM   #12
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
This has proven to be quite difficult. It turns out that passing ++$num works just fine, however, if I want to pass another parameter, such as $title, if I concatenate it like you have shown me, I lose data.

For example, if I do this:

echo "<a href=", $_SERVER['php_self'], "?num=", ++$num, "&title=", $title>Next</a>";

And lets say that $title references "The Hulk", then only "The" gets passed for some stupid reason I'm sure.

Any ideas why??
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP dynamic extension on Suse Vaish Linux - Software 1 06-08-2005 07:20 PM
Php-Nuke, Mambo or...? (web content management software) fturcic Linux - Software 0 01-30-2005 04:43 PM
php question - how can i replace the content? antony_csf Programming 2 06-29-2004 09:01 PM
php content management sridharinfinity Linux - General 1 11-04-2003 06:00 PM
Php Dynamic Refresh Bheki Linux - General 0 05-16-2002 05:56 AM


All times are GMT -5. The time now is 12:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration