Jump to content

Getting variable from current URL


OhTheNoes

Recommended Posts

Hello! Once again, I find myself in the need of help regarding PHP.

 

What I have is a URL that looks like this:

http://domain.com/gallery/?album=3&gallery=65

 

What I need is to extract the number that comes after "album=". In this case, it would be the number 3.

 

I currently have this:

<?php $url=getPageURL(); $var=explode('album=',$url); $var=explode('?',$var[1]); ?>

 

My problem is that it doesn't result in anything.

 

Can anyone lend a hand? :) It would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/218717-getting-variable-from-current-url/
Share on other sites

If this is something you'll be using several times, you may want to consider creating a function in combination with parse_url to return parameters in the query string. There's an example in a comment on the PHP manual.. Or if this is just a one-off case you could use preg_match:

 

$url = 'http://domain.com/gallery/?album=3&gallery=65';

if (preg_match('/album\=([^&$]+)/', $url, $matches))
{
    echo $matches[1];
}

Hello! Thank you for taking the time to help me out.

 

The code will be placed in a template file which will be called(?) each time someone opens a gallery page.

 

To give a wider view, I'm trying to insert a "back to album" link in each gallery page.

 

My original, incorrect code:

<?php $url=getPageURL(); $var=explode('album=',$url); $var=explode('?',$var[1]); ?>
<?php $albumVar=$var[1] ?>
Back to <a href="http://domain.com/gallery/?album=<?php echo $albumVar ?>">gallery</a>

 

Do you suggest I go with the function you mentioned or would the code you supplied be alright for this purpose?

You could also use parse_str to make sense of the query string.

 

$url = 'http://domain.com/gallery/?album=3&gallery=65';

// album=3&gallery=65
$query_str = parse_url($url, PHP_URL_QUERY);

// array('album' => '3', 'gallery' => '65');
parse_str($query_str, $query_arr);

// outputs: 3
echo $query_arr['album'];

 

That could be mushed on to one line, for you to echo as you want:

<?php parse_str(parse_url(getPageURL(), PHP_URL_QUERY), $query_arr); ?>
Back to <a href="http://domain.com/gallery/?album=<?php echo urlencode($query_arr['album']) ?>">gallery</a>

The subjects says "Getting variable from current URL". Maybe the OP should clarify.

 

Sorry! I am not very familiar with PHP terminology so I may have misrepresented some things with my words of choice.

 

What can I do to help clarify the situation?

would it be easier to wirte the code like this

<?php
$album=$_GET['album'];
$gallery=$_GET['gallery'];
echo"$album<br>$gallery";

 

if it not please forgive me for opening my big mouth

 

Think that was the point of Thorpe's post to be honest.

Are you wanting to return the "album" parameter from the URL the user is visiting, or from a URL contained within a variable?

 

From the URL the user is visiting. I'm sorry if I made things look confusing :(

 

What I meant from my title was how to get the album number which varies depending on the URL of the user.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.