Page 1 of 1

newbie needs advice & a little help

Posted: Tue Aug 13, 2002 4:55 pm
by drharv
Ok, I just started trying to learn PHP a week ago to use in a new site I'm building. I have a page that is sort of like a custom computer builder. I am trying out ways to send the selections(processor, ram, etc.) to a handler that figures the price and updates the original page with the new price & selections.

So far, I've been writing the choices to a cookie, but I'm getting some strange results. The cookie is right when it hits the price handler page, because it is getting the right price. However, certain cookies dissappear before it returns to the original page to update.

I've tried sessions variables, but their just as random as the cookies on working. Any ideas whats wrong, or suggestions how to go about things differently?

Thanks for any help, and sorry this was a longwinded post :) .

Posted: Wed Aug 14, 2002 9:41 am
by Johnm
Please post some code for us to look at.

Direwolf

heres the code

Posted: Wed Aug 14, 2002 10:59 am
by drharv
Ok, here's the code. There's a lot of it, but it's mostly the same statments over & over. I'm not posting the main form page yet, it is huge.

Here is my first handler page, it just sets the cookies.

Code: Select all

<?php
	
	if ( isset($buy) )
	&#123;
		header("Location: confirm_buy.php");
	&#125;
	else
	&#123;
		Setcookie(chassis, $f_chassis, time() + 36000);
		Setcookie(proc, $f_proc, time() + 36000);
		Setcookie(ram, $f_ram, time() + 36000);
		Setcookie(hd1, $f_hd1, time() + 36000);
		Setcookie(hd2, $f_hd2, time() + 36000);
		Setcookie(cd1, $f_cd1, time() + 36000);
		Setcookie(cd2, $f_cd2, time() + 36000);
		Setcookie(floppy1, $f_floppy1, time() + 36000);
		Setcookie(floppy2, $f_floppy2, time() + 36000);
		Setcookie(modem, $f_modem, time() + 36000);
		Setcookie(nic, $f_nic, time() + 36000);
		Setcookie(video, $f_video, time() + 36000);
		Setcookie(monitor, $f_monitor, time() + 36000);
		Setcookie(speakers, $f_speakers, time() + 36000);
		Setcookie(os, $f_os, time() + 36000);
		Setcookie(soft1, $f_soft1, time() + 36000);
		Setcookie(soft2, $f_soft2, time() + 36000);
		Setcookie(keyboard, $f_keyboard, time() + 36000);
		Setcookie(type, $f_type, time() + 36000);
		Setcookie(type2, $f_type2, time() + 36000);
	
		header("Location: price_cookie.php");
	&#125;
?>

That forwards to this next page, where the prices are set and a total is figured using switch statements. Like I said, I get the correct total - but the cookie is gone when it gets back to the form. It's only happening with the first 4 or so cookies. Here's the second handler. :cry:

Code: Select all

