Jump to content

aebstract

Members
  • Posts

    1,105
  • Joined

  • Last visited

Posts posted by aebstract

  1. When I explode by ; I am having a slight issue. If I have 5;1 1/2 and I explode by ;, I end up with 5; and 1 1/2. I want the ; to go away completely. Not sure why it isn't?

    function showCart2() {
    $cart = $_SESSION['cart'];
    echo "$cart";
    $var = explode(',',$cart);
    $cnt = count($var);
    
    for ($i=0; $i<$cnt; $i++) {
        list($id, $size) = explode(";", $var[$i]);
    
    if (isset($size)) {
       echo "ID is $id and Size is $size <br />";
    } else {
       echo "ID is $id and there is no size! <br />";
    }
    
    }
    }
    

  2. Okay here is where I put it in, which may be where my problem is:

    
    function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	list($item,$size) = explode(';',$item);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    	}
    
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	foreach ($contents as $id=>$qty) {
    
    
    
    		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
    		$output[] = '<tr>';
    		$output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
    		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
    		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td width="80">$'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    		$output[] = '</table>';
    	}
    	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
    	$output[] = '<div><button name="update" type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }
    
    
    

     

    I also have this neat function, just for testing:

     

    function showCart2() {
    $cart = $_SESSION['cart'];
    
    $var = explode(',',$cart);
    $cnt = count($var);
    
    for ($i=0; $i<$cnt; $i++) {
        list($id, $size) = explode(";", $var[$i]);
    
    if (isset($size)) {
       echo "ID is $id and Size is $size <br />";
    } else {
       echo "ID is $id and there is no size! <br />";
    }
    
    }
    }
    

     

    I echo that function out just to make sure everything is working. With that second function I get these results:

    ID is 8 and there is no size!

    ID is 1 and there is no size!

    ID is 10 and Size is 1.5

     

    However, with my main one that I am trying to get working, I end up having the first two products show correctly and my third item shows as the second item again.

     

     

    BPE-2009-2517

    $24.63 - size - $24.63

     

    BPE-2008-2505

    $15.64 - size - $15.64

     

    BPE-2008-2505

    $15.64 - size - $15.64

     

    The first two items are correct, but the third item should not be BPE-2008-2505 (that's the second item, 1).

  3. Okay I have this code inside functions.php:

    function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    	}
    
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	foreach ($contents as $id=>$qty) {
    
    
    
    		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
    		$output[] = '<tr>';
    		$output[] = '<td width="100"><img src="products/' .$partnumber. '&#45;cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
    		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
    		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td width="80">$'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    		$output[] = '</table>';
    	}
    	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
    	$output[] = '<div><button name="update" type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }
    

     

    Which is all there really is to the cart. If you want it, there is this, inside cart.php:

     

    
    
    $cart = $_SESSION['cart'];
    $action = $_GET['action'];
    switch ($action) {
    case 'add':
    	if ($cart) {
    		$cart .= ','.$_GET['id'];
    		header("Location: index.php?page=cart");
    		exit;
    	} else {
    		$cart = $_GET['id'];
    		header("Location: index.php?page=cart");
    		exit;		}
    	break;
    case 'delete':
    	if ($cart) {
    		$items = explode(',',$cart);
    		$newcart = '';
    		foreach ($items as $item) {
    			if ($_GET['id'] != $item) {
    				if ($newcart != '') {
    					$newcart .= ','.$item;
    				} else {
    					$newcart = $item;
    				}
    			}
    		}
    		$cart = $newcart;
    	}
    	break;
    case 'update':
    if ($cart) {
    	$newcart = '';
    	foreach ($_POST as $key=>$value) {
    		if (stristr($key,'qty')) {
    			$id = str_replace('qty','',$key);
    			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
    			$newcart = '';
    			foreach ($items as $item) {
    				if ($id != $item) {
    					if ($newcart != '') {
    						$newcart .= ','.$item;
    					} else {
    						$newcart = $item;
    					}
    				}
    			}
    			for ($i=1;$i<=$value;$i++) {
    				if ($newcart != '') {
    					$newcart .= ','.$id;
    				} else {
    					$newcart = $id;
    				}
    			}
    		}
    	}
    }
    $cart = $newcart;
    break;
    }
    $_SESSION['cart'] = $cart;

    Just so noone asks for it later for any reason. Now when you add an item to your cart, it is set in the session variable as the id from the database. So if you add several items, the variable maybe be. 4,4,5,6,2,4. For my own reasons, I need to be able to add an item to that session, such as: 3,4,5,2,5;1.5,2,2,3. Right now this will work, and what it does is try to fetch 5;1.5 as the id. I want it to use 5 as the id and 1.5 as $size. So if a ; exists within the $id variable it should explode it again in to $id and $size.

  4. Well for starters, look at what I said in my last reply:

     

    "Resource id #5, however it is actually set as 8. "

     

    I selected a part with id 8, the session has it set as id 8, though it is reading as "Resource id #5". Also read what I said about the images, the $id is not being set correctly with this.

  5. function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    	}
    
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	foreach ($contents as $id=>$qty) {
    
    list($id, $size) = explode(';', $cart);
    
    
    		$sql = mysql_query("SELECT * FROM p_products WHERE id = '.$id.'") or die(mysql_error());;
    		echo "$sql";
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
    		$output[] = '<tr>';
    		$output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
    		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
    		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td width="80">$'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    		$output[] = '</table>';
    	}
    	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
    	$output[] = '<div><button type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }
    
    

     

    I added one item to my cart, without a ; just a single item to make it easy and now it's not even doing that correctly.

     

    Resource id #5, however it is actually set as 8.

  6. Okay I took the entire if statement out and replaced it with what you stated. Here is what I am getting:

     

    ID is 8 and there is no size!

    ID is 4 and Size is 1.5

     

    this is with: 8,4;1.5

    which is correct, but this isn't part of my query dealing with the db.

    the part with the query is display this:

     

     

     

    [broken img]$ - size - 1.5

     

    [broken img]$ - size - 1.5

     

    The broken img, is suppose to be

    /products/$partnumber-cart.jpg

    it grabs that partnumber from the database based on id, it is outputting

    /products/-cart.jpg

    Here is the code:

    function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    	}
    
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	foreach ($contents as $id=>$qty) {
    
    list($id, $size) = explode(';', $cart);
    
    
    		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
    		$output[] = '<tr>';
    		$output[] = '<td width="100"><img src="products/' .$partnumber. '-cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
    		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
    		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td width="80">$'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    		$output[] = '</table>';
    	}
    	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
    	$output[] = '<div><button type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }
    
    

     

  7. Having trouble with this and don't really know what to call it. I've got my session set as something like: 4,4,5;10,3,5

    Which is fine, and when I run it through to my shopping cart, I get my results displayed where the number = id in database. However, for the item 5;10, it just displays the information for the item above it and doesn't do anything for it. What I'm trying to do is check to see if the "id" has a ";" in it and if it does, explode that in to 2 parts, "5" and "10" and set the 5 as the id and use the 10 as "$size". Here is what I have:

     

    
    function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    	}
    
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	foreach ($contents as $id=>$qty) {
    
    if(!strrpos($id, ";")){
    list ($id, $size) = explode (";", $id);
    }
    
    		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
    		$output[] = '<tr>';
    		$output[] = '<td width="100"><img src="products/' .$partnumber. '&#45;cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
    		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  '.$size.'</td>';
    		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td width="80">$'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    		$output[] = '</table>';
    	}
    	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
    	$output[] = '<div><button type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }
    
    
    

  8. I switched around the , and the ; since it needs to explode by , first. Here it is:

    
    function showCart2() {
    $cart = $_SESSION['cart'];
    
    $var = explode(',',$cart);
    $cnt = count($var);
    
    for ($i=0; $i<$cnt; $i++) {
        list($id, $size) = explode("; ", $var[$i]);
    
       echo "ID is $id and Size is $size <br />";
    }
    }
    

    It is echoing:

    ID is 8 and Size is

    ID is 10;1.5 and Size is

    ID is 3 and Size is

     

    I'm gonna continue to chug away at it and see if I can get it to separate the 10 from 1.5 and set 1.5 as the size variable and keep 10 as the id. If anyone has any input that would be great!

  9. function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    
    	}
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	foreach ($contents as $id=>$qty) {
    		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<table width="590" style="border-top: 1px solid #000;">';
    		$output[] = '<tr>';
    		$output[] = '<td width="100"><img src="products/' .$partnumber. '&#45;cart.jpg" class="jkimagelarge" title="products/' .$partnumber. '.jpg" /></td>';
    		$output[] = '<td width="350">'.$partname.' <br /> '.$partnumber.' <br /> $'.$price.' - size -  $'.$price.'</td>';
    		$output[] = '<td width="60"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td width="80">$'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    		$output[] = '</table>';
    	}
    	$output[] = '<p align="right">Grand total: $'.$total.'</p>';
    	$output[] = '<div><button type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }

     

    Okay, to add to the session is something like action=add&id=#. Which then this script will take the session and divide up all the items. So the array may be like 8,8,8,4,6,6,3,2,7,8. Which with this script will display having 4 of item 8, 2 of item 6, etc. I need to add a special thing to this and I about have it figured out but kinda lost. So I am going to give the option of a "size", the way I am thinking of formatting this is to add a ;# on to my numbers I have. So add&id=#;# = 8;1.5. Then in my array it would be:

    8;1.5,8;1.5,8;1.2,4,6,6,3;1.4,2,7,8

    Something like that, and explode the results of the first explode by ; to separate the first number from the second, then I need to set the first number as $id and would like to set the second number as $size. Can anyone point me in the right direction? I've already got it so it adds the ;# in the session, I just need to figure out how to explode and set the variable ;(

  10. I'm getting two errors, but the second one is what has me worried as I have never had this sort of error and don't know what could be causing it. I'm using a bunch of code to get me started on a cart, which is:

     

    
    <?php
    
    
    
    /**
    * MySQL Database Connection Class
    * @access public
    * @package SPLIB
    */
    class MySQL {
        /**
        * MySQL server hostname
        * @access private
        * @var string
        */
        var $host;
    
        /**
        * MySQL username
        * @access private
        * @var string
        */
        var $dbUser;
    
        /**
        * MySQL user's password
        * @access private
        * @var string
        */
        var $dbPass;
    
        /**
        * Name of database to use
        * @access private
        * @var string
        */
        var $dbName;
    
        /**
        * MySQL Resource link identifier stored here
        * @access private
        * @var string
        */
        var $dbConn;
    
        /**
        * Stores error messages for connection errors
        * @access private
        * @var string
        */
        var $connectError;
    
        /**
        * MySQL constructor
        * @param string host (MySQL server hostname)
        * @param string dbUser (MySQL User Name)
        * @param string dbPass (MySQL User Password)
        * @param string dbName (Database to select)
        * @access public
        */
        function MySQL ($host,$dbUser,$dbPass,$dbName) {
            $this->host=$host;
            $this->dbUser=$dbUser;
            $this->dbPass=$dbPass;
            $this->dbName=$dbName;
            $this->connectToDb();
        }
    
        /**
        * Establishes connection to MySQL and selects a database
        * @return void
        * @access private
        */
        function connectToDb () {
            // Make connection to MySQL server
            if (!$this->dbConn = @mysql_connect($this->host,
                                          $this->dbUser,
                                          $this->dbPass)) {
                trigger_error('Could not connect to server');
                $this->connectError=true;
            // Select database
            } else if ( !@mysql_select_db($this->dbName,$this->dbConn) ) {
                trigger_error('Could not select database');
                $this->connectError=true;
            }
        }
    
        /**
        * Checks for MySQL errors
        * @return boolean
        * @access public
        */
        function isError () {
            if ( $this->connectError )
                return true;
            $error=mysql_error ($this->dbConn);
            if ( empty ($error) )
                return false;
            else
                return true;
        }
    
        /**
        * Returns an instance of MySQLResult to fetch rows with
        * @param $sql string the database query to run
        * @return MySQLResult
        * @access public
        */
        function query($sql) {
            if (!$queryResource=mysql_query($sql,$this->dbConn))
                trigger_error ('Query failed: '.mysql_error($this->dbConn).
                               ' SQL: '.$sql);
            return new MySQLResult($this,$queryResource);
        }
    }
    
    /**
    * MySQLResult Data Fetching Class
    * @access public
    * @package SPLIB
    */
    class MySQLResult {
        /**
        * Instance of MySQL providing database connection
        * @access private
        * @var MySQL
        */
        var $mysql;
    
        /**
        * Query resource
        * @access private
        * @var resource
        */
        var $query;
    
        /**
        * MySQLResult constructor
        * @param object mysql   (instance of MySQL class)
        * @param resource query (MySQL query resource)
        * @access public
        */
        function MySQLResult(& $mysql,$query) {
            $this->mysql=& $mysql;
            $this->query=$query;
        }
    
        /**
        * Fetches a row from the result
        * @return array
        * @access public
        */
        function fetch () {
            if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) {
                return $row;
            } else if ( $this->size() > 0 ) {
                mysql_data_seek($this->query,0);
                return false;
            } else {
                return false;
            }
        }
    
        /**
        * Returns the number of rows selected
        * @return int
        * @access public
        */
        function size () {
            return mysql_num_rows($this->query);
        }
    
        /**
        * Returns the ID of the last row inserted
        * @return int
        * @access public
        */
        function insertID () {
            return mysql_insert_id($this->mysql->dbConn);
        }
    
        /**
        * Checks for MySQL errors
        * @return boolean
        * @access public
        */
        function isError () {
            return $this->mysql->isError();
        }
    }
    
    
    function checkNum($checknum){
      return ($checknum%2) ? TRUE : FALSE;
    }
    
    
    $host = 'localhost';
    $user = 'berryequipment';
    $pass = 'gU8Kso8Y';
    $name = 'berryequipment_net_db';
    $db = &new MySQL($host,$user,$pass,$name);
    
    
    function writeShoppingCart() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
    return '(0)';
    } else {
    // Parse the cart session variable
    $items = explode(',',$cart);
    $s = (count($items) > 1) ? 's':'';
    return '(<a href="cart.php">'.count($items).'</a>)';
    }
    }
    
    
    
    function showCart() {
    $cart = $_SESSION['cart'];
    if ($cart) {
    	$items = explode(',',$cart);
    	$contents = array();
    	foreach ($items as $item) {
    		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    	}
    	$output[] = '<form action="index.php?page=cart&action=update" method="post" id="cart">';
    	$output[] = '<table>';
    	foreach ($contents as $id=>$qty) {
    		$sql = 'SELECT * FROM p_products WHERE id = '.$id;
    		$result = $db->query($sql);
    		$row = $result->fetch();
    		extract($row);
    		$output[] = '<tr>';
    		$output[] = '<td><a href="index.php?page=cart&action=delete&id='.$id.'" class="r">Remove</a></td>';
    		$output[] = '<td>'.$title.' by '.$author.'</td>';
    		$output[] = '<td>£'.$price.'</td>';
    		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    		$output[] = '<td>£'.($price * $qty).'</td>';
    		$total += $price * $qty;
    		$output[] = '</tr>';
    	}
    	$output[] = '</table>';
    	$output[] = '<p>Grand total: £'.$total.'</p>';
    	$output[] = '<div><button type="submit">Update cart</button></div>';
    	$output[] = '</form>';
    } else {
    	$output[] = '<p>You shopping cart is empty.</p>';
    }
    return join('',$output);
    }
    
    
    
    ?>
    

     

    My errors are:

     

    Notice: Undefined variable: db in /home/virtual/site21/fst/var/www/html/performance/functions.php on line 232

     

    Fatal error: Call to a member function query() on a non-object in /home/virtual/site21/fst/var/www/html/performance/functions.php on line 232

×
×
  • 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.