Page 2 of 3

Posted: Wed Dec 27, 2006 11:45 am
by crazy8
Well I have had some luck and come across a few things. Now I dont know if either of these will work for me or not, and im not sure how to edit either of these either so if either of these will work for what I need some help on how to edit the scripts/form would be a huge help.

First I found this http://www.zend.com/codex.php?id=112&single=1

And then I also came across this

Code: Select all

<? 

class cart { 
  var $username = "username"; 
  var $password = "password"; 
  var $database = "your_db"; 
  var $hostname = "localhost"; 
  var $inv_table = "inventory"; 
  var $cart_table = "shopping";  


	function cart($cart_id) { 
	   $this->dblink = mysql_connect($this->hostname, $this->username, $this->password); 
	   mysql_select_db($this->database, $this->dblink); 
	   $this->cart_id = $cart_id; 
	}  
	
	
	function add_item($product, $color, $quantity) { 
		$qty = $this->check_item($product,$color); 
	
		if($qty == 0) { 
			$query = "INSERT INTO ".$this->cart_table . " (session, product, color, quantity) VALUES ('".$this->cart_id."', '$product', '$color', '$quantity') "; 
			mysql_query($query, $this->dblink); 
		} else { 
			$quantity += $qty; 
			$query = "UPDATE ".$this->cart_table . " SET quantity='$quantity' WHERE session='".$this->cart_id."' AND product='$product' AND color='$color' "; 
			mysql_query($query, $this->dblink); 
		} 
		return true; 
	}  
	
	
	function check_item($product, $color) { 
	
		$query = "SELECT quantity FROM ".$this->cart_table. " WHERE session='".$this->cart_id."' AND product='$product' AND color='$color' "; 
		$result = mysql_query($query, $this->dblink); 
	
		if(!$result) { 
			return 0; 
		} 
	
		$numRows = mysql_num_rows($result); 
	
		if($numRows != 0) { 
			$row = mysql_fetch_object($result); 
			return $row->quantity; 
		} 
	
	  }  
	
	
	function delete_item($product, $color) { 
		$query = "DELETE FROM ".$this->cart_table." WHERE session='" . $this->cart_id."' AND product='$product' AND color='$color' "; 
		mysql_query($query, $this->dblink); 
	 } 
	
	
	function clear_cart() { 
		$query = "DELETE FROM ".$this->cart_table." WHERE session='".$this->cart_id."' "; 
		mysql_query($query, $this->dblink); 
	 }  
	
	
	
	function modify_quantity($product, $color, $quantity) { 
	
		if($quantity <= 0)  return $this->delete_item($product);
	
		$query = "UPDATE ".$this->cart_table. " SET quantity='$quantity' WHERE session='".$this->cart_id."' AND product='$product' AND color='$color'"; 
		mysql_query($query, $this->dblink); 
	
		return true; 
	 }  
	
	
	function get_contents() {
		
		//get stuff in the cart and return in indexed array
		$q1 = "SELECT * FROM ". $this->cart_table. " WHERE session='".$this->cart_id."'";
		$incart_r = mysql_query($q1,$this->dblink) or die(mysql_error());
		
		$contents = array();
		
		while($incart = mysql_fetch_array($incart_r)){
			//get the item's price first
			$q2 = "SELECT price FROM ". $this->inv_table. " WHERE product='".$incart["product"]."'";
			$price_row = mysql_query($q2) or die(mysql_error());
			$price = mysql_fetch_row($price_row);
			
			//build array of info
			$item = array($incart['product'],$incart['color'],$incart['quantity'],$price[0]);
			array_push($contents,$item);
		}
		
		return $contents;
	} 
	
	
	function cart_total() {
	   $contents = $this->get_contents();
	   $total = 0;
	   for($i=0; $i < count($contents); $i++){
			$total = $total + ($contents[$i][3] * $contents[$i][2]);
	   }
	   $total = "$".number_format($total, 2, '.', '');
	   return $total;
	}
	
	
	function quant_items() { 
	   $contents = $this->get_contents();
	   $q = 0;
	   for($i=0; $i < count($contents); $i++){
			$q = $q + $contents[$i][2];
	   }
	   return $q;
	 } 
 

} 
?>
which came with a second script which is this form.

Code: Select all

<? 
// Cart Cookie Creator //////////////////////////
if(!isset($HTTP_COOKIE_VARS['cart_id'])) { 
     $cart_id = md5(uniqid(rand())); 
     setcookie("cart_id", $cart_id, time() + 14400); 
 } else { 
      $cart_id = $HTTP_COOKIE_VARS['cart_id']; 
 }
