Pass var on include_once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Pass var on include_once

Post by papa »

Hi,

I have a script on my site that displays by default 5 latest songs added. I include this script using include_once and I was thinking if I could get it more dynamic by setting how many songs I want to display in the include url.

I tried:

Code: Select all

<?php include_once("latest_music.php?song_nr=10"); ?>
But resulted in an error.

I want to define $song_nr in my url if possible.

Easy fix is to write:

Code: Select all

$song_nr = 10; include_once(...)
But wanted to see if there was another way.

cheers
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Pass var on include_once

Post by The_Anomaly »

From my understanding of PHP and HTTP protocol, GET data is passed via the HTTP request. include() basically, includes PHP pages on the server. It's not requesting the page--it's taking the code and placing it into its own page, and running it. Due to this lack of request, you're not going to be able to pass any GET data through include()/require().

A possible solution might be the following:

Code: Select all

 
 
$num_songs = 10;
 
include_once('latest_music.php");
 
latest_music.php:

Code: Select all

 
//Check to see if the num_songs was specified here.
echo $num_songs; //Use the variable as appropriate.
 
In other words, define the variable prior to requiring the page that uses it, and after checking if the variable exists, use it.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Pass var on include_once

Post by papa »

Ok thanks man.

That's what I did. Defining the var before requesting, not a big deal really but you always learn something :)
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Pass var on include_once

Post by The_Anomaly »

papa wrote:Ok thanks man.

That's what I did. Defining the var before requesting, not a big deal really but you always learn something :)
My pleasure! And hey, that's what the forums are all about: learning something :)
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Pass var on include_once

Post by panic! »

and also for blackhat SEO links in your signature.
Post Reply