Jump to content

Displaying options user has selected


thomashw

Recommended Posts

I have a form on one page, and on the next page after the user has submitted the form, it displays what the user selected in the form on the previous page. These pages with the forms have specific ID's, and I've been using that with sessions to display the options they've selected, except when the user goes back and chooses something different, it doesn't display both of what they've selected, but just the latest one.

 

For example - in the drop down list they select the number '16' and click submit. On the next page it shows they've selected '16'. They then navigate through the site to the same page, and select '20' instead and click submit. Now on the next page, both of them are '20' instead of one '16' and one '20.

 

What's a good way to make sure it displays what they've selected properly?

Link to comment
Share on other sites

As an example, i'd do it like this:

 

<?php
session_start();
if(!isset($_SESSION['selected'])){
$_SESSION['selected'] = array();
}
if(isset($_POST['submit'])){
$_SESSION['selected'] = $_POST['numbers'];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST" />
<select name="numbers[]" multiple="multiple" size="5" />
<?php
for($x=1;$x<=20;$x++){
if(in_array($x,$_SESSION['selected'])){
	$selected = ' selected = "selected" ';
}else{
	$selected = '';
}
echo '<option value="'.$x.'"'.$selected.'>'.$x."</option>\n";
}
?>
</select>
<br />
<input type="submit" name="submit" value="Confirm" />
</form>

Link to comment
Share on other sites

Here's the code that does the work when the URL goes to this page with action=add at the end. The ordertemp_option is where I'm storing the different options the person selects. As you can see the other two things being inserted are the session id and the product id.

 

Since the session and product id are the only two things being inserted (besides the options selected,) if a user goes back and selects another choice of options for the same thing, the options are updated for all products under that session id and product id.

 

I'm hoping for a way to be able to call the options the user has selected if they go back and make a new selection, even if they have the same product and session id.

 

<?
session_start();
ini_set("include_path","/home/tunerspe/public_html/v2/v3/include/");
include("dbinfo.php");
if(isset($_POST['qty'])) {
$qty = $_POST['qty'];
}
if(isset($_POST['product_id'])) {
$product_id = $_POST['product_id'];
}
if(isset($_POST['option'])) {
$option = $_POST['option'];
if(isset($option)) {
$optionsselect = implode(" <br />   ", $option);
}}
if(isset($_POST['modified_hidden'])) {
$modified_hidden = $_POST['modified_hidden'];
}
if(isset($_POST['modified_quan'])) {
$modified_quan = $_POST['modified_quan'];
}
$sess = session_id();
$action = $_REQUEST['action'];


switch ($action) {
case "add":
$query = "INSERT INTO ordertemp (
		ordertemp_sess,
		ordertemp_quan,
		ordertemp_product_id) 
		VALUES ('$sess','$qty','$product_id')";
$query1 = "INSERT INTO ordertemp_option (
		ordertemp_sess,
		ordertemp_option_name, 
		ordertemp_product_id) 
		VALUES ('$sess','$optionsselect','$product_id')";

 

 

Here's how it's actually shown on the page:

 

while($row = mysql_fetch_array($results)) {
echo "<tr>\n";
extract($row);
$prod = "SELECT * FROM feature_product, ordertemp, ordertemp_option, feature_item 
		WHERE feature_product_id={$ordertemp_product_id} 
		AND ordertemp.ordertemp_product_id=feature_product_id
		AND ordertemp.ordertemp_sess = '$sessid'
		AND ordertemp.ordertemp_sess=ordertemp_option.ordertemp_sess 
		AND ordertemp_option.ordertemp_product_id=ordertemp.ordertemp_product_id";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2) or die(mysql_error());
extract($prod3);
echo "<td>";
echo "\n<form method=\"POST\" action=\"modcart.php?action=change\">
<input type=\"hidden\" name=\"modified_hidden\" value=\"$ordertemp_hidden\">";
echo "\n<input type=\"checkbox\" name=\"remove\" value=\"on\" onclick=\"submit();\">\n</form>\n</td>";
echo "<td>\n";
echo "<a href=\"prod.php?product_id=" . $feature_product_id . "\">";
echo $feature_product_sku;
echo "</a>\n</td>";
echo "\n<td class=\"alignleft\">";
echo "<a href=\"prod.php?product_id=" . $feature_product_id . "\">";
echo '<span class="bold">' . $feature_product_name . '</span>';
echo "</a>\n<br />\n";
echo "<span class=\"italic\">Options:</span> <br />";
echo '   ' . $ordertemp_option_name;
echo "</td>";
echo "\n<td align=\"right\">";
echo '&#36;' . $feature_product_price;
echo "\n</td>";
echo "\n<td>";
echo "\n<form method=\"POST\" action=\"modcart.php?action=change\">\n
<input type=\"hidden\" name=\"modified_hidden\" value=\"$ordertemp_hidden\">";
echo "\n<input type=\"text\" name=\"modified_quan\" size=\"1\" value=\"$ordertemp_quan\">";
echo "\n</td>";
echo "\n<td align=\"right\">";
$extprice = $feature_product_price * $ordertemp_quan;
echo '&#36;' . $extprice;
echo "\n</td>";
echo "\n</tr>\n";

Link to comment
Share on other sites

For this part, I'm just using the session id to differentiate between people browsing the site so it only shows things they've selected. Everything is inserted into my database and then called back with their session id.

 

Thanks for the help GingerRobot - except I don't know how/what that's doing. :( Sucks helping people like me, hey? :P

Link to comment
Share on other sites

Well I meant I was using a database to store the session id to call back what the user selected - but when they select options more than once from the same page everything gets messed up. Sorry for not being clear - I was trying to not over-explain my situation as I've found when I do that people get bored of reading my posts and don't help. :P

 

The while loop displays what the user has selected, and also allows them to change the quantity of what they've selected. The submit button and </form> isn't in the while loop so the one submit button can affect the quantities of everything the user has selected, instead of just one.

Link to comment
Share on other sites

So when you said you were using sessions to track the user's selection, you really meant you were using a database?

 

Anyway, your code seems pretty confusing. As far as i can see, you're creating a form inside a while loop, which seems rather bizarre.

Yeah, I agree fully there.

 

thomashw, it doesn't suck helping people, but when you're confusing people and or asking the wrong question, then it gets very confusing as the case is here. You're not using sessions. And that code you posted is very confusing to go through.

 

Find:

$query = "INSERT INTO ordertemp (
		ordertemp_sess,
		ordertemp_quan,
		ordertemp_product_id) 
		VALUES ('$sess','$qty','$product_id')";
$query1 = "INSERT INTO ordertemp_option (
		ordertemp_sess,
		ordertemp_option_name, 
		ordertemp_product_id) 
		VALUES ('$sess','$optionsselect','$product_id')";