/////////////////////////////////////////////////

 
// Make the Cart Object /////////////////////////
require_once("shoppingcart.php");
$cart = new cart($cart_id);
/////////////////////////////////////////////////


// Select Action ////////////////////////////////
switch($_GET['a']){
	
	case "add":
		if(isset($_POST['p']) and isset($_POST['c']) and isset($_POST['q'])) $cart->add_item($_POST['p'],$_POST['c'],$_POST['q']);
		break;
		
	case "update":
		$cart->modify_quantity($_POST["p"],$_POST['c'],$_POST["q"]);
		break;
	
	case "delete":
		$cart->delete_item($_POST['p'],$_POST['c']);
		break;
		
	case "clear":
		$cart->clear_cart();
		break;
}
///////////////////////////////////////////////


// Begin Cart Display /////////////////////////
$c = $cart->get_contents();
$cart_tbl = "
	<!-- Begin Cart Display -->
	<table summary='Your shopping cart' id='cart'>
		<tr>
			<th>Item</th><th>Color</th><th>Quantity</th><th>Price</th><th>&nbsp</th>
		</tr>";

if(count($c) <= 0){ //nothing in cart

	$cart_tbl .= "
		<tr>
			<td class='items' colspan='5'>Your cart is empty</td>
		</tr>";
				  
}else{ //stuff in the cart so show it

	for($i=0; $i < count($c); $i++){
		$cart_tbl .= "
		<tr>
			<td class='items'>" . $c[$i][0] . "</td>
			<td class='items'>" . $c[$i][1] . "</td>
			<td class='items'>
			<form action=\"showcart.php?a=update\" method=\"POST\">
				<input type='hidden' name='p' value='". $c[$i][0] ."' />
				<input type='hidden' name='c' value='". $c[$i][1] ."' />
				<input type='text' name='q' size='3' value='" . $c[$i][2] . "' /> 
				<input type='submit' value='update' />
			</form>
			</td>
			<td class='items'>$". $c[$i][3] ."</td>
			<td class='items'>
			<form action=\"showcart.php?a=delete\" method=\"POST\">
				<input type='hidden' name='p' value='". $c[$i][0] ."' />
				<input type='hidden' name='c' value='". $c[$i][1] ."' />
				<input type='submit' value='remove item' class='delitem_btn' />
			</form>
			</td>
		</tr>";
	}

}	
	$cart_tbl .= "
		<tr>
			<td colspan='3' class='empty'>&nbsp;</td><td class='total'>". $cart->cart_total() ."</td><td class='items'><form action=\"showcart.php?a=clear\" method=\"post\"><input type=\"submit\" value=\"clear cart\" /></form></td>
		</tr>";
	$cart_tbl .= "
		</tr>
	</table>
	<!-- End Cart Display -->\n\n\n";
