Question : Add price modifier from MySQL with PHP

I have a database setup where some products have different sizes which in turn have different prices. (see database pic)

What i need to do is show the available sizes on the detail page which will then add the corresponding price if selected. You'll see from the databse pic that the products have a unique code shown in the 'code' column.

This is really a follow on from a previous question which may give more information if any of this does not make sense!

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_26313087.html#a33163450

Here's the website: http://www.leylasbijoux.com/necklaces.php

I have also added the code for the detail page...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
<?php require_once('Connections/Leyla.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}



mysql_select_db($database_Leyla, $Leyla);
$query_rs_style = "SELECT * FROM ringstyle";
$rs_style = mysql_query($query_rs_style, $Leyla) or die(mysql_error());
$row_rs_style = mysql_fetch_assoc($rs_style);
$totalRows_rs_style = mysql_num_rows($rs_style);

mysql_select_db($database_Leyla, $Leyla);
$query_rs_colour = "SELECT * FROM colourNecklace";
$rs_colour = mysql_query($query_rs_colour, $Leyla) or die(mysql_error());
$row_rs_colour = mysql_fetch_assoc($rs_colour);
$totalRows_rs_colour = mysql_num_rows($rs_colour);

mysql_select_db($database_Leyla, $Leyla);
$query_rs_size = "SELECT * FROM ringsize ORDER BY `size` ASC";
$rs_size = mysql_query($query_rs_size, $Leyla) or die(mysql_error());
$row_rs_size = mysql_fetch_assoc($rs_size);
$totalRows_rs_size = mysql_num_rows($rs_size);

$colname_rs_items = "-1";
if (isset($_GET['id'])) {
  $colname_rs_items = $_GET['id'];
}
mysql_select_db($database_Leyla, $Leyla);
$query_rs_items = sprintf("SELECT id, name, `description`, price, image, type, style, colour, `size`, COALESCE(image2, 'spacer.gif') AS image2, COALESCE(image3, 'spacer.gif') AS image3, COALESCE(image4, 'spacer.gif') AS image4, relate1, relate2, relate3, relate4, code FROM products WHERE id = %s", GetSQLValueString($colname_rs_items, "int"));
$rs_items = mysql_query($query_rs_items, $Leyla) or die(mysql_error());
$row_rs_items = mysql_fetch_assoc($rs_items);
$totalRows_rs_items = mysql_num_rows($rs_items);

$sizes = $row_rs_items['size'];
$dropdownOptions = explode(",", $sizes);

$dropDown = explode(";", $row_rs_items['relate1']);

$dropDown2 = explode(";", $row_rs_items['relate2']);

$dropDown3 = explode(";", $row_rs_items['relate3']);

$dropDown4 = explode(";", $row_rs_items['relate4']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cocktail Rings, Earings, Bracelets &amp;  Jewellry</title>

<link rel="stylesheet" href="lightbox.css" type="text/css" media="screen" />
	
	<script src="js/prototype.js" type="text/javascript"></script>
	<script src="js/scriptaculous.js?load=effects,builder" type="text/javascript"></script>
	<script src="js/lightbox.js" type="text/javascript"></script>
    
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
<link href="leyla.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>
<link rel="stylesheet" type="text/css" href="RbZoom.css" />

<script type="text/javascript" src="RbZoom.js"></script>


</head>

<body onload="MM_preloadImages('uploads/<?php echo $row_rs_items['image2']; ?>','uploads/<?php echo$row_rs_items['image']; ?>','uploads/<?php echo$row_rs_items['image2']; ?>','uploads/<?php echo$row_rs_items['image3']; ?>','uploads/<?php echo$row_rs_items['image4']; ?>')">


<table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center"><img src="images/leya-logo.jpg" width="300" height="100" alt="Leya Bijoux Logo" /></td>
  </tr>
  <tr>
    <td align="center"><table width="209" height="10" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td><!-- saved from url=(0014)about:internet -->
            <div id='menu' style='display:none;'>
              <div><a href='index2.html'>HOME</a></div>
              <div><a href='jewellery.php'>RINGS</a></div>
              <div><a href='bracelets.php'>BRACELETS</a></div>
              <div><a href='earrings.php'>EAR RINGS</a></div>
              <div><a href='necklaces.php'>NECKLACES</a></div>
              <div><a href='bangles.php'>BANGLES</a></div>
              <div><a href='about.php'>ABOUT LEYLA</a></div>
              <div><a href='faqs.php'>FAQ%5C%27S</a></div>
              <div><a href='contact.php'>CONTACT</a></div>
          
          </div>
          <div id="f_source_navigation" style="position:relative; top:0px; left:0px; width:928px;">
            <!-- Don't change any part of the DIV code (Press Ctrl+Shift+F to edit) -->
            <script src='Menu/MenuScript.js' type='text/javascript'></script>
            <!-- saved from url=(0014)about:internet -->
           
            <!-- Flash Object code (EMBED tag will be generated automatically) -->
            <div id="f-source-menu" style="position:relative;" onmouseover="if(!ied){i= fdiv.style.height.indexOf('px');h=Number(fdiv.style.height.substring(0,i));fdiv.style.height=h+1+'px';}">
              <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" id="Menu" width="928" height="33" >
                <param name="movie" value="Menu/menu.swf" />
                <param name="quality" value="high" />
                <param name="wmode" value="transparent" />
                <param name="FlashVars" value="flashlet={bg_Pic_URL:'None',xml_Path:'',stretch_width_to:'928px',_TransparencyShadow:30,_removeSubDelay:500,_minSubWidth:60,_max_height:550,_removeDividers:false,showSearchField:false,searchFieldWidth:80,searchTextColor:#000000,searchFieldColor:#FFFFFF,mainSoundURL:'',clickSoundURL:'',subSoundURL:'',_mainButtonHeight:28,_menuColor:#DDDDDD,_mainFont:'Times Roman',_mainFontSize:10,_mainButtonTextColor:#5e5e5e,_mainButWidthExt:6,_mainHighlightColor:#CCCCCC,_TransparencyMain:100,_subButtonHeight:20,_subMenuColor:#FEFEFE,_subFont:'Times New Roman',_subFontSize:10,_subButtonTextColor:#333333,_subHighlightColor:#999999,_TransparencySub:100,_pos_type:'relative',_pos_centered:false,_pos_top:0,_pos_left:0,_pos_insert:'the insertion point',_file_folder:'Menu'}" />
                <noscript>
                  The <a href="http://f-source.com/">flash menu</a> provided by f-source.com. All Rights Reserved
                  </noscript>
              </object>
              <script type="text/javascript">var connected; var message="Hello!"; if (connected){ Run_f_source_menu() }else{ alert("The file MenuScript.js could not be loaded!") }</script>
            </div>
          </div></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><div>
      <table width="930" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="center"><table width="929" border="0" cellpadding="0" cellspacing="0" class="querybar">
            <tr>
              <td width="119" bgcolor="#BBCACC" class="type"><table width="109" height="18" border="0" cellpadding="0" cellspacing="0" class="type">
                <tr>
                  <td><?php echo $row_rs_items['type']; ?></td>
                </tr>
              </table></td>
              <td width="573" align="center" bgcolor="#BACACC"><form action="necklaces.php" method="get">

                <table width="580" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="518" align="center" valign="middle"><p class="black">
                    Colour 
                    <select name="colour" class="black" id="colour">
                      <option value="ANY" <?php if (!(strcmp("ANY", $row_rs_colour['colour']))) {echo "selected=\"selected\"";} ?>>All</option>
                      <?php
do {  
?>
                      <option value="<?php echo $row_rs_colour['colour']?>"<?php if (!(strcmp($row_rs_colour['colour'], $row_rs_colour['colour']))) ?>><?php echo $row_rs_colour['colour']?></option>
                      <?php
} while ($row_rs_colour = mysql_fetch_assoc($rs_colour));
  $rows = mysql_num_rows($rs_colour);
  if($rows > 0) {
      mysql_data_seek($rs_colour, 0);
	  $row_rs_colour = mysql_fetch_assoc($rs_colour);
  }
?>
                    </select>
                     Price Range
                    <select name="range" class="black" id="range">
                      <option value="0-50">Less than £50</option>
                      <option value="51-100">£51 - £100</option>
                      <option value="101-200">£101-£200</option>
                      <option value="201-300">£201 - £300</option>
                      <option value="300-99999999">£300 +</option>
                      <option value="0-999999999" selected="selected">All</option>
                    </select>
                    </p></td>
                    <td width="62" align="left" valign="middle"><span class="black">
                      <input name="submit" type="image" id="submit" src="images/search.png" alt="Add to Basket" />
                    </span></td>
                  </tr>
                </table>
              </form></td>
              <td width="237" align="right" bgcolor="#BACACC"><table width="220" border="0" cellpadding="0" cellspacing="0" class="checkoutbar">
                <tr>
                  <td width="199" align="right" class="black">Items:
                      <script language="JavaScript" src="http://www.romancart.com/cartinfo.asp?storeid=50225&amp;type=1" type="text/javascript"></script>
                    Total:
                    <script language="JavaScript" src="http://www.romancart.com/cartinfo.asp?storeid=50225&amp;type=2" type="text/javascript"></script>
                    <a href="http://www.romancart.com/cart.asp?storeid=50225" class="price">Checkout</a></td>
                  <td width="38" align="right"><img src="images/bag.png" alt="checkout" width="35" height="33" border="0" usemap="#Map4" /></td>
                </tr>
              </table>
                <map name="Map4" id="Map42">
                  <area shape="rect" coords="9,4,28,29" href="http://www.romancart.com/cart.asp?storeid=50225" />
                </map>
             </td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td align="center"><table width="928" border="0" cellpadding="7" cellspacing="0" class="gapbottom">
            <tr>
              <td bgcolor="#EBEBEB"><table width="914" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="419" valign="top" bgcolor="#FFFFFF"><table width="390" border="0" cellspacing="0" cellpadding="20">
                    <tr>
                      <td valign="top"><p><a href="uploads/<?php echo$row_rs_items['image']; ?>" class="RbZoom"><img src="uploads/<?php echo$row_rs_items['image']; ?>" alt="" name="main" width="350" height="300" border="0" id="main" /><br />
                      </a></p>
                        <span class="copyright">Rollover the image to zoom in                        </a></span></td>
                    </tr>
                  </table></td>
                  <td width="388" valign="top" bgcolor="#FFFFFF"><table width="350" border="0" cellpadding="20" cellspacing="0">
                    <tr>
                      <td width="373" valign="top"><table width="250" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td valign="top"><table width="250" height="50" border="0" cellpadding="0" cellspacing="0" class="linebottom">
                            <tr>
                              <td><p><span class="ringname"><?php echo $row_rs_items['name']; ?></span><br />
                                <br />
                                <?php echo $row_rs_items['description']; ?><br />
                                <br />
                                 Size: 
             
                                <select name="size">
	<? foreach ($dropdownOptions as $option): ?>
	<option value="<?=trim($option)?>"><?=trim($option)?></option>
	<? endforeach; ?>
</select>
                                <br />
                                <br />
                              </p></td>
                            </tr>
                          </table></td>
                        </tr>
                        <tr>
                          <td valign="top"><form action="http://www.romancart.com/cart.asp" method="post">
                            <p>
                              <input type="hidden" name="itemname" value='<?php echo $row_rs_items['name']; ?>' />
                              <input type="hidden" name="itemname2" value='<?php echo $row_rs_items['code']; ?>' />
                              <input type="hidden" name="storeid" value="50225" />
                              <input type="hidden" name="price" value="<?php echo $row_rs_items['price']; ?>" />
                              <span class="greytext">Prices will be displayed in Euro and US dollars in in your cart.</span><br />
                           
                              <span class="detailprice">£</span>
                              <span class="detailprice"><?php echo $row_rs_items['price']; ?></span><br />
                              <br />
<input type="image" src="images/sumit.gif" border="0" name="submit3" />
                            </p>
                          </form></td>
                        </tr>
                      </table></td>
                    </tr>
                  </table></td>
                  <td width="107" align="center" valign="top" bgcolor="#FFFFFF"><span class="copyright"><br />
                    Related items</span>
                    <table width="0" border="0" cellspacing="0" cellpadding="5">
                    <tr>
                      <td><a href="necklacedetail.php?id=<?php echo $dropDown[0]; ?>"><img src="uploadsmall/<?php echo $dropDown[1]; ?>" alt="" width="80" height="69" border="0" /></a></td>
                    </tr>
                    <tr>
                      <td><a href="necklacedetail.php?id=<?php echo $dropDown2[0]; ?>"><img src="uploadsmall/<?php echo $dropDown2[1]; ?>" alt="" width="80" height="69" border="0" /></a></td>
                    </tr>
                    <tr>
                      <td><a href="necklacedetail.php?id=<?php echo $dropDown3[0]; ?>"><img src="uploadsmall/<?php echo $dropDown3[1]; ?>" alt="" width="80" height="69" border="0" /></a></td>
                    </tr>
                    <tr>
                      <td><a href="necklacedetail.php?id=<?php echo $dropDown4[0]; ?>"><img src="uploadsmall/<?php echo $dropDown4[1]; ?>" alt="" width="80" height="69" border="0" /></a></td>
                    </tr>
                  </table></td>
                </tr>
              </table></td>
            </tr>
            </table></td>
        </tr>
        <tr>
          <td bgcolor="#BBCACC">&nbsp;</td>
        </tr>
      </table>
    </div></td>
  </tr>
  <tr>
    <td><table width="410" border="0" cellpadding="10" cellspacing="0">
      <tr>
        <td class="copyright"><a href="login.php" class="small">© 2010 Leylas Design</a> : Terms &amp; conditions : Delivery</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
<?php


mysql_free_result($rs_style);

mysql_free_result($rs_colour);

mysql_free_result($rs_size);

mysql_free_result($rs_items);
?>
Attachments:
 
database
database
 

Answer : Add price modifier from MySQL with PHP

Sorry - I can't read 300 lines of code but I can tell you what might work well for you.

Looks like you might want a query that selects all WHERE code = {something}.  You can count the number of results, and you can iterate over the results set to get zero, one or any number of possible answers from the query.

Once you have done that query, the results set will also contain the name, description, color, size and price, along with the id number.  You can present these options on the browser page for the client to choose.  The "id" column appears to be a unique number.  When the client selects one of those, you can use the id number to uniquely identify the product.

Does that help? ~Ray
Random Solutions  
 
programming4us programming4us