Show content according to explode

Hi all,

I would like to use explode to determine whether content should be displayed on a page or not. The idea is to show a certain image on the http://www.site.com/dir, but not on http://www.site.com/dir/subdir.

The problem is that there is only one template file to determine the look of these pages, called main.php, which loads subdir content accordingly. So I figured I could place following code on top of main.php to see if a subdirectory exists in the url and only show the image for the /dir/ page and not the /dir/subdir/ pages:


<?php
    $subdir = explode('/', $_SERVER['REQUEST_URI']);

    if (isset($subdir[2])) {
    echo 'no image';
    } 
    else {
    echo 'image'; } ?>

However, it seems the image is never loaded in this case. What am I doing wrong here?

Thanks in advance,

Stef

I see. Thanks for the info. The result is still the same though…

This is the code as it is now:


<?php
    $subdir = explode('/', $_SERVER['REQUEST_URI']);
    
    // Remove starting slash
    if (substr($subdir, 0, 1) == '/')
    {
    $subdir = substr($subdir, 1);
    }

    // Remove ending slash
    if (substr($subdir, -1) == '/')
    {
    $subdir = substr($subdir, 0, -1);
    } 

    
    if(count($subdir) > 1){
        echo 'No image';
    }
    else{
           echo 'yes image';
    } 
    ?>


I even tried changing the > 1 count to several other numbers. The result is either “yes image” or “no image” on all pages, no difference between them.

Awesome! Works like a charm!

Thanks to all for your help, much appreciated.

Stef

You need to strip the slashes before exploding, you can’t use those functions on an array :wink:

<?php

    $subdir = $_SERVER['REQUEST_URI'];

    // Remove starting slash
    if (substr($subdir, 0, 1) == '/')
    {
        $subdir = substr($subdir, 1);
    }
 
    // Remove ending slash
    if (substr($subdir, -1) == '/')
    {
        $subdir = substr($subdir, 0, -1);
    }

    $subdir = explode('/', $subdir);
    
    if(count($subdir) > 1){
        echo 'No image';
    }
    else{
           echo 'yes image';
    } 
?>

Actually, an easier way could be just to remove the first and last keys if they are empty:

<?php
    $subdir = explode('/', $_SERVER['REQUEST_URI']);
    
    if ($subdir[0] == '')
    {
        array_shift($subdir);
    }

    if ($subdir[count($subdir) - 1] == '')
    {
        array_pop($subdir);
    }
 
    if(count($subdir) > 1){
        echo 'No image';
    }
    else{
           echo 'yes image';
    } 
?>

Your choice :wink:

Since you’re exploding by the trailing slashes you need to remove them, otherwise they become empty keys. I.e /jobs/ would become:

Array (0 => '', 1 => 'jobs', 2 => '')

You can remove them like so:

// Remove starting slash
if (substr($subdir, 0, 1) == '/')
{
   $subdir = substr($subdir, 1);
}

// Remove ending slash
if (substr($subdir, -1) == '/')
{
   $subdir = substr($subdir, 0, -1);
}

I suspect you have correctly returned URI with $_SERVER[‘REQUEST_URI’].

Try:


echo _SERVER['REQUEST_URI'];

And see what it returns!

It returned a parse-error actually, until I added a $ in the front. :wink:

It returns things like:

  • /jobs/category/keuken
  • /jobs/search?query=Antwerpen
  • /jobs/

Each time followed by “No image”, although the last one (/jobs/) should return “yes image”.

Strip off the beginning and trailing slashes :wink:

Still no difference, although both snippets seem to request exactly what I need… :frowning:

I don’t understand why this happens, does it have anything to do with the fact that these subdirectories and -pages are no actual files, but exist only “virtually”?

Seriously? Would that make a difference? If so, how do I do that?


$subdir = explode('/', $_SERVER['REQUEST_URI']);
if(count($subdir) > 1){
    echo 'No image';
}
else{
    echo 'yes image';
}

:stuck_out_tongue: Enough?

Hi,

That looks great, but it does the same thing as before. It just says “No image” on every page, no matter if there is a subdirectory or not.

You can see what I mean here and [URL=“http://www.culiconsulting.be/jobs/category/keuken”]here, the first link is the main page, where an image (eventually the map you see below) should appear. The second one is a subpage, as you can see underneath the title this one also says “No image”, as intended.

By the way, if I ask to echo the subdir count like this:

echo (count($subdir)); 

The site returns “3” for both the site.com/jobs and site.com/jobs/search?query=Antwerpen pages, and “4” for the site.com/jobs/category/receptie page.

Only the site.com/jobs should display the image.

Hi
This might solve your problem. Please let me know if any problem.


<?php
$subdir = explode('/', $_SERVER['REQUEST_URI']);
$subdir=explode("/",$subdir);
if(in_array("dir",$subdir)){
	echo "Image";
}else{
	echo "No Image";
}
?>