/////////////////////////////////////////////////							
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Your Shopping Cart</title>
	
	<style type="text/css" media="all">
		/* Some Clunky Style Rules To Be Highly Modified */
		#cart 
				 {width:auto;}
		
		#cart th 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#999;
				  color:#000;
				  border:1px solid #ccc;
				  text-transform:uppercase;}
		
		#cart .items 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#ccc;
				  color:#333;
				  border:1px solid #999;
				  vertical-align:middle;}
				  
		#cart .empty 
				 {background-color:#fff;}
		
		#cart .total 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#999;
				  color:#333;
				  border:1px solid #fff;}
				  
		#minicart 
				  {width:auto; 
				   border:1px solid #333;
				   padding:0px;
				   background-color:#ccc;}		
				   
		.minihead 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#999;
				  color:#000;
				  border:1px solid #ccc;
				  text-transform:uppercase;
				  text-align:center;}
		
		.minicontent 
				 {padding:5px 15px 5px 15px; 
				  font-family:Arial, Helvetica, sans-serif;
				  background-color:#ccc;
				  color:#333;
				  border:1px solid #999;
				  text-align:center;}  
	</style>

</head>

<body>

<? 
echo $cart_tbl; 
$t = $cart->cart_total();
$qty = $cart->quant_items();
	
echo "
	<p>
	<!-- Begin Mini Cart Display -->
	<!-- Note: Put this mini cart on all ecommerce pages except showcart.php. -->
	<!-- It is just here for demonstration and testing purposes -->
	<table summary='Your shopping cart' id='minicart'>
		<tr>
			<td class='minihead' colspan='2'>Your Shopping bag</td>
		</tr>
		<tr class='minicontent'>
			<td>" . $qty . " items</td><td>" . $t . "</td>
		</tr>
	</table>
	<!-- End Mini Cart Display -->
	</p>\n\n\n";
?>

	<br />
	<br />
	<!-- Sample Cart Interaction Buttons (Add item buttons would normally be on product pages) -->
	<p><form action="showcart.php?a=add" method="post">
	<input type="submit" value="add widget" />
	<input type="hidden" value="widget" name="p" />
	<input type="hidden" value="green" name="c" />
	<input type="hidden" value="1" name="q" /></form></p>
	<p><form action="showcart.php?a=add" method="post">
	<input type="submit" value="add widget 2" />
	<input type="hidden" value="widget2" name="p" />
	<input type="hidden" value="red" name="c" />
	<input type="hidden" value="1" name="q" /></form></p>
	<!-- End Sample Cart Interaction Buttons -->


</body>
</html>

now here is the code for my form

Code: Select all

<form name="cart_quantity" method="POST" style="display:inline; margin:0px;" autocomplete = "off">
<div class="bttns"><input type="image" src="i/update_quote.gif" border="0" alt="add" ></div>

<a name="quote"></a><div class="h2brdr"><h2>Current Quote</h2></div>

  <table width="600" height="20" border="0" align="center" cellspacing="0">
  <tr><td align="center" width="107" class="plinky"><strong>Quantity:</strong></td>
<td class="plinky" align="center" width="85"><input name="prod_quantity" value="0" size="2" maxlength="4" class="quanw" type="text">
 </td>
  <td width="402" align="left" class="plinky"><strong>Enter Model Number:</strong>
  <input name="model" style="width: 50px;" size="20" maxlength="20" class="quanw" value="" type="text">
  </td>
</tr></table>
    </form>
I guess if either of these will do what I need somehow, Im not realy sure on how to incorporate them into my existing form. Or would a sessions script be better? I have been doing experimentation between these scripts but so far with no luck. Thank you all so very much for the help. I know im probably a pain in the a** by now.lol

Posted: Sat Dec 30, 2006 3:48 pm
by crazy8
Does anyone have anything that would help me at all?

Thank You

Posted: Sat Dec 30, 2006 4:27 pm
by RobertGonzalez
Post your current form. Lets see if we can help you incorporate this code into it.

Posted: Sat Dec 30, 2006 4:29 pm
by timvw
crazy8 wrote:Now I dont know if either of these will work for me or not, and im not sure how to edit either of these either so if either of these will work for what I need some help on how to edit the scripts/form would be a huge help.
So the questions you (we) have are:

- What do you want to do?
- What does the script do? If there are specific parts, we could try to explain them.. but i think it's useless to explain the working of a complete script...
- Does the script meet your requirements?
- Which parts need to be changed? How will you change them? Again, if you have specific problems we'll be happy to help you.. If you want to to write a complete application, you'll have to reread the sticky threads...

Posted: Tue Jan 02, 2007 9:19 am
by crazy8
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is the form part of my code for the page.

[syntax="html"]<form name="cart_quantity" action="famtest.php?name=test" method="POST" style="display:inline; margin:0px;" autocomplete = "off">
<div class="bttns"><input type="image" src="i/update_quote.gif" border="0" alt="add" ></div>

<a name="quote"></a><div class="h2brdr"><h2>Current Quote</h2></div>
	
  <table width="600" height="20" border="0" align="center" cellspacing="0">
  <tr><td align="center" width="107" class="plinky"><strong>Quantity:</strong></td>
<td class="plinky" align="center" width="85"><input name="prod_quantity" value="0" size="2" maxlength="4" class="quanw" type="text">
 </td>
  <td width="402" align="left" class="plinky"><strong>Enter Model Number:</strong>
  <input name="model" style="width: 50px;" size="20" maxlength="20" class="quanw" value="" type="text">
  </td>
</tr></table>
    </form>
I have realised that currently there are about 5 or so scripts on the site now that are involved with the form. If it helps for either of you to in how you can help me, go to http://www.winsted.com/winstedSQL/famil ... ?name=lcd3 and play with the form on the bottom of the page. I need to imitate how that form works down to a "T" if possable with some slight differences. One of which is that mine will have 2 text input boxes as you see in the code. One named "prod_quantity" and the other "model".
How I need this to work is for a customer to be enter in a model (product number) and a quantity for that item. Once "enter" or the "update quote" button is hit a connection is made to the db and the model number is displayed as a link to an image (I have this taken care of), a "short description" is also displayed (also taken care of already) and the amount that was entered by customer should be echoed but when echoed should have itself displayed in a new text box incase further editing may need to be done to the order.To see what I mean by all of this in graphical form goto http://www.millermachinecompany.com/win ... l/test.htm
As you will see in this link there are for buttons. If possable also after the first time a customer hits "enter" or the "update quote" button I would like the other 3 buttons to pop up just like the original form on the site does now.


Well I think that says it all right there hopefully you guys have an idea what im looking for. Again if either of you have a question or need something from me to better help me out feel free to ask. Thank you both so much.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jan 02, 2007 10:36 am
by RobertGonzalez
If you look really close at the way that form processes, you can almost see the code behind it.

What I would do is build the form with default values of 0 (zero) for each item. Then, when the form is submitted (not refreshed, but actually sent) I would check to make sure that at least one of the fields is not 0. If it is, then someone passed a default form and you should error. If there is at least one field set to a number greater than 0, process the form accordingly. That handles the actual processing of the quote request. Now to the updating that you are looking to do...

If there is a fresh page load (or if all the values of the quantities are 0) then you really do not need to show any current status form. But if there is a numeric value greater than 0, then you need to show all items that meet that criteria (almost as though you build an array of those items whose quantities are a numeric greater than 0 Image). You keep showing the status until the form is submitted.

I know this is stripped down logic, but I think it should do what you are looking to do. You should have all the items in your database, which, when displayed should be coming from an array already. You have default values (which must be overridden by the passed value if it is numeric greater than 0) so in reality you have everything you need to get this thing moving right now.

Posted: Tue Jan 02, 2007 11:51 am
by crazy8
Yea all the database stuff is already there and ready. As far as the logic goes, thats totaly understandable. One of my classes at college right now is advance programing with VB.NET. I only mention this because I understand logicv and how to create it when looking at an application. The trick is putting it into code. Now on that not if this was something I needed to do in VB .NET it would take time but I would have had it done long ago but understanding SESSIONS and trying to translate this logic to PHP is still a bit foreign to me. :lol:

Posted: Tue Jan 02, 2007 12:54 pm
by RobertGonzalez
PHP Sessions are quite a bit different than ASP sessions. But I still think that this does not need session management. Unless you are using sessions for the rest of the app somewhere. But if this is something that will not be a stand alone session application piece, I wouldn't implement sessions into it unless you absolutely need to.

This is the code logic I would employ...
1. Select all necessary items into an array.
2. Check for posted form data.
3. review the posted data for numeric values greater than 0.
4. Assign all values that meet the criteria in #3 to variables.
5. Assign all non-posted values a default of 0.
6. Check if the form was actually posted to submit. If so submit it.
7. Check if the form was refreshed. If so, refresh it.

Posted: Tue Jan 02, 2007 2:41 pm
by crazy8
Well even though the logic seems like it will be much easier to implement then sessions, I only ask about sessions just because the form on the site now uses a sessions script and my boss isnt realy requiring it but he did request that I try to use all the original files or even slightly modified files of the original. I am still playing around and I think I may have made some progress today. So far since I havent been able to come up with any code im just trying my best to modify copies of the original scripts to see if I can get them to work with my form. So far Id have to say I think its progressing but of course with doing this comes some bugs to work out which is what I am currently doing untill (if I can) come up with working code that will do everything I need it to

Posted: Tue Jan 02, 2007 2:46 pm
by RobertGonzalez
Keep in mind also, that just because sessions are used on the page does not mean that you have to use them for this part of the app.

Posted: Tue Jan 02, 2007 2:59 pm
by crazy8
So you think that even though a customer will be "building" an order that will get sent to us to be quoted can be done other then using sessions? Im open for anything as long as I can pull it off right if it keeps me from doing sessions since I still havent grabed a full grasp on it :lol: If thats the case I will surely see what I can do with the logic and see if I can turn out some code.

Posted: Fri Jan 05, 2007 10:48 am
by crazy8
Well here is a little bit of an update for the rest of you. Also I have a few questions below to see if the rest of you might have any ideas as to how any of these remaining issues might be solved?

Well I just now figured out one of my issues. I dont know if I mentioned to you or not that no matter what I typed in I would get feedback but it would always be one certain item though it wasnt the right one. Here is what that problem was.

Original code

Code: Select all

// Get information for prod
                $query = 'SELECT model,little_desc FROM product WHERE id = '.intval($id);
                $prod = mysql_query($query) or die("Query failed");
                $desc = mysql_fetch_array($prod, MYSQL_ASSOC);
               
                $cart[$i][0] = intval($id);
                $cart[$i][1] = intval($quantity);
                $cart[$i][2] = $desc['model'];
                $cart[$i][3] = $desc['little_desc'];
                $i++;
        }