In that portion of the code, you're replacing the product id with the current one. You should get the product ID from the database and tack it on to the current one. You can separate them however you want, but just make sure you will need to dissect the string when you display them.

Link to comment
Share on other sites

Sorry guys - I guess I wasn't too sure how I should be asking the question. I'll try to be more clear next time. :)

 

 

I'm not sure what you mean here:

 

You should get the product ID from the database and tack it on to the current one. You can separate them however you want, but just make sure you will need to dissect the string when you display them.

 

While we're at it, do you guys think using the session id and storing the temporary information in the database is a good way to do it, or is it better to use sessions the whole way through and not use the database to recall the information that was stored under the session id?

Link to comment
Share on other sites

The while loop displays what the user has selected, and also allows them to change the quantity of what they've selected. The submit button and </form> isn't in the while loop so the one submit button can affect the quantities of everything the user has selected, instead of just one.

 

If you're using the code you posted, then the form tag is inside the loop. In fact, there's no closing brace in the code you posted at all.

Link to comment
Share on other sites

Usually when you want to save some user preferences without having a user authentication or registration system you'd use Cookies. As far as I know that's how every other site does it. If you don't want that data to persist then you can use the $_SESSION variable.

 

Using a database for temporary data seems like the wrong way to go about it to me.

Link to comment
Share on other sites

Okay, well if you're going for a new approach with using the superglobal $_SESSION, then I'm all for it. Your way is very complicated and hard to understand.

 

I agree with you there. I think it has to do with my small "repertoire" of PHP, so I'm only doing things the way I know how - which is often way more complicated than it has to be.

 

If you're using the code you posted, then the form tag is inside the loop. In fact, there's no closing brace in the code you posted at all.

The closing form tag is a tiny bit farther down the page. I didn't post it because it's just closing the table and the form.

 

Now...

 

Using sessions, if a person goes back and selects something different from the same form, what would the best way to post both things they've selected, instead of "updating" their selection?

Link to comment
Share on other sites

While we're at it, do you guys think using the session id and storing the temporary information in the database is a good way to do it, or is it better to use sessions the whole way through and not use the database to recall the information that was stored under the session id?

 

I'd probably switch to just using sessions. Since you're limited to keeping the information during the current session anyway (because you're tracking by session ID, and not a username), the database doesn't offer the advantage of keeping the information for the user.

 

Also, all you really need to track is the IDs relating to the items(products?) that the user has previously selected, so we're not storing lots of information.

Link to comment
Share on other sites

The closing form tag is a tiny bit farther down the page. I didn't post it because it's just closing the table and the form.

 

Erm, i think you misunderstand me. I'm taking about the closing brace ( } )  of the while loop. If that is further down your page, then your form is inside a while loop.

Link to comment
Share on other sites

I'd probably switch to just using sessions. Since you're limited to keeping the information during the current session anyway (because you're tracking by session ID, and not a username), the database doesn't offer the advantage of keeping the information for the user.

 

Also, all you really need to track is the IDs relating to the items(products?) that the user has previously selected, so we're not storing lots of information.

Deal.

 

The only "roadblock" I'm having is showing the options the user selects on the products page. If they go back and select new options, I'd like the forms "action" page to display both their old and new selections.

 

Erm, i think you misunderstand me. I'm taking about the closing brace ( } )  of the while loop. If that is further down your page, then your form is inside a while loop.

Ohh, I understand now. The closing brace is indeed before the </form> tag.

 

So I want the user to be able to go back to the page with the form, and be able to select new "options" and have the forms action page display both their old and new "options" that they've selected. Am I completely missing something - or is this as hard as I'm making it?

Link to comment
Share on other sites

It's all about how you want to code it.

 

Like I would just put them as strings. So if the user selects a product with ID 5, the session would say "5". Say the user then selects 7, it would say "5,7" and so on. As for quantity, I would say something like: "5|3" <- denotes product id 5 and quantity 3. So it's all about how you approach it. There isn't one way to do it and there isn't a "correct" way. There are obviously some easier approach, but I generally would just do that.

Link to comment
Share on other sites

Uh that's a method that I came up with when I was coding something for a forum in JavaScript and I was limited in signature space. Anyways, I doubt Google would have this. If you need help, you can post it here or PM me.

 

But it's not hard. You set a session variable and you can always check if it's set. If it is, then you can just append some text onto it. It's all about how you manipulate the strings.

 

When displaying, you would want to explode the commas, then for each element in that array, you can explode "|" to get the product ID and quantity.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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