Page 1 of 1

Pass var on include_once

Posted: Tue Sep 30, 2008 6:13 am
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

Re: Pass var on include_once

Posted: Tue Sep 30, 2008 7:25 am
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.

Re: Pass var on include_once

Posted: Tue Sep 30, 2008 7:27 am
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 :)

Re: Pass var on include_once

Posted: Tue Sep 30, 2008 7:28 am
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 :)

Re: Pass var on include_once

Posted: Tue Sep 30, 2008 12:50 pm
by panic!
and also for blackhat SEO links in your signature.