Here are the changes I had to make

Code: Select all

// Get information for prod
                $query = 'SELECT * FROM product WHERE model = '.intval($id);
                $prod = mysql_query($query) or die("Query failed");
                $desc = mysql_fetch_array($prod, MYSQL_ASSOC);
               
                $cart[$i][0] = intval($id);
                $cart[$i][1] = intval($prod_quantity);
                $cart[$i][2] = intval($model);
                $cart[$i][3] = $desc['little_desc'];
                $i++;
        }
There is one issue im having that has me kind of stumped. Im not sure if it has to do with the sessions script or one of the other scripts but maybe you know whats going on or can give me an idea.

Ok so I type in a model number and a quantity and then it gets displayed like this (nevermind the description part)

Image

Now if I go and type in another model number and quantity and hit "Enter" or the "update quote" button I get this. Which of course I want "update quote" to add to the list, not to replace things on the list.

Image

Lastly If I do the same thing but hit the "Change Quote" button I get this. Now I need "Change Quote" to be used to edit quantities in the list that the customer may want to change. Maybe this is just a matter of doing some tweaking or something, I dunno? But you can also see by hitting this button that it also displays my form again. I might also add that no matter what I still cant get more than 1 or 2 itmes or instances I should say in the list depending which button I hit. Any ideas?