<?php

	$p_chenbro = 45;
	$p_foxconn_black = 40;
	$p_ahanix = 45 + 29;
	$p_fongkai = 80;
	$p_lianli_regular = 108 + 29;
	$p_lianli_black = 113 + 29;
	$p_chftch = 70;
	$p_chftch_window = 99;
	
	$p_duron = 48;
	$p_1600 = 68;
	$p_1800 = 84;
	$p_1900 = 108;
	$p_2000 = 128;
	$p_2100 = 157;
	$p_2200 = 210;
	
	$p_20 = 70;
	$p_40 = 72;
	$p_60 = 80;
	$p_80 = 89;
	$p_120 = 140;
	
	$p_128DDR = 41;
	$p_256DDR = 66;
	$p_512DDR = (2 * $p_256DDR);
	$p_256DDR333 = 73;
	$p_512DDR333 = (2 * $p_512DDR);
	
	$p_cd = 24;
	$p_cd_black = 32;
	$p_cdr = 64;
	$p_cdr_black = 67;
	$p_dvd = 41;
	$p_dvd_black = 48;
	$p_combo = 85;
	$p_combo_black = 169;
	$p_dvdrw = 299;
	
	$p_modem = 11;
	$p_nic = 6;
	
	$p_floppy = 8;
	$p_floppy_black = 11;
	$p_zip = 67;
	
	$p_keyboard = 19;
	$p_keyboard_black = 18;
	$p_keyboard_wireless = 45;
	$p_keyboard_wireless_black = 79;
	
	$p_mon17 = 129;
	$p_mon17_black = 145;
	$p_mon17flat = 142;
	$p_mon17flat_black = 180;
	$p_mon19flat = 285;
	$p_mon19flat_black = 305;
	$p_mon22flat = 600;
	$p_mon22flat_black = 610;
	
	$p_benwin2pc = 5;
	$p_cyber2pc = 6;
	$p_creative3pc = 36;
	$p_logitech3pc = 36;
	$p_logitech5pc = 136;
	$p_klipsch6pc = 368;
	
	$p_winxphome = 90;
	$p_winxppro = 139;
	$p_win2k = 140;
	$p_norton = 15;
	$p_works = 30;
	$p_officexp = 179;
	
	$p_ati7500 = 51;
	$p_ati8500 = 110;
	$p_gf4ti4200 = 141;
	$p_gf4ti4400 = 258;
	$p_gf4ti4600 = 299;
	
	$p_extras = 83;
	$p_total = 0;
	
	switch ( $_COOKIE&#1111;'chassis'] )
	&#123;
		case ("chenbro"):
		&#123;
			$p_total += $p_chenbro;
			break;
		&#125;
		case ("foxconn_black"):
		&#123;
			$p_total += $p_foxconn_black;
			break;
		&#125;
		case ("ahanix"):
		&#123;
			$p_total += $p_ahanix;
			break;
		&#125;
		case ("fongkai"):
		&#123;
			$p_total += $p_fongkai;
			break;
		&#125;
		case ("lianli_regular"):
		&#123;
			$p_total += $p_lianli_regular;
			break;
		&#125;
		case ("lianli_black"):
		&#123;
			$p_total += $p_lianli_black;
			break;
		&#125;
		case ("chftch"):
		&#123;
			$p_total += $p_chftch;
			break;
		&#125;
		case ("chftch_window"):
		&#123;
			$p_total += $p_chftch_window;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'$proc'] )
	&#123;
		case ("duron"):
		&#123;
			$p_total += $p_duron;
			break;
		&#125;
		case ("1600"):
		&#123;
			$p_total += $p_1600;
			break;
		&#125;
		case ("1800"):
		&#123;
			$p_total += $p_1800;
			break;
		&#125;
		case ("1900"):
		&#123;
			$p_total += $p_1900;
			break;
		&#125;
		case ("2000"):
		&#123;
			$p_total += $p_2000;
			break;
		&#125;
		case ("2100"):
		&#123;
			$p_total += $p_2100;
			break;
		&#125;
		case ("2200"):
		&#123;
			$p_total += $p_2200;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'hd1'] )
	&#123;
		case ("20"):
		&#123;
			$p_total += $p_20;
			break;
		&#125;
		case ("40"):
		&#123;
			$p_total += $p_40;
			break;
		&#125;
		case ("60"):
		&#123;
			$p_total += $p_60;
			break;
		&#125;
		case ("80"):
		&#123;
			$p_total += $p_80;
			break;
		&#125;
		case ("120"):
		&#123;
			$p_total += $p_120;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'hd2'] )
	&#123;
		case ("20"):
		&#123;
			$p_total += $p_20;
			break;
		&#125;
		case ("40"):
		&#123;
			$p_total += $p_40;
			break;
		&#125;
		case ("60"):
		&#123;
			$p_total += $p_60;
			break;
		&#125;
		case ("80"):
		&#123;
			$p_total += $p_80;
			break;
		&#125;
		case ("120"):
		&#123;
			$p_total += $p_120;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'ram'] )
	&#123;
		case ("128DDR"):
		&#123;
			$p_total += $p_128DDR;
			break;
		&#125;
		case ("256DDR"):
		&#123;
			$p_total += $p_256DDR;
			break;
		&#125;
		case ("512DDR"):
		&#123;
			$p_total += $p_512DDR;
			break;
		&#125;
		case ("256DDR333"):
		&#123;
			$p_total += $p_256DDR333;
			break;
		&#125;
		case ("512DDR333"):
		&#123;
			$p_total += $p_512DDR333;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'cd1'] )
	&#123;
		case ("cd"):
		&#123;
			$p_total += $p_cd;
			break;
		&#125;
		case ("cd_black"):
		&#123;
			$p_total += $p_cd_black;
			break;
		&#125;
		case ("cdr"):
		&#123;
			$p_total += $p_cdr;
			break;
		&#125;
		case ("cdr_black"):
		&#123;
			$p_total += $p_cdr_black;
			break;
		&#125;
		case ("dvd"):
		&#123;
			$p_total += $p_dvd;
			break;
		&#125;
		case ("dvd_black"):
		&#123;
			$p_total += $p_dvd_black;
			break;
		&#125;
		case ("combo"):
		&#123;
			$p_total += $p_combo;
			break;
		&#125;
		case ("combo_black"):
		&#123;
			$p_total += $p_combo_black;
			break;
		&#125;
		case ("dvdrw"):
		&#123;
			$p_total += $p_dvdrw;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'cd2'] )
	&#123;
		case ("cd"):
		&#123;
			$p_total += $p_cd;
			break;
		&#125;
		case ("cd_black"):
		&#123;
			$p_total += $p_cd_black;
			break;
		&#125;
		case ("cdr"):
		&#123;
			$p_total += $p_cdr;
			break;
		&#125;
		case ("cdr_black"):
		&#123;
			$p_total += $p_cdr_black;
			break;
		&#125;
		case ("dvd"):
		&#123;
			$p_total += $p_dvd;
			break;
		&#125;
		case ("dvd_black"):
		&#123;
			$p_total += $p_dvd_black;
			break;
		&#125;
		case ("combo"):
		&#123;
			$p_total += $p_combo;
			break;
		&#125;
		case ("combo_black"):
		&#123;
			$p_total += $p_combo_black;
			break;
		&#125;
		case ("dvdrw"):
		&#123;
			$p_total += $p_dvdrw;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'floppy1'] )
	&#123;
		case ("floppy"):
		&#123;
			$p_total += $p_floppy;
			break;
		&#125;
		case ("floppy_black"):
		&#123;
			$p_total += $p_floppy_black;
			break;
		&#125;
		case ("zip"):
		&#123;
			$p_total += $p_zip;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'floppy2'] )
	&#123;
		case ("floppy"):
		&#123;
			$p_total += $p_floppy;
			break;
		&#125;
		case ("floppy_black"):
		&#123;
			$p_total += $p_floppy_black;
			break;
		&#125;
		case ("zip"):
		&#123;
			$p_total += $p_zip;
			break;
		&#125;
	&#125;
	
	if ( $_COOKIE&#1111;'modem'] == "modem" )
	&#123;
		$p_total += $p_modem;
	&#125;
	
	if ( $_COOKIE&#1111;'nic'] == "nic" )
	&#123;
		$p_total += $p_nic;
	&#125;
	
	switch ( $_COOKIE&#1111;'keyboard'] )
	&#123;
		case ("regular"):
		&#123;
			$p_total += $p_keyboard;
			break;
		&#125;
		case ("regular_black"):
		&#123;
			$p_total += $p_keyboard_black;
			break;
		&#125;
		case ("wireless"):
		&#123;
			$p_total += $p_keyboard_wireless;
			break;
		&#125;
		case ("wireless_black"):
		&#123;
			$p_total += $p_keyboard_wireless_black;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'monitor'] )
	&#123;
		case ("mon17"):
		&#123;
			$p_total += $p_mon17;
			break;
		&#125;
		case ("mon17_black"):
		&#123;
			$p_total += $p_mon17_black;
			break;
		&#125;
		case ("mon17flat"):
		&#123;
			$p_total += $p_mon17flat;
			break;
		&#125;
		case ("mon17flat_black"):
		&#123;
			$p_total += $p_mon17flat_black;
			break;
		&#125;
		case ("mon19flat"):
		&#123;
			$p_total += $p_mon19flat;
			break;
		&#125;
		case ("mon19flat_black"):
		&#123;
			$p_total += $p_mon19flat_black;
			break;
		&#125;
		case ("mon22flat"):
		&#123;
			$p_total += $p_mon22flat;
			break;
		&#125;
		case ("mon22flat_black"):
		&#123;
			$p_total += $p_mon22flat_black;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'speakers'] )
	&#123;
		case ("benwin2pc"):
		&#123;
			$p_total += $p_benwin2pc;
			break;
		&#125;
		case ("cyber2pc"):
		&#123;
			$p_total += $p_cyber2pc;
			break;
		&#125;
		case ("creative3pc"):
		&#123;
			$p_total += $p_creative3pc;
			break;
		&#125;
		case ("logitech3pc"):
		&#123;
			$p_total += $p_logitech3pc;
			break;
		&#125;
		case ("logitech5pc"):
		&#123;
			$p_total += $p_logitech5pc;
			break;
		&#125;
		case ("klipsch6pc"):
		&#123;
			$p_total += $p_klipsch6pc;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'os'] )
	&#123;
		case ("windowsxphome"):
		&#123;
			$p_total += $p_winxphome;
			break;
		&#125;
		case ("windowsxppro"):
		&#123;
			$p_total += $p_winxppro;
			break;
		&#125;
		case ("windows2k"):
		&#123;
			$p_total += $p_win2k;
			break;
		&#125;
	&#125;

	switch ( $_COOKIE&#1111;'soft1'] )
	&#123;
		case ("norton"):
		&#123;
			$p_total += $p_norton;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'soft2'] )
	&#123;
		case ("works"):
		&#123;
			$p_total += $p_works;
			break;
		&#125;
		case ("officexp"):
		&#123;
			$p_total += $p_officexp;
			break;
		&#125;
	&#125;
	
	switch ( $_COOKIE&#1111;'video'] )
	&#123;
		case ("ati7500"):
		&#123;
			$p_total += $p_ati7500;
			break;
		&#125;
		case ("ati8500"):
		&#123;
			$p_total += $p_ati8500;
			break;
		&#125;
		case ("gf4ti4200"):
		&#123;
			$p_total += $p_gf4ti4200;
			break;
		&#125;
		case ("gf4ti4400"):
		&#123;
			$p_total += $p_gf4ti4400;
			break;
		&#125;
		case ("gf4ti4600"):
		&#123;
			$p_total += $p_gf4ti4600;
			break;
		&#125;
	&#125;

	$p_total += $p_extras;
	$p_total *= 1.3;
	
	Setcookie(total, $p_total, time() + 36000);
	
	header("Location: custom_builder_power.php");
?>
Thanks again for any help.

Posted: Wed Aug 14, 2002 11:40 am
by RandomEngy
You can only have one cookie at a time, (previous cookie is overwritten when you set one) so instead, serialize all your data from an associative array to put it in the cookie:

Code: Select all

$info_array = array(
  "chassis" => $f_chassis,
  "proc" => $f_proc,
  // the other stuff
  "type2" => $f_type2
);

setcookie("comp_info",serialize($info_array),time()+36000);
And to get it out:

Code: Select all

$cookie_array = unserialize($_COOKIE&#1111;'comp_info']);

echo "The chassis is: ".$cookie_array&#1111;'chassis'];

Posted: Wed Aug 14, 2002 11:47 am
by drharv
Cool, I'll try that. Thank you very much.

Posted: Wed Aug 14, 2002 1:45 pm
by drharv
Ok, I moved them to an array, but once it gets to the second page it is erasing them. Here's what I do at the end of the page now ( after figuring the total).

Code: Select all

$parts&#1111;'total'] = $p_total;
	
Setcookie($info, serialize($parts), time() + 36000);
I created the 'total' on the first page and set it to 0. So now that I have the actual, I need to put it into the cookie. Instead of this reloading the whole array into the cookie, it just loads the 'total' element, and forgets all of the others. Any ideas? :(


P.S. I checked the cookie after the first page and it is working, so I know it is dissapearing here.

Posted: Wed Aug 14, 2002 2:08 pm
by MattF
$parts = unserialise($_COOKIE[$info]);
$parts['total'] = $p_total;
Setcookie($info, serialize($parts), time() + 36000);