JJohnsenDK Posted February 9, 2007 Share Posted February 9, 2007 Hey Quick quess I have this code: <? session_start(); $inventory = array( "001" => "Tandpasta", "002" => "Ansigtsservietter", "003" => "Vatpinde", "004" => "Shampoo", "005" => "Balsam", "006" => "Deodorant" ); global $inventory; function populate_cart() { global $name, $email, $items; $items = array(0, 0, 0, 0, 0, 0); session_register("name"); session_register("email"); session_register("items"); } function get_user_info() { ?> <h3>Indtast venligst følgende information, inden du fortsætter:</h3> <form action="session_cart.php" method="post"> <p>Navn: <input type="text" name="name"> <br>E-mail: <input type="text" name="email"> <p><input type="submit" name="infosubmit" value="Send"> </form> <? } function shop() { $name = $_POST['name']; $email = $_POST['email']; $items = $_POST['items']; ?> <p>Foretag dine indkøb her: <form action="session_cart.php" method="post"><p> <? $total = 0; for($i = 0; $i < sizeof($inventory); $i++) { ?> Varenummer: <b><?=key($inventory);?></b> Beskrivelse: <b><?=$inventory[key($inventory)]?></b> Antal: <input type="text" name="items_in[<?=$i?>]" value="<?=$items[$i]?>" size="2" maxlength="2"><br> <? next($inventory); $total = $total + $items[$i]; } ?> <p>Du har <?=$total?> varer i din indkøbsvogn. <p><input type="submit" name="additems" value="Læg i indkøbsvogn"> </form> <? if($total > 0) { ?> <form action="session_cart_checkout.php"> <input type="submit" name="checkout" value="Check ud!"> </form> <? } } /* MAIN */ if(isset($_POST['additems'])) { $items = $items_in; echo("<h3>Din indkøbsvogn er opdateret!</h3>"); shop(); }elseif(!isset($_POST['name'])) { get_user_info(); } elseif(isset($_POST['infosubmit'])) { populate_cart(); shop(); } else { shop(); } ?> How do i use the $inventory in the functions? Its something with globals right? Quote Link to comment Share on other sites More sharing options...
phil88 Posted February 9, 2007 Share Posted February 9, 2007 Instead of; global $inventory; function populate_cart() { Have; function populate_cart() { global $inventory; You'll need to do that for your other functions too. Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 9, 2007 Share Posted February 9, 2007 Rather than using functions, I would much rather see you pass your $inventory variable to your functions. Something like this: <?php function shop(&$inventory) { // Do your shop function here } // When you call shop(), pass in inventory shop($inventory); ?> The ampersand in front of the parameter in the function declaration allows you to pass the variable in by reference, meaning that you are using it the same way you would be if you made it global in scope. Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 9, 2007 Author Share Posted February 9, 2007 Aaah yes that worked. Thanks. Can you also tell why the script doesnt count the total amout of items, after i have seleted the amout i want and pressed the submit button?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.