dswu26846 2016-10-01 18:38
浏览 211
已采纳

如何在echo php中使用if语句

Okay so I have a for loop that displays books and their associated sales info...I have it displaying correctly..I have a for loop that goes through an array and displays the information. What I would like to do is have the for loop only display information about a hardcover book or a softcover book, if their values are only more that 0; I have tried putting a for loop in the echo statement but it's giving me an error.. I have a feeling it has to do with the way am concatinating the values..anyway here is my for loop

function displayData($array){

  // create a form

 echo ' <form action="order_summary.php" method="post">';

 // for loop to go through data

  for($row = 0; $row < sizeof($array);$row++){

 echo '<div class="book-details"><img src="images/' . 
     $array[$row]['isbn'].'.jpg" alt="'.$array[$row]['title'] .'" >'. 
    '<br/>'.$array[$row]['title'].'<br/>by '.$array[$row]['author'].
    '<br/><input type="radio" name="orders['.$array[$row]['title'].
    ']" value="hardcover" >Hardcover: $'.$array[$row]['hardcover'].
    '<br/><input type="radio" name="orders['.$array[$row]['title'].
     ']" value="softcover" >Softcover: $'.$array[$row]['softcover'].
    '<br/><input type="radio" name="orders['.$array[$row]['title'].
    ']" value="e-book" >E-Book: $'.$array[$row]['e-book']."</div>";

 };

echo '<div class = "cart"><input  type="submit" value="Add Selected Items to Cart"></div>';

echo '</form>';

  }// end of function

I would like to include something like this

if (!$array[$row]['hc-quantity']== 0) {
 // display hardcover price
}
else {
   go to the next book and repeat check for softcover
 };

can you please help me work this out...

  • 写回答

2条回答 默认 最新

  • doutui2883 2016-10-01 19:08
    关注

    You could improve your code by using the foreach syntax. The if condition you suggested is close, but the ! operator is actually acting on the array element, not on the equation.

    I would also suggest closing the PHP tag to generate the HTML, instead of using echo. Then use <?= ... ?> for injecting PHP values inside your HTML. And for the if conditions you can insert some <?php ... ?> blocks.

    Here is the adapted code (for loop only):

    <?php
    // ... other code ...
    
    foreach ($array as $item){
    ?>
    <div class="book-details">
        <img src="images/<?=$item['isbn']?>.jpg" alt="<?=$item['title']?>"><br>
        <?=$item['title']?><br>
        by <?=$item['author']?><br>
    <?php
    if ($item['hc-quantity'] > 0) { 
    ?>
        <input type="radio" name="orders[<?=$item['title']?>]" value="hardcover">
            Hardcover: $<?=$item['hardcover']?><br>
    <?php
    }
    if ($item['sc-quantity'] > 0) {
    ?>
        <input type="radio" name="orders[<?=$item['title']?>]" value="softcover">
            Softcover: $<?=$item['softcover']?><br>
    <?php
    }
    ?>
        <input type="radio" name="orders[<?=$item['title']?>]" value="e-book">
            E-Book: $<?=$item['e-book']?>
    </div>
    <?php
    }
    
    // ... more code
    //
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决