Help on loading an image based on a directory name from URL

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
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Help on loading an image based on a directory name from URL

Post by datter »

Hi all, first post here after shopping around for a good php forum and this appears to be the place.

I'm trying to sort out a way I can load an image based on a url. For instance, if my php is being called from http://www.domain.com/news/ it should load subheader_news.jpg. If it's being loaded from http://www.domain.com/weather/ then it should load subheader_weather.jpg and so on. An issue is that sometimes the url will be longer than that, so in the first example it might be http://www.domain.com/news/articles/daily/today.php yet I'll still need it to load subheader_news.jpg. In other words I need to load an image based on the first directory name after the root URL, whether it's /news/, /weather/ or whatever else.

Any thoughts as to how to pull that piece of the URL out on page load, and then insert it into the img code to load the proper image for that location? Sort of like
  • If it resides in http://www.domain.com/news/articles/
    Get url and strip out everything except /news/
    Insert /news/ (minus slashes) into image code such as img src=http://domain.com/images/subheader_news.jpg
This way it will load subheader_WHATEVER.jpg based entirely on what folder it's being loaded from.

Any help would be appreciated.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Help on loading an image based on a directory name from URL

Post by jaoudestudios »

You always know how long your domain is going to be, so you could use substr to chop up to ....com/ leaving you with news/.... then explode on / and take the first part.
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

That sounds like what I need to do, just not clear on how to go about doing it exactly.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Help on loading an image based on a directory name from URL

Post by jaoudestudios »

When you get it working, we can then simplify it and make it more efficient & robust.

Something like this...

Code: Select all

 
// test page
$url = "http://www.domain.com/news/sub/sub2"; // you only want [b]news[/b] from this?
$url_extra = substr($url, 22);
$url_extra_arr = explode('/', $url_extra);
$section = $url_extra_arr[0];
echo $section;
 
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

ok this is good... very good in fact. Now what I had done before was nab the url, through some fashion or others. I could then apply this to that and get the /news/ bit (or whatever dir it might be) from that url. From there I'd take that nabbed bit and insert it into the img src like subheader_NABBEDBIT_.gif

If you take my meaning. ;)

Now all I have to do is find the code I was using to nab said URL, which I suspect I might have dumped in a fit of frustration or some such. Will post if I find it.

Thanks very much thus far, this is really helpful.
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

aha, this is what I was fetching the URL with...

Code: Select all

 
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>
 
Trying to sort out how to take the above, then get the bit you so helpfully added farther up such that it's doing what I need it to do... from there it will just be a matter of sticking the output in the img src... i think.
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

okokok, I'm getting this.. the following gets the url and strips it of everthing except the /new/ (or whatever it might be).

Code: Select all

// Get URL
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
 
// Strip it of extra junk
$url = curPageURL();
$url_extra = substr($url, 22);
$url_extra_arr = explode('/', $url_extra);
$section = $url_extra_arr[0];
echo $section;
The echo at the end displays as the bit I need. Now, to get that into the img src

Code: Select all

 
<img src="domain.com/images/subheader_" + SOMETHINGHERE + ".gif">
 
At least I think that's all I need to do. :)
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

Close, should the bit that strips stuff away be a separate function, like so?

Code: Select all

 
<?php
 
// Get URL
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
 
// Strip it of extra junk
function subHead() {
$url = curPageURL();
$url_extra = substr($url, 22);
$url_extra_arr = explode('/', $url_extra);
$section = $url_extra_arr[0];
echo $section;
}
 
?>
 
<br>x<br>
<img src="domain.com/images/subheader_"+<?php subHead() ?>+".gif" alt="" border="0">
<br>x<br>
 <?php echo subHead();?>
Ignore all the BR's at the end there... it's just a quick and dirty way for me to see where the image SHOULD appear (between the X's)

Now using the above, the echo subHead() at the very end does indeed return the portion of the URL I want, though the image isn't showing above so I'm clearly screwing something up. Possibly many things, but this is closer than I've managed to get to date. :)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Help on loading an image based on a directory name from URL

Post by jaoudestudios »

Can you post the source - view source from the browser.

You dont need the '+' on either side of the php, this is not javascript :P
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

jaoudestudios wrote:Can you post the source - view source from the browser.

You dont need the '+' on either side of the php, this is not javascript :P
HAH.

Those silly +'s were killing me.

Code: Select all

 
img src="http://domain.com/images/subheader_<?php subHead() ?>.gif" alt="" border="0"
This actually works... though I'm not sure any of this is what you'd call clean... functional though! :)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Help on loading an image based on a directory name from URL

Post by jaoudestudios »

datter wrote:
jaoudestudios wrote:Can you post the source - view source from the browser.

Code: Select all

 
img src="http://domain.com/images/subheader_<?php subHead() ?>.gif" alt="" border="0"
Not browser source code, but glad it works :) (php would not show)
datter
Forum Newbie
Posts: 8
Joined: Tue Jan 13, 2009 6:30 am

Re: Help on loading an image based on a directory name from URL

Post by datter »

Your help has been INVALUABLE so thank you VERY much. :)
Post Reply