Jump to content

globals? need some explaining


JJohnsenDK

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.