/    Sign up×
Community /Pin to ProfileBookmark

Some session help

It seems now that I got that one issue fixed it some how fixed another one also. So it seems everything is working great but one thing I would like to do is I have noticed with one of the model numbers I have used for testing “53011” no longer is in the database and when the number is entered it shows nothing in the cart other then the quantity.

Is there a way for me to maybe have a window pop up with a nice message if a number is either typed incorrectly or does not exist in the DB, or perhaps a way that in that case nothing would get put into the cart?

I also have one more minor issue I have noticed. If I build a quote and I type a “0” into the text box for a certain item I may not want, it does take it out of the cart. Where my issue is, is that even though its not in the cart, if I go and hit “Get Quote” It will still be in the list. How would I go about fixing this?

Here is the code I am using.

[code=php]<form name=”cart_quantity” action=”ez_quote.php” method=”POST” style=”display:inline; margin:0px;” autocomplete = “off”>

<?
//This gives you the ability to delete the contents in the cart.
if ($_POST[‘del_x’]){
unset($_SESSION[‘prod_quantity_cart’]);
}
?>

<div class=”bttns”>
<input type=”image” src=”i/update_quote.gif” border=”0″ alt=”Update Totals”>
<A HREF=”quote_sess.php”><IMG src=”i/get_quote.gif” border=”0″></A>
<input type=”image” name=”del” src=”i/clear_quote.gif”>
</div>
<a name=”quote”></a><div class=”h2brdr”><h2>Current Quote</h2></div>

<?php
include (‘ez_db.php’);

//building our session here.
if (!empty($_POST[‘prod_quantity’]) AND !empty($_POST[‘model’])){
$_SESSION[‘prod_quantity_cart’][$_POST[‘model’]] = $_POST[‘prod_quantity’];

} elseif (!empty($_POST[‘cart_quantity’])){
// update qty in cart session after submit
for ($i=0; $i<count($_SESSION[‘prod_quantity_cart’]); $i++){
if ($_POST[‘cart_quantity’][$i] >= 1){
$_SESSION[‘prod_quantity_cart’][$_POST[‘cart_id’][$i]] = $_POST[‘cart_quantity’][$i];
} else {
$_SESSION[‘prod_quantity_cart’][$_POST[‘cart_id’][$i]] = ”;
unset($_SESSION[‘prod_quantity_cart’][$_POST[‘cart_id’][$i]]);
}
}
}
//This will connect to the database and grab the data that we need and display everything accordingly.
if (is_array($_SESSION[‘prod_quantity_cart’])){
echo “<table width=”600″ border=”0″ cellspacing=”0″ align=”center”>”;
foreach ($_SESSION[‘prod_quantity_cart’] AS $prod=>$qty){
$sql = mysql_query(“SELECT * FROM product WHERE model=’$prod'”);
$row = mysql_fetch_array($sql);
$_SESSION[‘prod_quantity’][$row[‘id’]] = $qty;
echo <<< TBL
<tr><td class=”plinky”><img src=”i/t.gif” alt=”” height=”1″ width=”52″></td>
<td width=”71″ align=”center” class=”plinky”><a href=”p.php?n={$row[‘id’]}” target=”_blank” onclick=”javascript:op(‘p.php?n={$row[‘id’]}’); return false”>{$row[‘model’]}</a></td>
<td width=”329″ align=”center”>{$row[‘little_desc’]}</td>
<td align=”center” class=”plinky”><input type=”text” name=”cart_quantity[]” value=”$qty” style=”width: 40px;” size=”5″ maxlength=”5″ class=”plinky”>
<input type=”hidden” name=”cart_id[]” value=”$prod”></td>
</tr>
TBL;
}
echo “</table>”;
}
?>

<table style=”table-layout:fixed” width=”597″ height=”20″ border=”0″ align=”center” cellspacing=”0″>

<tr>
<td align=”right” width=”105″ class=”plinky”><strong>Model #:</strong></td>
<td class=”plinky” align=”center” width=”70″><input name=”model” style=”width: 40px;” size=”20″ maxlength=”20″ class=”plinky” value=”” type=”text”></td>
<td colspan=”2″ width=”416″ align=”left” class=”plinky”><strong>Quantity:</strong>
<input name=”prod_quantity” size=”2″ maxlength=”4″ class=”plinky” type=”text” style=”width: 40px”>
</td>
</tr></table>
</form>[/code]

Any Ideas?
Thank you

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@crazy8authorMar 08.2007 — Ok I think I have decided that for the whole model number verification that maybe I wont have a window pop up but rather have just nothing happen. By nothing I mean that the quantity wont even get added to the cart. So with the code I do have how would I go about having a model number get checked against the database then either add (if model exists in db) and to add nothing to cart (if model number doesnt exist in db)?
Copy linkTweet thisAlerts:
@benifactorMar 08.2007 — something like this...

[code=php]

$query = mysql_query("select * from product where model = '$model'");
$is_model = mysql_num_rows($query);
//check to see if the model number exists
//if not, error
if (!$is_model) {
echo("not a model, try again...");
}
//ok it exists, add to the database
else {
//add product to the database here
mysql_query("INSERT INTO product (field1, field2, field3) VALUES ('$value1', '$value2', '$value3')");
}

[/code]


just an example but it should give you the gist of it.
Copy linkTweet thisAlerts:
@crazy8authorMar 08.2007 — Well that looks good but I think if I am understanding the code right is this going to add the model to the cart on my page or is it going to add something to the db? If it adds the item to the db, I need it to add the item to the cart.
Copy linkTweet thisAlerts:
@crazy8authorMar 09.2007 — I did try that and also tried it with some minor editing and I cant get it to work or atleast the way I need it to work. Any ideas how I would do everything you got but add to cart instead of the db?
Copy linkTweet thisAlerts:
@crazy8authorMar 12.2007 — Any other suggestions from anyone?
Copy linkTweet thisAlerts:
@crazy8authorMar 13.2007 — Well here is what I have done up to now. I do have the verification working for the most part. Though its not a huge deal I cant seem to get a message to pop on the screen in the case of an invalid model number. Here is the code I have as of now.
[code=php]<?php
include ('ez_db.php');

//This will validate that the model typed in by the customer exists in the db.
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM product WHERE model= '$model'"))){
if($rec['model']==$model){
//building our session here.
if (!empty($_POST['prod_quantity']) AND !empty($_POST['model'])){
$_SESSION['prod_quantity_cart'][$_POST['model']] = $_POST['prod_quantity'];
}
}
}
elseif (!empty($_POST['cart_quantity'])){
// update qty in cart session after submit
for ($i=0; $i<count($_SESSION['prod_quantity_cart']); $i++){
if ($_POST['cart_quantity'][$i] >= 1){
$_SESSION['prod_quantity_cart'][$_POST['cart_id'][$i]] = $_POST['cart_quantity'][$i];
} else {
$_SESSION['prod_quantity_cart'][$_POST['cart_id'][$i]] = '';
unset($_SESSION['prod_quantity_cart'][$_POST['cart_id'][$i]]);

}
}
}

