Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

url condition in php statement

I have this code

<?
      
      if ( $filename == 'testimonials_detail.php' ) {
      
      ?>

<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>

<? } ?>

<?
      
      if ($filename == 't_detail.php' ) {

?>

<li><a href="index.php?filename=testimonials_detail.php">Back to Testimonials</a></li>
<li class=breaker>|</li>
<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>
<? } ?>


It works fine if my url is index.php?filename=testimonials_detail.php

but if  I add an index.php?filename=testimonials_detail.php&id=20

It breaks - what do I need to add so that it will work for the expanded url.
Thank you.
0
celnelson
Asked:
celnelson
  • 5
  • 4
  • 4
  • +2
2 Solutions
 
vibrazyCommented:
Try this

<?
      
      if ( $_GET['filename'] == 'testimonials_detail.php' ) {
      
      ?>
<?
      
      if ($_GET['filename'] == 't_detail.php' ) {
 
?>

Open in new window

0
 
James LooneySr. Programmer/AnalystCommented:
Try this:

<?
if ( array_key_exists('filename', $_GET) && $_GET['filename'] == 'testimonials_detail.php' )
{
?>
	<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>
<?
}
if ( array_key_exists('filename', $_GET) && $_GET['filename'] == 't_detail.php' ) 
{
?>
	<li><a href="index.php?filename=testimonials_detail.php">Back to Testimonials</a></li>
	<li class=breaker>|</li>
	<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>
<? } ?>

Open in new window

0
 
James LooneySr. Programmer/AnalystCommented:
or
this one cuts down on a little bit of duplicated code

<?
if ( array_key_exists('filename', $_GET) && $_GET['filename'] == 't_detail.php' )
{
?>
	<li><a href="index.php?filename=testimonials_detail.php">Back to Testimonials</a></li>
	<li class=breaker>|</li>
<? 
}
if ( array_key_exists('filename', $_GET) && 
		($_GET['filename'] == 'testimonials_detail.php' || $_GET['filename'] == 't_detail.php') )
{
?>
	<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>
<?
}
?>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
@celnelson: Check phpinfo() to see if "register_globals" is on.  If it is, turn it off - it will cause you endless trouble.  Without seeing how you set the $filename variable from the value in $_GET we can only guess at a "best practices" solution for you, but I would suggest that you:

(1) isolate ONLY the variables you expect to see in $_GET
(2) clean up the keys and values for each of those
(3) take action depending on those values and no others

As a matter of design practice, you should never have a variable (in $_GET or otherwise) that can mean more than one thing. Disambiguation is a virtue.

Can you please tell what this means, exactly:
"if  I add an index.php?filename=testimonials_detail.php&id=20 It breaks" - what is the symptom that makes you say it breaks?

Thanks and regards, ~Ray

0
 
celnelsonAuthor Commented:
With all 3 solutions - the problem with adding &id= to the url still exists.
0
 
James LooneySr. Programmer/AnalystCommented:
By using $_GET, it shouldn't matter if you pass &id=

So can you paste in here what error you are getting or what output you get if you pass &id=  
0
 
celnelsonAuthor Commented:
When I spoke of it breaking-
That wasn't a good description.
Basically - if &id= is added to the url - the nav that I want to display- is no longer being added.

You can see where I am working on this at-
http://www.rustedtruck.com/index.php?filename=testimonials_detail.php
and
http://www.rustedtruck.com/index.php?filename=testimonials_detail.php&id=20

The gray nav is what disappears on adding the condition.
Thanks.

0
 
James LooneySr. Programmer/AnalystCommented:
can you post the relevant code from your index.php so we can see what the controls are?
0
 
vibrazyCommented:
Hi,

I changed the from id=20, to id=1, and the grey bar appears, so there must be something in your code that if id = 20, doesnt show the bar.

Can you give us more code?

0
 
vibrazyCommented:
Are you echoing the results from within
<ul class="sub-nav">?

If any id is entered, the bar disappears

Regards,
Dan

<ul class="sub-nav">
 
<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>
   
        
</ul>

Open in new window

0
 
celnelsonAuthor Commented:
This is the entire index page- it only works for id=1 no other numbers - so  don't think it is a problem with the 20.

<?php
 include("config.php");
 global $dir;
 
 
if($_GET['filename']==""){
      $filename="main.php";
}else{
      $filename=$_GET['filename'];
}
      
?>
<?
      //Included classes
      include($dir.'classes/cls_textmanagement.php');
      include($dir.'classes/cls_faq.php');
      include($dir.'classes/cls_pfaq.php');
      include($dir.'classes/cls_faqCat.php');
      include($dir.'classes/cls_category.php');
      include($dir.'classes/cls_product.php');
      include($dir.'classes/cls_news.php');
      include($dir.'classes/cls_dealer.php');
      include($dir.'classes/cls_members.php');
      include($dir.'classes/cls_regProduct.php');
      include($dir.'classes/lib/cls_form.php');
      include($dir.'classes/cls_mail.php');
      //include($dir.'classes/cls_testimonials.php');      
      $sendMailObj = new MailClass();
      
?>
<?
      $form=new Form();
?>
<?
      if( isset($_POST['hSubmit']) && ( $_POST['hSubmit'] == 1 )){
            $member->memberLogin($_POST['userName'],$_POST['Password']);
      }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $siteConfig['configuration_site_title']?></title>
<meta name="keywords" content="<?php echo $siteConfig['configuration_global_keywords']?>" />
<meta name="description" content="<?php echo $siteConfig['configuration_site_desc']?>" />
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/rollover.js"></script>
<script type="text/javascript" src="scripts/slide.js"></script>
<script language="javascript" src="scripts/chainedmenu.js"></script>
<script language="javascript" type="text/javascript">
function isValidEmail(str) {

      return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function IsEmpty(aTextField) {
      if ((aTextField.value.length==0) ||
      (aTextField.value==null)) {
            return true;
      }
      else { return false; }
}
</script>
<!-- -->
<?
      if( $filename == 'rCats.php' ){
?>

<script language="javascript" type="text/javascript">
var disable_empty_list = true;
var onclickaction = "goto" ;
var newwindow = 0;


addListGroup("chainedmenu", "Categories");

addOption("Categories", "Select an Category", "", 1);
<?
      $catMenuList = $category->getMenuMainCat();
      for($i=0;$i<count($catMenuList);$i++){
            
            $proMenuList = $product->getActiveProductList($catMenuList[$i]['id']);
?>

addList("Categories", "<?=$catMenuList[$i]['cat_name']?>", "", "<?=$catMenuList[$i]['id']?>");

addOption("<?=$catMenuList[$i]['id']?>", "Select an Product", "", 1);
      <?
            for($j=0;$j<count($proMenuList);$j++){
      ?>
addList("<?=$catMenuList[$i]['id']?>", "<?=$proMenuList[$j]['pro_name']?>", "index.php?filename=pRegister.php&id=<?=$proMenuList[$j]['id']?>");
<?
            }
      }
?>
</script>
<?
      }
?>
<!-- /////////////////////////////////////////////////////// -->
<?
      //
      if( $filename == 'pRegister.php' ){
?>
<script language="javascript" type="text/javascript">
//var hide_empty_list=true;
var disable_empty_list = true;
var onclickaction = "goto" ;
var newwindow = 0;


addListGroup("chainedmenu2", "dealerState");

addOption("dealerState", "Select Dealer State", "", 1);
<?
      $stateArray = array("International","AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY");
      
      for($i=0;$i<count($stateArray);$i++){
            
            $dealerStateList = $dealerObj->getActiveStateDealers($stateArray[$i]);
?>
addList("dealerState", "<?=$stateArray[$i]?>", "", "<?=$stateArray[$i]?>");
      <?
            for($j=0;$j<count($dealerStateList);$j++){
      ?>
addList("<?=$stateArray[$i]?>", "<?=$dealerStateList[$j]['dealer_name']?>", "");
<?
            }
      }
?>
</script>
<?
      }
      //
?>
<!-- -->
<style>
      .frmfield{
            border:1px solid #999999;
      }
</style>
</head>

<body style="margin: 0px; padding: 0px; background-image: url(images/left-nav/nav-repeat.png); background-repeat: repeat-y;" <? if( $filename == 'rCats.php' ){ echo 'onLoad="initListGroup(\'chainedmenu\', document.listmenu0.firstlevel, document.listmenu0.secondlevel, \'savestate\')"'; } ?>
<? if( $filename == 'pRegister.php' ){ echo 'onLoad="initListGroup(\'chainedmenu2\', document.pRegFrm.dealerState, document.pRegFrm.dealerSelect, \'savestate\')"'; } ?> >


<!--Top-->
<table cellpadding="0" cellspacing="0" width="100%" style="background-image: url(images/top-curve-back.png);  height: 73px; background-repeat: repeat-x;">
<tr>
<td style="background-image: url(images/left-nav/zeagle-logo.png); width: 495px; height: 97px; background-repeat: no-repeat;" valign=top>
<table style="height: 44px; width: 100%"><tr><td class=head-nav  align="right" >

<a href="index.php?filename=dealer.php">FIND&nbsp;A&nbsp;DEALER</a>&nbsp;&nbsp;&nbsp;

</td></tr></table>

</td><td valign=top>





<table cellpadding="0" cellspacing="0" width="100%" border="0">
<tr><td style="  height: 44px; "  width="100%" >
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr><td class=head-nav >

<a href="index.php?filename=page.php&id=6">ASK&nbsp;A&nbsp;QUESTION?&nbsp;&nbsp;</a>
</td><td class=head-nav  align=right>
<input value="Keyword" style="width: 100px; font-size: 11px; height: 18px; margin: 0px; padding: 0px;">
<input type="submit" value="GO">&nbsp;&nbsp;</td></tr></table>
</td></tr>

<!-- This is for nav - if no nav -leave out -->
<tr><td>
<?
      //( $filename != 'products.php' ) AND
      if( ( $filename != 'main.php' ) AND ( $filename != 'news.php' )  AND ( $filename != 'login.php' ) AND ( $filename != 'forget.php' ) AND ( $filename != 'register.php' ) ){
      if( ( $filename != 'products.php' ) ){
                  if( isset($_GET['id']) ){
                        $catCal = $_GET['id'];
                  }else{
                        $catCal = 0;
                  }
                  $catListArr232 = $category->getSubCategoryList($catCal);
                  if( count($catListArr232) ){
      
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" ><tr>
<td style="background-image: url(images/grey-left.png); height: 34px; background-repeat: no-repeat;" width="100%">
<div id="sub-nav">
<center>

<ul class=sub-nav>
<?
     
      if ( $_GET['filename'] == 'testimonials_detail.php' ) {
     
      ?>
     

<li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li>
       
        <?
}
?>
<?
     
      if ($_GET['filename'] == 't_detail.php' ) {
 
?>
         
   
         <li><a href="index.php?filename=testimonials.php">Leave Testimonial</a></li> <li class=breaker>|</li><li><a href="index.php?filename=testimonials_detail.php">Back to Testimonials</a></li>
       
<?
}
?>

<?
      if( ( $filename == 'product.php' ) OR ( $filename == 'proFaq.php' ) ){
            
            if( isset($_GET['id']) && !empty($_GET['id']) ){
            $proMenuArr = $product->getProductDetails($_GET['id']);
            $proMenuCat = $category->getCategoryDetails($proMenuArr['catID']);
            }
?>
<?
      $proImgArr = $product->getProductImageList($proMenuArr['id']);
      if( count($proImgArr) ){      
?>
<script type="text/javascript">

var viewer = new PhotoViewer();

      <? for($i=0;$i<count($proImgArr);$i++){ ?>
            viewer.add('<?=$proImgArr[$i]['image']?>');
      <? } ?>

</script>
<li><a href="javascript:void(viewer.show(0))">Gallary</a></li>
<li class=breaker>|</li>
<?
      }
      $proFaqArr = $pfaq->getActiveProductList($proMenuArr['id']);
      if( count($proFaqArr) ){
?>
<li><a href="index.php?filename=proFaq.php&id=<?=$proMenuArr['id']?>">FAQs</a></li>  
<li class=breaker>|</li>
<?
      }
      if( $proMenuArr['pro_vedio'] != '' ){
      
            if( file_exists($proMenuArr['pro_vedio']) ){
?>
<li><a href="<?=$proMenuArr['pro_vedio']?>">Video&nbsp;Instruction</a></li>
<li class=breaker>|</li>
<?
            }
      }
      if( $proMenuArr['pro_manual'] != '' ){
      
            if( file_exists($proMenuArr['pro_manual']) ){
?>
<li><a href="<?=$proMenuArr['pro_manual']?>">Owners&nbsp;Manual</a></li>
      <?
                  }
            }
      ?>
      <?
      // Start :Link Pages
            $proLinks = $product->getActiveProductLinks($proMenuArr['id']);
            for($i=0;$i<count($proLinks);$i++){
      ?>
            <li class=breaker>|</li>
            <li><a href="index.php?filename=page.php&id=<?=$proLinks[$i]['page_id']?>">
            <?
                  $pageDtl = $page->getPageInfo($proLinks[$i]['page_id']);
                  echo $pageDtl['page_title'];
            ?>
            </a></li>
      <?
            }
      // End :Link Pages
      ?>

<?
}else if( $filename == 'cat.php' ){
      if( isset($_GET['id']) && !empty($_GET['id']) ){
            $catNavList = $category->getSubCategoryList($_GET['id']);
      }else{
            $catNavList = $category->getAllMainCat();
      }
      //echo count($catNavList);
      for($i=0;$i<(count($catNavList));$i++){
            /////////////
            $catProArr = $product->getActiveProductList($catNavList[$i]['id']);
            $proCount = count($catProArr);
            if( $proCount == 0 ){
                  $Lnk1 = 'index.php?filename=cat.php&id='.$catNavList[$i]['id'];
            }else{
                  $Lnk1 = 'index.php?filename=products.php&id='.$catNavList[$i]['id'];
            }
            /////////////
?>


      <li><a href="<?=$Lnk1?>"><?=$catNavList[$i]['cat_name']?></a></li>
      <?
      if( ($i+1) != count($catNavList) ){
      ?>
      <li class=breaker>|</li>  
      <?
      }
      ?>
<?
      }
}
            }
?>
<?
            if(( $filename == 'page.php' ) AND ( $filename == 'contact.php' ) AND ( $filename == 'dealer-inquiry.php' )){
                  $pageChildList = $page->getParentList($_GET['id']);
                  if( count($pageChildList) ){
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" ><tr>
<td style="background-image: url(images/grey-left.png); height: 34px; background-repeat: no-repeat;" width="100%">
<div id="sub-nav">
<center>
<ul class=sub-nav>
<?
            
            for($i=0;$i<count($pageChildList);$i++){
?>
<li><a href="index.php?filename=page.php&id=<?=$pageChildList[$i]['id']?>"><?=$pageChildList[$i]['page_name']?></a></li>
<?      
                  if( $i+1 < count($pageChildList) ){
                        echo'<li class=breaker>|</li>';
                  }
            }
      }
      }      
      }else if( $filename == 'products.php' ){
      
            if( isset($_GET['id']) ){
                  $proCal = $_GET['id'];
            }else{
                  $proCal = 0;
            }
            $proListArr232 = $product->getActiveProductList($proCal);
            if( count($proListArr232) ){

?>


<table cellpadding="0" cellspacing="0" border="0" width="100%" ><tr>
<td style="background-image: url(images/grey-left.png); height: 34px; background-repeat: no-repeat;" width="100%">
<div id="sub-nav">
<center>
<ul class=sub-nav>
<?
      for($i=0;$i<(count($proListArr232));$i++){
            
?>
      <li><a href="index.php?filename=product.php&id=<?=$proListArr232[$i]['id']?>"><?=$proListArr232[$i]['pro_name']?></a></li>
      <?
      if( ($i+1) != count($proListArr232) ){
      ?>
      <li class=breaker>|</li>  
      <?
      }
      ?>
<?
      }
?>
<?
            }
      }
?>
</ul>
</center>
</div>
</td></tr>
<!--END NAV -->
</table>
<div style="clear: both;"></div>
<?
}
?>
</td></tr></table> </td></tr>
</table>
<!--End-Top-->

<!-- -->
<? include($filename); ?>
<!-- -->

<br clear=all>

<div id="footer">
<center>
<hr style="width: 90%;">
<ul class=footer>
<li><a href="#">Products</a></li>
<li>-</li>
<li><a href="#">Support</a></li>
<li>-</li>
<li><a href="#">Contact</a></li>
<li>-</li>
<li><a href="#">Find a Dealer</a></li>
<li>-</li>
<li><a href="#">Site Map</a></li>
</ul></center>
</div>
</div>
</body>
</html>
0
 
vibrazyCommented:
On this line

     if( count($catListArr232) ){

that is where its printing the link..so I am assuming the error is there.

can you change to

     if( count($catListArr232) >0){
0
 
cr4ck3rj4ckCommented:
The Nav Bar displays only when id is equal to 1, 6, 7 or 9.
0
 
James LooneySr. Programmer/AnalystCommented:
Yep, looks like what you have happening is that the link is only printed out IF the function getSubCategoryList() returns a result.

So based on your function name and the results of passing id=1 or id=20, I would assume that in your database, that id=1 has some sub-categories, and that id=20 has no sub-categories.

So if you want the link to show whether or not the current category has sub-categories you'll want to adjust the if statement

$catListArr232 = $category->getSubCategoryList($catCal);
                  if( count($catListArr232) ){
0
 
cr4ck3rj4ckCommented:
@jimjo: Spot on, just what I was typing.

Looks as if $category->getSubCategoryList($catCal) with values of 1, 6, 7 or 9 returns results. Everything else makes your IF statement false and proceeds without your nav bar.
0
 
celnelsonAuthor Commented:
That did it- thanks for everyone's help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now