URGENT.. I Need Help Solving A Simple Sidebar elseif Problem

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
jalvini
Forum Newbie
Posts: 2
Joined: Sat Nov 20, 2010 2:55 am

URGENT.. I Need Help Solving A Simple Sidebar elseif Problem

Post by jalvini »

Okay This Might Be a Very Easy Question. I have about 10 pages and I would like them to all display a different set of links (links that are relevant to the page).

Unfortunately the pages are created dynamically and they are not static nor can they be.

What I would like to do is something like this:

if (url == htttp://www.examplesite.com/page1/)

{
echo <a href=blahblahblah.html>blahblahblah</a>
}

elseif (url == htttp://www.examplesite.com/page2/)

{
echo <a href=blahblahblah.html>blahblahblah</a>
}

etc.etc.etc.

Now the only problem I am having is the part with the URL. I know URL needs to be declared as a variable of some sort (god I hope I am saying this right) but I dont know what that variable is.

Can someone please break it down to me in laymans terms. Maybe even write me out an example because I looked over some other sites and was completely confused by the code.

I would really appreciate the help. Thank you in advance
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: URGENT.. I Need Help Solving A Simple Sidebar elseif Pro

Post by social_experiment »

The variable to use is $_SERVER['SCRIPT_NAME'], it contains the current script's path. Here is an example of code that shows how to use this to get the page value. It takes the current path of the script, explodes it at the forward slashes (/) then selects the pagename value from the array and passes it to a switch statement.

Code: Select all

<?php
// Contains the current script's path
$script_name = $_SERVER['SCRIPT_NAME'];
	
// explode() splits the script name at
// each '/' and places the parts into 
// an array
$array = explode('/', $script_name);
	
// the name of the page is contained in
// the last item of the array. Because
// arrays are indexed from 0, substract
// 1 from the total value to get the 
// value of the last index
$amountOfItems = count($array) - 1;
	
// this name of the page is the only 
// part needed for the switch statement
$value = $array[$amountOfItems];
	
// use a switch statement to determine
// what will display
switch($value) {
 case 'test.php';
 echo 'Test Page';
 break;
 case 'other.php';
 echo 'something else';
 break;
 default;
 echo 'something entirely different';
 break;
}
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jalvini
Forum Newbie
Posts: 2
Joined: Sat Nov 20, 2010 2:55 am

Re: URGENT.. I Need Help Solving A Simple Sidebar elseif Pro

Post by jalvini »

First and foremost, thank you for the help thus far social_experiment but I still dont get it because I am a rookie when it comes to writing complex scripts.

Okay I am still very confused Let me go into a little further detail and also show you the piece of the script that I am trying to implement it in.

1) page 1 URL: http://www.mysite.com/coupons/Internet-Web-Services/ page 2 URL: http://www.mysite.com/coupons/Internet-Web-Services/ page 3 URL: http://www.mysite.com/coupons/Computers-Software/ now all of these pages are rewritten using the.htaccess file from a file called tag_result.php

What I tried to do is the following but it did not work. Please explain a little further. I am a total newb when it comes to writing these complex scripts:

<div id="minor">
<!-- google_ad_section_start(name=tags) -->
<div class="panel">
<h3><span>Related Coupon Categories</span></h3>
<div class="inner">
<ul class="tags">

<?php
// Contains the current script's path
$script_name = $_SERVER['SCRIPT_NAME'];

// explode() splits the script name at
// each '/' and places the parts into
// an array
$array = explode('/', $script_name);

// the name of the page is contained in
// the last item of the array. Because
// arrays are indexed from 0, substract
// 1 from the total value to get the
// value of the last index
$amountOfItems = count($array) - 1;

// this name of the page is the only
// part needed for the switch statement
$value = $array[$amountOfItems];

// use a switch statement to determine
// what will display
switch($value) {
case 'PC-Software';
echo 'Test Page';
break;
case 'Food-Groceries';
echo 'something else';
break;
default;
echo 'something entirely different';
break;
}
?>




</ul>
<!-- google_ad_section_end(name=tags) -->
<div class="break"></div>
</div>
</div>


<div class="panel">
<div class="inner">

<div class="break">
</div>
<div class="tagCloud">


<?php
$g_ads = $data->select ( "MarketingAdManager" ,"*", array ( "MarketingPlacing" => "Tag Side Bar" ) ) ;
if ( ! empty ( $g_ads ) )
foreach ( $g_ads as $g_ad )
echo $g_ad["MarketingScript"]."<br><br>" ;
?>


</div>

</div>
</div>


<div class="panel">
<h3><span>Top <?php echo $tag["TagName"] ?> Stores</span></h3>
<div class="inner">
<ol class="topList">
<?php
$websites = $data->select ( "Website_Tag" , "*", array ( "TagID" => intval ( $tag["TagID"] ) ) ) ;
if ( ! empty ( $websites ) )
foreach ( $websites as $website ) :
$web_detail = $data->select ( "Website" , "*" , array ( "WebsiteID" => $website["WebsiteID"] ) ) ;
$web_detail = $web_detail[0] ;
?>
<li>
<a href="<?php echo base_url.get_sef_url ( $website["WebsiteID"] , "Website" ) ?>/" title="<?php echo $web_detail["WebsiteTitle"] ?> Coupon Codes"><?php echo $web_detail["WebsiteName"] ?></a>
</li>
<?php
endforeach ;
?>
</ol>
</div>
</div>
<?php
include ( "inc.social.php" ) ;
?>
</div>
<!-- end: #minor -->
<div class="break">
</div>
</div>
<!-- end: #content -->
</div>





social_experiment wrote:The variable to use is $_SERVER['SCRIPT_NAME'], it contains the current script's path. Here is an example of code that shows how to use this to get the page value. It takes the current path of the script, explodes it at the forward slashes (/) then selects the pagename value from the array and passes it to a switch statement.

Code: Select all

<?php
// Contains the current script's path
$script_name = $_SERVER['SCRIPT_NAME'];
	
// explode() splits the script name at
// each '/' and places the parts into 
// an array
$array = explode('/', $script_name);
	
// the name of the page is contained in
// the last item of the array. Because
// arrays are indexed from 0, substract
// 1 from the total value to get the 
// value of the last index
$amountOfItems = count($array) - 1;
	
// this name of the page is the only 
// part needed for the switch statement
$value = $array[$amountOfItems];
	
// use a switch statement to determine
// what will display
switch($value) {
 case 'test.php';
 echo 'Test Page';
 break;
 case 'other.php';
 echo 'something else';
 break;
 default;
 echo 'something entirely different';
 break;
}
?>
Post Reply