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; ?>

据我了解和我看到的情况,该片段返回搜索中返回的产品的价格。

但是,并非所有出现的结果都是待售产品,有时它们是博客文章,等等。所以我尝试了以下代码,但我无法让它工作。我想要发生的是,如果价格为 0,或者没有价格,则不会显示任何内容。

<?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; ?> 

再说一次,我知道我不像你们那么棒,所以我很感激你没有指出这一点,那不会解决我的问题。

谢谢!

我试过了,但价格没有显示

<?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; ?>
4

4 回答 4

2

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
于 2013-07-05T22:41:00.060 回答
1

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>';
  }
}
于 2013-07-05T22:42:22.883 回答
0

我认为你有两个选择:

if($searchprice>0)

或者丑陋的

if($searchprice)

可能不起作用,但 php 将 0 转换为 false。我会用第一个。

于 2013-07-05T22:48:56.927 回答
0

首先,我不知道返回什么wpsc_product_normal_price()wpsc_the_product_price()所以我只关注你的代码,然后我会关注你想要发生的事情。我希望通过使用我的一种解释或两种解释的组合,您将弄清楚该怎么做。

您的代码需要进行一些修改,存在语法错误(不知道是否是拼写错误)并且语法使用不太常见。我将在代码注释中解决这个问题。像你这样在php代码片段外回显html没有错,为了清楚起见我就换个方式写,html就不关注了。我使用的 if 语法可能是程序员的标准做法,我建议你习惯使用它;但当然不是强制性的。

// 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();

}

现在,改进此代码,我们将:

// 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;
}

那是关于你展示的代码,但你想要发生的是

如果价格为 0,或者没有价格,则不会显示任何内容。

对于那个精确的请求,代码将类似于(同样我不知道你的函数是做什么的或者 $searchprice 来自哪里)。

// 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.
}

因此,根据您的需要,它甚至可以更短:

if ($searchprice > 0) echo $searchprice;
于 2013-07-05T23:51:28.467 回答