2016-10-01 18:38
浏览 105

如何在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...

图片转代码服务由CSDN问答提供 功能建议

好的,我有一个for循环,显示书籍及其相关的销售信息......我正确显示它。 。我有一个循环,通过一个数组并显示信息。 我想做的是让for循环只显示有关精装书或软皮书的信息,如果它们的值仅为0; 我已经尝试在echo语句中放入一个for循环,但它给了我一个错误.. 我感觉它与我的汇总值的方式有关..这里是我的for循环

  function displayData($ array){
 echo'&lt; form action =“order_summary.php”method =“post”&gt;'; \  n 
 // for循环遍历数据
 for($ row = 0; $ row&lt; sizeof($ array); $ row ++){
 echo'&lt; div class =“book- 详情“&gt;&lt; img src =”images /'。
 $ array [$ row] ['isbn']。'。jpg“alt =”'。$ array [$ row] ['title']。'  “&gt;'。  
'&lt; br /&gt;'。$ array [$ row] ['title']。'&lt; br /&gt; by'。$ array [$ row] ['author']。
'&lt;  br /&gt;&lt; input type =“radio”name =“orders ['。$ array [$ row] ['title']。
']”value =“精装”&gt;精装:$'。$ array  [$ row] ['精装']。
'&lt; br /&gt;&lt; input type =“radio”name =“orders ['。$ array [$ row] ['title']。
']  “value =”softcover“&gt; Softcover:$'。$ array [$ row] ['softcover']。
'&lt; br /&gt;&lt; input type =”radio“name =”orders ['。$  array [$ row] ['title']。
']“value =”e-book“&gt;电子书:$'。$ array [$ row] ['e-book']。”&lt; /  div&gt;“; 
echo'&lt; div class =”cart“&gt;&lt; input type =”submit“value =”将所选商品添加到购物车“&gt;&lt; / div&gt;'  ; 
echo'&lt; / form&gt;'; 
}} //函数结束


  if(!$ array [$ row] ['hc-quantity'] == 0){
else {
转到 下一本书并重复检查软件


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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):

    // ... other code ...
    foreach ($array as $item){
    <div class="book-details">
        <img src="images/<?=$item['isbn']?>.jpg" alt="<?=$item['title']?>"><br>
        by <?=$item['author']?><br>
    if ($item['hc-quantity'] > 0) { 
        <input type="radio" name="orders[<?=$item['title']?>]" value="hardcover">
            Hardcover: $<?=$item['hardcover']?><br>
    if ($item['sc-quantity'] > 0) {
        <input type="radio" name="orders[<?=$item['title']?>]" value="softcover">
            Softcover: $<?=$item['softcover']?><br>
        <input type="radio" name="orders[<?=$item['title']?>]" value="e-book">
            E-Book: $<?=$item['e-book']?>
    // ... more code
    打赏 评论
  • douwo3665 2016-10-01 19:20

    You can do that with recursive function or array walk recursive

    打赏 评论

相关推荐 更多相似问题