By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,643 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,643 IT Pros & Developers. It's quick & easy.

Im trying to understand php classes please help

P: n/a
Im tring to learn about classes with php. I've read alot of posts here
and look at other peoples code. So I appied all the absorbed to this.
Im tring to build a Shopping cart. not for live use but just to learn
about classes and get better at php. Anyway, i put this code together
and can't get it to work. it may be comletely wrong and if it is
please direct me to a good tutorial.
But at the risk of looking like a complete ass. here is my code.
please be kind.

A page calls a page with this code;
(showCart.php)
<?php
require( 'ShoppingCenter.php' );

$item = new ShoppingCenter( $itemArray );

$item->addToCart();
$itemList = getUnkownKeyValues( $item->getFileName() );
?>

This is the ShoppingCenter class:
note that the inifunc.php is code i writen/found to write out to a
file.
(ShoppingCenter.php)
<?php
include( 'inifunc.php' );

class ShoppingCenter
{
var $fileName = "tfsmc.txt";
var $iArray;
var $qty = 0,
$itemNumber = 0,
$itemCost = 0,
$totalCost = 0,
$itemDescription = "";

function getFileName()
{
return $fileName;
}

function ShoppingCenter( $itemArray )
{
$this->iArray = $itemArray;
$this->itemNumber = $itemArray[0];
$this->itemDescription = $itemArray[1];
$this->itemCost = $itemArray[2];
$this->qty = $itemArray[3];

$totalCost = $qty * $itemCost;
$totalCost = number_format($totalCost, 2, '.', '');
$this->iArray[2] = $totalCost;
}

function addToCart()
{
if( !ifSectionExisits( $itemNumber ) )
{
foreach( $iArray as $key => $value )
{
write_key($this->itemNumber, $key, $value, $this->file_name);
}
}
}
}
?>

After the ShoppingCenter is called the showCart.php "SHOULD" display
what was writen to the file:
(showCart.php)
<?php
for($i = 0;$i<count($itemList);$i++)
{
foreach($itemList[$i] as $key => $value)
{
if( $key == "removed")break;
echo "<td>$value</td>";
if( $key == "totalCost" )echo"<td><div align='center'><input
type='submit' name='Submit' value='Remove'></div></td></tr>";
}
}
?>
if more is needed please let me know and if this just sucks please let
me know. also i know that there are probably much better ways of doing
things like using sessions and such. but like i said i just want to
learn .
Thanks a million in advance.
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.