enano2054
enano2054

Reputation: 329

Nested (?) if statement PHP

Ok the last time I asked a question like this, more people got on my case for not knowing something this simple rather than actually helping me out. I don't want any of that. I just need help and would greatly appreciate it.

I am trying to style the search results on an ecommerce site. What I HAVE gotten to work so far is this statement.

<?php if(wpsc_product_normal_price() == wpsc_the_product_price()):  ?>              
    <p class="s_price" style="margin:0 0 10px 30px;"><?php  echo wpsc_the_product_price(); ?></p>
    <?php else: ?>
    <p class="s_price s_promo_price"><span class="s_old_price"><?php  echo wpsc_product_normal_price(); ?></span><?php  echo wpsc_the_product_price(); ?></p>
    <?php endif; ?>

So as far as I understand and what I see happening, that snippet returns the price of the product being returned in the search.

BUT, not all of the results that come up are products for sale, sometimes they are blog posts, blah blah. So I tried the following code but I couldn't get it to work. What I want to happen is that if the price is 0, or if there isn't a price, there won't be anything displayed.

<?php $searchprice=wpsc_product_normal_price();
 if $searchprice > "0"  ?> 
<?php if(wpsc_product_normal_price() == wpsc_the_product_price()):  ?>              
<p class="s_price" style="margin:0 0 10px 30px;"><?php  echo wpsc_the_product_price(); ?></p>
<?php else: ?>
<p class="s_price s_promo_price"><span class="s_old_price"><?php  echo wpsc_product_normal_price(); ?></span><?php  echo wpsc_the_product_price(); ?></p>
<?php endif; ?>
<?php endif; ?> 

Again, I know I am not as awesome as you all are so I would appreciate that you didn't point that out, that won't solve my problem.

Thanks!

I tried this but the prices don't show up

<?php $searchprice=wpsc_product_normal_price();
if ($searchprice > "0") :  ?> 
<?php if(wpsc_product_normal_price() == wpsc_the_product_price()):  ?>              
<p class="s_price" style="margin:0 0 10px 30px;"><?php  echo wpsc_the_product_price(); ?></p>
<?php else: ?>
<p class="s_price s_promo_price"><span class="s_old_price"><?php  echo wpsc_product_normal_price(); ?></span><?php  echo wpsc_the_product_price(); ?></p>
<?php endif; ?>
<?php endif; ?>

Upvotes: 0

Views: 166

Answers (4)

user2513484
user2513484

Reputation: 324

First of all, I've no idea what wpsc_product_normal_price() and wpsc_the_product_price() return, so I will just focus on your code, and then I'll focus on what you want to happen. I hope that by using one or a combination of both my explanations you will figure out what to do.

Your code needs a bit of revision, there's is a syntax mistake (don't know if it is a typo) and not so common syntax uses. I'll address that in the code comments. There is nothing wrong in echoing the html outside the php snippets like you do, I'll just write it the other way for clarity, and I will not pay attention to html. The if syntax I use is probably the standard practice among programmers, I suggest you get accostumed to using it; but is not mandatory, of course.

// We get the normal price of the product and store it 
// in $searchprice, 
// we don't need to call that function again
$searchprice=wpsc_product_normal_price();

// If is not 0, I guess it means we have a price? 
// This is a better syntax, you were not using parenthesis 
// and you don't need to quote a number 
if ($searchprice > 0) {

     // we compare the normal price to the product price and
     // if they're equal we output "either one" since they're equal, 
     // and we already have wpsc_product_normal_price() stored 
     //in $searchprice so we output $searchprice       
     if($searchprice == wpsc_the_product_price()) {
         echo $searchprice;
     }

   // this else means that $searchprice, that is,  
   // wpsc_product_normal_price(), is 
   // either 0 or a negative price?
   // then we output $searchprice? remember it will be 0 or less 
 } else {

// echo $searchprice since it already has the value returned 
// by wpsc_product_normal_price(), no need to call that 
// function again
 echo 'The normal price is '. $searchprice  . ', and this is the product price:' .  wpsc_the_product_price();

}

Now, improving this code we would:

// We store both since we're going to use them more than once
 // This way we call these functions only once (it is better and 
 // faster to reuse data in variables than calculating that data 
 // everytime we need it)
 $searchprice= wpsc_product_normal_price();
 $productprice= wpsc_the_product_price();

// We check both conditions at the same time.
// This means $searchprice is a number higher than zero AND it is 
// the same number as in  $productprice (calculated by 
// wpsc_the_product_price)
if ($searchprice > 0 && $searchprice == $productprice) {
    // We output either one
    echo $searchprice;
} else {
    // $searchprice is 0 or less and it is not the same as $productprice.
    // We echo both. They are 2 different prices but the first is 0 or less.
    echo 'The normal price/search is '. $searchprice  . ', and this is the product price:' .  $productprice;
}

That was about the code you've shown, but what you want to happen is

that if the price is 0, or if there isn't a price, there won't be anything displayed.

For that precise request the code would be something like (again I've no idea what your functions do or where $searchprice comes from).

// Check if $searchprice is greater than zero
// or not empty, for instance, or you could 
// also check for null, false depending on 
// what $searchprice is
if ($searchprice > 0 || $searchprice!="") {
    // We output $searchprice
    echo $searchprice;
} else {
    // $searchprice is 0 or less
    // if you don't want anything 
    // to happen when $searchprice is 0 
    // just remove this 
    // entire else statement.
    // Nothing will happen.
}

So, according to what you want it could even be shorter:

if ($searchprice > 0) echo $searchprice;

Upvotes: 0

Barmar
Barmar

Reputation: 781721

This line:

 if $searchprice > "0"  ?> 

should be:

 if ($searchprice > "0") :  ?> 

You're missing the parentheses around the condition, and the : to indicate where the then-block starts.

Here's the whole thing. I replaced your function calls with variables because I don't have the rest of your code, but that should make no difference.

<?php
  $searchprice = 20;
  $productprice = 10;
if ($searchprice > "0"):  ?> 
<?php if($searchprice == $productprice):  ?>              
  <p class="s_price" style="margin:0 0 10px 30px;"><?php  echo "Product: " . $productprice; ?></p>
<?php else: ?>
  <p class="s_price s_promo_price"><span class="s_old_price"><?php  echo "Normal: " . $searchprice; ?></span><?php  echo " Product:" . $productprice; ?></p>
<?php endif; ?>
<?php endif; ?> 

It displays:

Normal: 20 Product:10

Upvotes: 2

Gustavo
Gustavo

Reputation: 1683

I think you have two choices:

if($searchprice>0)

Or the ugly

if($searchprice)

Might not work, but php converts 0 to false. I'll use the first one.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

I would suggest you try to use the more standard syntax for code blocks, and use echo instead of dropping in and out of PHP:

<?php
$searchprice = wspc_product_normal_price();
if($searchprice) {
  $productprice = wspc_the_product_price();
  if( $searchprice == $productprice) {
    echo '<p class="s_price" style="margin:0 0 10px 30px;">'.$searchprice.'</p>';
  }
  else {
    echo '<p class="s_price s_promo_price"><span class="s_old_price">'.$searchprice.'</span>'.$productprice.'</p>';
  }
}

Upvotes: 1

Related Questions