Edit
- If there is a better way to ask this question to avoid negative votes, please let me know.
- I have looked into https://codereview.stackexchange.com/help/how-to-ask
How would I implement a class dedicated to returning html strings for a list of products/cart items?
- Written as part of an interview assignment.
- Simply this is a shopping website with some items a user may add or remove from their shopping cart.
- This function is responsible for returning the html to display for the list of cart items.
- Currently there is no dedicated class for returning html strings.
- I feel I would be moving most of this code into another class, defeating the point.
Code:
function listItems()
{
$formatter = new Formatter();
$html = "";
$id = 0;
foreach ($_SESSION["Cart"] as $item) {
if ($item["quantity"] > 0) {
$html = $html .
'<pre>
<div class="name">'.$this>getName($item) .'</div><div class="price">Price: $' . $formatter->formatPrice($item) . '</div>
<div class="quantity">Quantity: ' . $item["quantity"] . '</div>
<div class="total">Total: $' . $this->getTotal($item) . '</div>
<form class="removeForm" method="POST">
<input class="removeInput" type="hidden" name="removeId" value=' . $id . ' />
<input class="removeInput" type="submit" name="removeButton" value="Remove from Cart" />
</form>
</pre>';
}
$id++;
}
$html = $html .
'<pre>
<div class="overallTotal">Overall Total: $' . $this->getOverallTotal() . '</div>
</pre>';
return $html;
}
Formatter class method returns a number with 2 decimal point format.
Full Repository here if you want more to critique:
How would I implement
, making it look not implemented yet - down-voters, please comment! OTOH, the post does not show the implementation of such a class: if you got one, include it. (If not, I think the question off-topic.) \$\endgroup\$I [presented] this function because I thought it could be improved, but I don't have an argument as to why I think that
one of the "use cases" for CodeReview@SE. Just stay on topic and don't ask in a way to avoid. \$\endgroup\$one of the "use cases" for CodeReview@SE
. Are you saying your quote of me doesn't follow the use case? \$\endgroup\$