Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
alexismaillard
2,023 PointsHELP for Extra Credit Challenge : "Build a Simple PHP Application" Stage 6
Hi everyone,
Noob here.
"Users might like to add a shirt to the cart directly from the home page or the Shirts Listing page. Add to the function we just wrote so that it displays a dropdown field and an Add to Cart button."
First try: I tried to concatenate via a variable $addtocart the Paypal HTML code into the function hold in the index.php page:
<?php
$total_products = count($products);
$position = 0;
$list_view_html = " ";
$addtocart = '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="<?php echo $product[\"paypal\"]; ?>"><input type="submit" value="Add to Cart" name="submit"></form>';
foreach($products as $product_id => $product) {
$position = $position + 1;
if ($total_products - $position < 4) {
$list_view_html = get_list_view_html($product_id,$product) . $list_view_html . $addtocart;
}
}
echo $list_view_html;
?>
Second try: I tried to concatenate the paypal button HTML code of the button into the function hold in the products.php page:
function get_list_view_html($product_id, $product) {
$output = "";
$output = $output . "<li>";
$output = $output . '<a href="shirt.php?id=' . $product_id . '">';
$output = $output . '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
$output = $output . "<p>View Details</p>";
$output = $output . "</a>";
$output = $output . "</ br>";
$output = $output . '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick">' . '<input type="hidden" name="hosted_button_id" value="<?php echo $product[\"paypal\"]; ?>">' . '<input type="submit" value="Add to Cart" name="submit"></form>';
$output = $output . "</li>";
return $output;
}
It displays a Add to cart button but the php code "value="<?php echo $product["paypal"]; ?>" is not working and shown as text.
I guess there is a much simplier alternative!
Thank you!
1 Answer
alexismaillard
2,023 Points//edit of the Markdown Cheatsheet