0

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

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
Will Barangi
  • 145
  • 1
  • 2
  • 13
  • And what is the problem? Have you trie to add your `if` statement to your code? – u_mulder Oct 01 '16 at 18:40
  • I have but when I do it gives me an error – Will Barangi Oct 01 '16 at 18:52
  • By the way, `if (!$array[$row]['hc-quantity']== 0) { ...` should be `if ($array[$row]['hc-quantity'] != 0) { ...` – Rajdeep Paul Oct 01 '16 at 18:53
  • `if (!$array[$row]['hc-quantity']== 0)` should be if `($array[$row]['hc-quantity'] != 0)`. Remove that first exclamation point. You're trying to do two different types of comparisons there. – Robert Wade Oct 01 '16 at 18:54
  • And what error is this? – u_mulder Oct 01 '16 at 18:54
  • so what is the error then?? – Robert Wade Oct 01 '16 at 19:02
  • That only OP can tell – Hanky Panky Oct 01 '16 at 19:07
  • @HankyPanky *hmm*, yes, for integers(for this case specifically), not make much of a difference, but OP should know the correct syntax. Or may be OP already knows the difference, I'm not sure. [https://eval.in/653586](https://eval.in/653586) – Rajdeep Paul Oct 01 '16 at 19:10
  • @HankyPanky But the eval, [https://eval.in/653586](https://eval.in/653586) tells a different story. What happens here `if (!$array[$row]['hc-quantity']== 0) { ...` is, first `!$array[$row]['hc-quantity']` gets evaluated which is going to be either `true` or `false`, and then the intermediate boolean result gets compared with `0`, which will work in *this case only*, that's all I'm saying. – Rajdeep Paul Oct 01 '16 at 19:20

2 Answers2

1

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
//
?>
trincot
  • 317,000
  • 35
  • 244
  • 286
0

You can do that with recursive function or array walk recursive

Community
  • 1
  • 1
MIT
  • 140
  • 1
  • 5