so, i've been trying learn php. i've got assignment i'm supposed make list of products prices, , depending on day is, it's supposed give discount. works far. however, 1 thing i'm trying not echo result directly in code; want create function takes necessary parameters (taste, price, amount) , returns formatted string. haven't been able this, despite trying lot. here's code have. first time i'm using stack overflow, forgive me if didn't type in correctly.
<?php $donuts = array( array("strawberry", 10), array("chocolate", 20), array("vanilla", 30) ); $weekday = date('n'); $week = date('w'); $hour = date('g'); if ($weekday == 4) { echo "hi"; } ?> <?php $today = date("l f j, y"); if ($weekday % 2 == 1 && $week % 2 == 1 && $hour >= 14 && $hour < 17){ echo "congratulations! 5% discount, , wares delivered tomorrow!"; } print "$today"; ?> <table border="1"> <tr> <th>taste</th> <th>price</th> <th>discount</th> <th>amount</th> </tr> <?php for($i = 0; $i<count($donuts); $i++) { echo "<tr>"; echo "<td>".$donuts[$i][0]."</td>"; if( $weekday == 1){ echo"<td>".getpricepercent($donuts[$i][1], .5)." kr</td>"; } if( $weekday == 3){ echo"<td>".getpricepercent($donuts[$i][1], 1.1)." kr</td>"; } $pris = $donuts[$i][1]; if ($weekday == 5 && $pris > 20){ echo"<td>".($pris-1)." kr</td>"; } else { echo"<td>".getprice($donuts[$i][1], .9)." kr</td>";} ?> <td><input type="number" name="donuts-<?php echo $i; ?>" /></td> <?php echo "</tr>"; } ?> </table> <?php function getprice($price, $percent) { return ($price); } function getpricepercent($price, $percent){ return ($price * $percent); } ?>
i dont understand trying print in price
, discount
section. can add getdiscount function make code clean.
function getdiscount($weekday, $price){ if($weekday == 1){ return .5; } else if($weekday == 3){ return 1.1; } else if($weekday == 5 && $price > 20){ return $price-1; } }
then can that,
<table border="1"> <tr> <th>taste</th> <th>price</th> <th>discount</th> <th>amount</th> </tr> <?php for($i = 0; $i<count($donuts); $i++) { echo "<tr>"; echo "<td>".$donuts[$i][0]."</td>"; echo"<td>".getpricepercent($donuts[$i][1], getdiscount($weekday,$donuts[$i][1]))." kr</td>"; echo"<td>".getprice($donuts[$i][1], getdiscount($weekday,$donuts[$i][1]))." kr</td>"; ?> <td><input type="number" name="donuts-<?php echo $i; ?>" /></td> <?php echo "</tr>"; } ?> </table>
still, dont understand, trying dynamically update values of price depending on amount ?
Comments
Post a Comment