4 Replies - 526 Views - Last Post: 16 March 2014 - 04:13 PM Rate Topic: -----

#1 hazelnutz77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-December 13

Help passing a variable from function to function......

Posted 16 March 2014 - 01:03 PM

first of all, i am only 6 weeks into php and programming in general so bear with me. i'm creating an application that starts on the main page listing ten parts with price and displaying them. then you can click on the add part link to kick you to a new area where you select the quantity of that part and then place it in the cart. the cart displays your item, quantity, price etc. I cant figure out how to send the selected part when clicking the link to the getQuantity function where it says...... You have selected < variable here >. i tried using <?php echo $part->getName() ?> but erroring. help and thank you in advance!

<?php
session_start();


class Part {
	private $partId;
	private $partName;
	private $partPrice;
	
	public function __construct($partId, $partName, $partPrice) {
		$this->partId = $partId;
		$this->partName = $partName;
		$this->partPrice = $partPrice;
		}
	public function getId() {
		return $this->partId;
	}
	public function getName() {
		return $this->partName;
	}
	public function getPrice() {
		return $this->partPrice;
	}
		
}
$parts = array(
	1 => new Part (1, "Front Bumper", 199.99),
	2 => new Part (2, "Rear Bumper", 169.990),
	3 => new Part (3, "Front Struts", 219.99),
	4 => new Part (4, "Rear Struts", 219.99),
	5 => new Part (5, "Exhaust", 399.99),
	6 => new Part (6, "Headers", 299.99),
	7 => new Part (7, "Air Intake", 149.99),
	8 => new Part (8, "Turbo", 499.99),
	9 => new Part (9, "Supercharger", 429.99),
	10 => new Part (10, "Oil Pan", 79.99)
);

if (!isset($_SESSION["list"])) $_SESSION["list"] = array();

if (isset($_POST["selectNo"])) {
	displayCart();
	}
elseif (isset($_GET["action"]) and $_GET["action"] == "getQuantity") {
	getQuantity();
}
else {	
	displayList();
}

function displayList() {
	displayPageHeader();
	global $parts;
?>
	<h1 align="center">Parts List</h1>
	<dl align="center">
<?php foreach($parts as $part){ ?>
	<dt>******<?php echo $part->getName() ?>******<dt>
	<dd>$<?php echo number_format($part->getPrice(), 2) ?>
	<a href="midtermMain.php?action=getQuantity&partId=<?php echo $part->getId() ?>" style="text-decoration:none">&nbsp;&nbsp;Add Part To Shopping Cart<a/><br><br>
	</dd>
<?php 
} 
?>
	</dl>
	</body>
</html>
<?php
}

function displayPageHeader() {
?>
<html>
<head>
<style>
th, td {
	border:1px solid gray;
	height:20px;
	width:200px;
	text-align:center;
}
</style>
</head>
	<body>
	<hr>
	<h1 align="center">Welcome to Hayes' After-market Auto Parts Online Warehouse!</h1>
	<br>
	<hr>

<?php
}

function getQuantity() {
	displayPageHeader();
	global $parts;

?>
	<br>
	<p align="center">You have selected the            </p>
	<form align="center" action="midtermMain.php" method="post">
	<label for="quantity">Please select the Quantity you would like.</label>
	<input type="text" size="10" name="selectNo" id="selectNo" placeholder="quantity" value=""/><br>
	<input type="submit" name="submit" id="submit" value="Place in Cart"/>
	</form>
<?php
}

