Jump to content

[SOLVED] help with a simple web-store.


devdavad

Recommended Posts

I'm building a simple web-store and I need some help. I have a page where a client can check different training classes they want. Each checkbox looks like this

<p>Class one<br />Date of event: 2008-07-17<br />Price: $3992<input type="checkbox" name="1" /></p>

which is generated by

"<p>" . $row['course'] . "<br />Date of event: " . $row['date'] . "<br />Price: $" . $row['cost'] . 
"<input type=\"checkbox\" name=\"" . $row['ID'] . "\" /></p>"

and then it sends it to another page where they can confirm the order. I need some help with the confirm page that can spit out some intelligible answers (like what classes they clicked on, subtotal, total, etc. Here's an example of what the training table looks like

ID tinyint(3) divID varchar(20) course varchar(40) date date cost varchar(40)

Currently I'm completely lost on the confirm order page but if I could get a start I'm sure I could figure out the rest (like totalling the courses selected and submitting them). Thank you for your time / looking at this post ;D.

Link to comment
https://forums.phpfreaks.com/topic/115731-solved-help-with-a-simple-web-store/
Share on other sites

Uh??? We have no idea of your code, or anything specifically you want. I presume you transfer all you information through either POST or a session or whatever. Do you have existing code you want this basing upong?

 

Upon second looks at your code, your input boxes have no names, which would make it rather problematic to track the values of it to do stuff with. Use the ID as the name, transfer the info through post, and away you go with your code.

I guess the problem is I'm having trouble telling php to look at the $_POST array (which could look like Array ( [1] => on [3] => on ) for example meaning class one and three have been selected) and telling it too look up the course name for ID 1 and ID 3 and printing them to the screen. Currently all I have is

	foreach ($_POST as $value) {
	if ($value == on) {
		print $value . " ";
	}
	else {
		print $value . " ";
	}
}

which will just print on on for the array Array ( [1] => on [3] => on )

Here's what you do.

 

You name all of the checkboxes something like checked[] with the [] at the end, then you make the value field of the checkbox the ID of what you want.  Then, $_POST['checked'] will be a multi-dimensional array containing all of the values that were checked.

Here's what you do.

 

You name all of the checkboxes something like checked[] with the [] at the end, then you make the value field of the checkbox the ID of what you want.  Then, $_POST['checked'] will be a multi-dimensional array containing all of the values that were checked.

That worked pretty well. Thanks DarkWater. From that my older code returned course ID numbers instead of just on. Now I just have to figure out the SQL queries to have PHP look up the corresponding course names for each id.

Show me the database setup and I'll write a quick query.  Sounds like a simple join.

Thanks a lot man. Here's what phpmyadmin does for exporting the training table.

CREATE TABLE IF NOT EXISTS `training` (
  `ID` tinyint(3) NOT NULL,
  `divID` varchar(20) NOT NULL,
  `course` varchar(40) NOT NULL,
  `date` date NOT NULL,
  `cost` varchar(40) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `training`
--

INSERT INTO `training` (`ID`, `divID`, `course`, `date`, `cost`) VALUES
(1, '', 'Class one', '2008-07-17', '3992'),
(2, '', 'Class two', '2008-07-27', '38287'),
(3, '', 'Class three', '2008-07-21', '2347'),
(4, '', 'Class four', '2008-07-19', '234324');

PROGRESS!

Woo almost there.

	echo "You're order: ";
$result = mysql_query("SELECT ID, course FROM training GROUP BY ID");
$row = mysql_fetch_row($result);
foreach ($_POST[checked] as $value) {
	#query the ID row and then get the course names
	print "<font color=\"red\">" . $value . " </font>";
	print $row[1] . " ";
}

Close but no cigar. Prints the checkbox number that was selected in red and then just writes class one next to it. Is it right to use mysql_fetch_row this way?

	print "You're order:<br />";
foreach ($_POST[checked] as $value) {
	#query the ID row and then get the course names
	$result = mysql_query("SELECT ID, course FROM training WHERE ID = \"$value\"");
	$row = mysql_fetch_row($result);
	print $row[1] . "<br />";
}
print "Which totals to:<br />";
$total = 0;
foreach ($_POST[checked] as $value) {
	$result = mysql_query("SELECT ID, cost FROM training WHERE ID = \"$value\"");
	$row = mysql_fetch_row($result);
	$total += $row[1];
}
print "$" . $total;

Topic solved. Thanks everybody!

;D ;D :D.......... :-*

Archived

This topic is now archived and is closed to further replies.

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