Image


One more thing I was wondering is if anyone else had any ideas about the possability of this being done without sessions if its possable and if so how would I go about it?

Thank you all so much for the help

Posted: Fri Jan 05, 2007 11:09 am
by RobertGonzalez
Button handling is a bit of a different issue. For example, if your buttons are all submits, you should be checking the name of the button as an index to the POST array to make sure you are doing what the button is supposed to do.

Posted: Fri Jan 05, 2007 11:22 am
by crazy8
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Well thats the weird part. Like I have said there is so much stuff and so many different scripts going into this that it doesnt help make it simple   

Here is the only button refference in the actual form
[syntax="html"]<div class="bttns"><input type="image" src="i/update_quote.gif" border="0" alt="Update Totals" ></div>
Then in another script which I think has to do with how stuff gets aranged on the form page has a reference to the other three buttons that pop up after the customer enters the first model number and quantity and hits "update quote". Almost ALL of it is $page_out and &out_data stuff. Not sure what that is so if anyone could fill me in on that it would be one more thing I can learn[/syntax]

Code: Select all

$page_out .= '<div class="bttns">';
			$page_out .= '<a href="'.$clearcart.'"><img src="i/clear_quote.gif" border="0" alt="Clear Quote" class="multi"></a><input type="image" src="i/change_quote.gif" border="0" alt="Change Quote" name="Change"><input type="image" src="i/get_quote.gif" border="0" alt="Get Quote" name="getchange">';
			$page_out .= '</div>'."\n";
Then lastly in the sessions script you can see the $clearcart variable which is what is mentioned in the script above this.

Code: Select all

$_SESSION['prev_page'] = "ez_sess.php?name=ez_quote";
	if(isset($_GET["page"]))  {
		$_SESSION['prev_page'] .= '&page='.$_GET["page"];
	}
	
	// Clear Cart link
	$clearcart = $_SESSION['prev_page'];

	if(isset($_GET["action"]))  {
		$_SESSION['prev_page'] .= '&action=update_product';
		$clearcart .= '&action=clear';
	}
So to see this behavior with the buttons based on these bits of code is confusing to me why those buttons would do what they are doing. Unless of course there is something Im missing, which is possable since I have been working on this project for about a month now nonstop, and want to finish it more then anything so I can move on :lol:
Well let me know what you all think.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jan 05, 2007 12:05 pm
by RobertGonzalez
Can you put all of the files for this project into a zip file and upload to a place where we can download it? There is a lot of code that needs to be looked at in order to help you get out of this mess.