function displayCart() {
	displayPageHeader();
	global $parts;
?>

	<br>
	<table style="border:1px solid gray;border-collapse:collapse;">
		<tr><th>ITEM</th><th>QUANTITY</th><th>UNIT PRICE</th><th>TOTAL PRICE</th></tr>
		<tr><td> </td><td> </td><td> </td><td> </td></tr>
	</table><br><br>
	<caption>ADDRESS ENTRY</caption><br><br>
	<form action="midtermMain.php" method="post">
	<label for="street">STREET</label>
	<input type="text"  name="street" id="street" placeholder="street" value=""/><br>
	<label for="city">CITY</label>
	<input type="text"  name="city" id="city" placeholder="city" value=""/><br>
	<label for="state">STATE</label>
	<select name="state">
	<option value="AL">AL</option>
	<option value="AK">AK</option>
	<option value="AZ">AZ</option>
	<option value="AR">AR</option>
	<option value="CA">CA</option>
	<option value="CO">CO</option>
	<option value="CT">CT</option>
	<option value="DE">DE</option>
	<option value="DC">DC</option>
	<option value="FL">FL</option>
	<option value="GA">GA</option>
	<option value="HI">HI</option>
	<option value="ID">ID</option>
	<option value="IL">IL</option>
	<option value="IN">IN</option>
	<option value="IA">IA</option>
	<option value="KS">KS</option>
	<option value="KY">KY</option>
	<option value="LA">LA</option>
	<option value="ME">ME</option>
	<option value="MD">MD</option>
	<option value="MA">MA</option>
	<option value="MI">MI</option>
	<option value="MN">MN</option>
	<option value="MS">MS</option>
	<option value="MO">MO</option>
	<option value="MT">MT</option>
	<option value="NE">NE</option>
	<option value="NV">NV</option>
	<option value="NH">NH</option>
	<option value="NJ">NJ</option>
	<option value="NM">NM</option>
	<option value="NY">NY</option>
	<option value="NC">NC</option>
	<option value="ND">ND</option>
	<option value="OH">OH</option>
	<option value="OK">OK</option>
	<option value="OR">OR</option>
	<option value="PA">PA</option>
	<option value="RI">RI</option>
	<option value="SC">SC</option>
	<option value="SD">SD</option>
	<option value="TN">TN</option>
	<option value="TX">TX</option>
	<option value="UT">UT</option>
	<option value="VT">VT</option>
	<option value="VA">VA</option>
	<option value="WA">WA</option>
	<option value="WV">WV</option>
	<option value="WI">WI</option>
	<option value="WY">WY</option>
	</select><br>
	<label for="zip">ZIP</label>
	<input type="text"  name="zip" id="zip" placeholder="zip" value=""/><br>
<?php
}


Attached Image
Attached Image
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Help passing a variable from function to function......

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,180
  • Joined: 08-August 08

Re: Help passing a variable from function to function......

Posted 16 March 2014 - 01:30 PM

  • You're not storing a session variable, so each refresh is a brand new page.
  • Your data should not be held in a static array. Use a database.
  • Mixing languages (HTML, PHP, CSS) will only make your code harder to read and harder to debug.

Was This Post Helpful? 0
  • +
  • -

#3 hazelnutz77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-December 13

Re: Help passing a variable from function to function......

Posted 16 March 2014 - 01:40 PM

hmmmmm...thanks for the reply. well we haven't even touched databases yet so i assume this must be possible somehow without one. Yeah sorry for the ugly coding. i thought my parts array was stored as a session variable?no?
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 625
  • View blog
  • Posts: 2,815
  • Joined: 03-December 12

Re: Help passing a variable from function to function......

Posted 16 March 2014 - 01:43 PM

Skimming the code it looks like you are missing some semicolons in a few places.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,180
  • Joined: 08-August 08

Re: Help passing a variable from function to function......

Posted 16 March 2014 - 04:13 PM

View Posthazelnutz77, on 16 March 2014 - 03:40 PM, said:

i thought my parts array was stored as a session variable?no?

No. This would do it, but then you'd need to use the session variable if it's set, and since the values won't change the session variable would be superfluous:
$_SESSION['parts_list'] = $parts;

Honestly, you have many problems with your code, and usually by the time some one starts using classes they stop breaking in/out of PHP so much, so I'm not sure where to begin.

This post has been edited by CTphpnwb: 16 March 2014 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1