Im trying to understand php classes please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • todd

    Im trying to understand php classes please help

    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 = getUnkownKeyVal ues( $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,
    $itemDescriptio n = "";

    function getFileName()
    {
    return $fileName;
    }

    function ShoppingCenter( $itemArray )
    {
    $this->iArray = $itemArray;
    $this->itemNumber = $itemArray[0];
    $this->itemDescriptio n = $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( !ifSectionExisi ts( $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($ite mList);$i++)
    {
    foreach($itemLi st[$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.
Working...