updating dynamically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aragon127
Forum Newbie
Posts: 3
Joined: Tue Sep 24, 2002 1:38 pm

updating dynamically

Post by aragon127 »

Here's my problem. I'm using a set of drop down boxes to configure a product for sale. These attributes are stored in a database and PHP is used to create the drop down boxes(code pasted below). Problem I'm have is that I need to show the new price. With PHP I can only do this with a refresh, but when I refresh, the selections made are wiped out. Is there anyway to update a new price, possibly with a combonation javascrip/php? I was thinking possibly a button linked to some javascript which would recalculate the price and print it out. IS that possible at all? Thanks.

<?php
if ($products_attributes == '1') {
$products_options_name = tep_db_query("select distinct popt.products_options_id, popt.products_options_name from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . $HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . $languages_id . "'");
echo '<b>' . TEXT_PRODUCT_OPTIONS . '</b><br>';
echo '<table border="0" cellpading="0" cellspacing"0">';
while ($products_options_name_values = tep_db_fetch_array($products_options_name)) {
$selected = 0;
$products_options = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . $HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . $products_options_name_values['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . $languages_id . "'");

//heres an attempted change
if ($products_options_name_values['products_options_name'] == "Processor") { print ("Processor goes here");}
//end of attempt

echo '<tr><td class="main">' . $products_options_name_values['products_options_name'] . ': </td><td>' . "\n" . '<select name ="id[' . $products_options_name_values['products_options_id'] . ']">' . "\n";
while ($products_options_values = tep_db_fetch_array($products_options)) {
echo "\n" . '<option name="' . $products_options_name_values['products_options_name'] . '" value="' . $products_options_values['products_options_values_id'] . '"';
if ( ($products_options_values['options_values_price'] == 0 && $selected == 0) || ($cart->contents[$HTTP_GET_VARS['products_id']]['attributes'][$products_options_name_values['products_options_id']] == $products_options_values['products_options_values_id'])) {
$selected = 1;
echo ' SELECTED';
}
echo '>' . $products_options_values['products_options_values_name'];
if ($products_options_values['options_values_price'] != '0') {
echo ' (' . $products_options_values['price_prefix'] . $currencies->format($products_options_values['options_values_price']) .') ';
}
echo '</option>';
};
echo '</select></td></tr>';
}
echo '</table>';
}
?>
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You could use client-side Javascript, or server side processing to repopulate the selections.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Javascript to the rescue!

Yup, this is a javascript thing.

If you have alot of select fields, i'd reccomend doing the refresh though. Like dell (i think) does when you're building a computer on their site.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

here is a little script that I use to keep the values of drop boxes if there is an error or the page refreshes...

Code: Select all

&lt;?php
// FUNCTION FOR CREATING SELECT BOXES AND KEEPING THEIR VALUE FOR DISPLAY

function create_select ( $Sname, $table, $class, $any = "",$av, $cid, $cname, $select_id, $order) { 
# $Sname is the name to be applied to the select control. 
# $table is the name of the table to query. 
# NOTE: This function assumes you are using a lookup table that has 2 fields (id,name) or you only name those 2 fields in your sql statement. 
# $any 0=Without the "Any" option, 1=With the "Any" option, this is a NULL value. 
# $curr_id is used to set the "SELECTED" flag on the select control. 

$result = query_db("SELECT * FROM $table"); 


$html = ""; //variable holding all the html to be printed
$html.="&lt;select name="$Sname" class="$class"&gt;\n"; 
if ($any != "") { 
$html.="&lt;option value="$av"&gt;$any\n"; 
} 
while ($row = mysql_fetch_array($result)): 
$id = $row&#1111;"$cid"]; 
$name = $row&#1111;"$cname"]; 

if ($id == $select_id) { 
$html.="&lt;option SELECTED value="$id"&gt;$name\n"; 

} else { 

$html.="&lt;option value="$id"&gt;$name\n"; 
} 
endwhile; 
$html.="&lt;/select&gt;\n"; 

return $html;
}// END FUNCTION
?&gt;

and here is how you call the function:

Code: Select all

&lt;?php
echo create_select("f_state","states","boxform","select a state","","states_id","states_desc",$_POST&#1111;'f_state'],"2");
?&gt;
aragon127
Forum Newbie
Posts: 3
Joined: Tue Sep 24, 2002 1:38 pm

Post by aragon127 »

wow, 3 quality replies in just a few minutes. Thanks for all the info. I think I can make it work with what you guys gave me, if not, I'll post again :lol:
Post Reply