WEB couple ecommerce code questions

Discussion in 'OT Technology' started by BoostedBoogie, Mar 17, 2008.

  1. so i have a couple questions for the e-commerce scripts i'm working on.

    Code:
    <?php
    session_start();
    $cart = $_SESSION['cart'];
    $action = $_GET['action'];
    switch ($action) {
    	case 'add':
    		if ($cart) {
    			$cart .= ','.$_GET['id'];
    		} else {
    			$cart = $_GET['id'];
    		}
    		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;
    ?>
    
    that code allows me to add and delete items in my cart. to show the contents and modify the quantities, i'm echoing this function

    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="cart.php?action=update" method="post" id="cart">';
    		$output[] = '<table width="500" align="left">';
    		foreach ($contents as $id=>$qty) {
    			$sql = 'SELECT * FROM packages WHERE id = '.$id;
    			$result = $db->query($sql);
    			$row = $result->fetch();
    			extract($row);
    			$output[] = '<tr>';
    			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Delete</a></td>';
    			$output[] = '<td>'.$title.'</td>';
    			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="1" /></td>';
    			$output[] = '<td>'.($price * $qty).'</td>';
    			$total += $price * $qty;
    			$output[] = '</tr>';
    		}
    		$output[] = '</table>';
    		$output[] = '<br><br><br><br><p>Total: <strong>'.$total.'</strong></p>';
    		$output[] = '<div><button type="submit">Update</button></div>';
    		$output[] = '<table width="500">';
    		$output[] = '<tr>';
    		$output[] = '<td align="left"><br><p><a href="checkout.php">Proceed to Checkout</a></p></td>';
    		$output[] = '</tr>';
    		$output[] = '</table>';
    		$output[] = '</form>';
    	} else {
    		$output[] = '';
    	}
    	return join('',$output);
    }
    ?>
    
    how can i take my variables and pass them into paypal? i've never done that before so i have no idea where to start. :dunno:

    Code:
    <?php
    mysql_connect("localhost", "username", "password");
    mysql_select_db("search");
    
    $search=$_POST["search"];
    $result = mysql_query("SELECT * FROM keywords WHERE words LIKE '%$search%'");
    echo "<br><h1>Here are your search results:</h1><br>";
    
    while($row=mysql_fetch_array($result))
    {
    $pack=$row["pack"];
    $link=$row["link"];
    
    echo "<p>$pack<br>$link<br><br></p>";
    }
    ?>
    
    this code is just taking whatever keywords i put in my textfield and searching for those keywords within a database and spitting them out onto my page.

    i'm not sure why but that code is spitting out all of the data from my "keywords" database which i use for my searching right away when the search page loads. i had an else statement on there for it to be blank until someone actually searches for something and hits the submit button, but it doesn't work. im just trying to figure out how to get the search results to load only after the submit button is pressed, if that makes sense. :dunno:
     

Share This Page