//This will connect to the database and grab the data that we need and display everything accordingly.
if (is_array($_SESSION['prod_quantity_cart'])){
echo "<table width="600" border="0" cellspacing="0" align="center">";
foreach ($_SESSION['prod_quantity_cart'] AS $prod=>$qty){
$sql = mysql_query("SELECT * FROM product WHERE model='$prod'");
$row = mysql_fetch_array($sql);
$_SESSION['prod_quantity'][$row['id']] = $qty;
echo <<< TBL
<tr><td class="plinky"><img src="i/t.gif" alt="" height="1" width="52"></td>
<td width="71" align="center" class="plinky"><a href="p.php?n={$row['id']}" target="_blank" onclick="javascript:op('p.php?n={$row['id']}'); return false">{$row['model']}</a></td>
<td width="330" align="center">{$row['little_desc']}</td>
<td align="center" class="plinky"><input type="text" name="cart_quantity[]" value="$qty" style="width: 40px;" size="2" maxlength="6" class="plinky">
<input type="hidden" name="cart_id[]" value="$prod"></td>
</tr>
TBL;
}
echo "</table>";
}
?>[/code]

One more issue I have that I realy need solved more then anything right now. Is with my cart when I enter "0" or hit the "Get Quote" button. I will explain now in much greater detail what is going on.

Now in this issue I will be refering to two pages. Page one is the cart or the one that will load for a customer to build a quote. Page two is the page they see when they hit "Get Quote" to fill out some personal info to submit their quote.

So here is my issue.Now when I build a quote (submit a model number and quantity) I get a list of "parts" which contain model numbers, corrisponding descriptions and a quantity, but the quantity gets displayed inside a text box so that the quantity of any item can be changed if the customer decided to do so.

Ok so on with the show.As it sits now, if I build a quote then either enter a "0" for the quantity of any item in the quote (to remove it from the quote) and hit "Get Quote"(directs me to page two) the item I wanted removed from the cart will still be displayed onto page two.The same also happens if I hit "Clear Quote", only that ALL items get removed on page one but if I hit "Get Quote" they will ALL show up in the list on page two.

Any ideas to what I need to do with my code to fis this would be GREATLY appreciated.This is the number one bug im trying to fix and thus far has been eluding me for a long time.

Thank you all for your help
Copy linkTweet thisAlerts:
@crazy8authorMar 16.2007 — Any one here who can help with my issues I have posted above?
Copy linkTweet thisAlerts:
@crazy8authorMar 20.2007 — Any one at all?
×

Success!

Help @crazy8 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...