Elit3d Posted September 5, 2014 Share Posted September 5, 2014 So currently I have 2 files that pretty much does the same thing but connects to 2 different tables. I sort of want to merge it together to save file space and make everything more efficient: caseprice.php <?php $q = intval($_GET['q']); $con = mysqli_connect('localhost','root','','test'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"compcase"); $sql="SELECT * FROM compcase WHERE id = '".$q."'"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['case_price'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> caselightprice.php <?php $q = intval($_GET['q']); $con = mysqli_connect('localhost','root','','test'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"complight"); $sql="SELECT * FROM complight WHERE id = '".$q."'"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['caselight_price'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> also how can I use all this to display a sub total on my page when the radio buttons are pressed? I have it so the price shows I just cant seem to add them both up: index.php <script> //CASE PRICE// function casePrice(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","caseprice.php?q="+str,true); xmlhttp.send(); } //CASE PRICE// //CASE LIGHT PRICE// function caselightPrice(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","caselightprice.php?q="+str,true); xmlhttp.send(); } //CASE LIGHT PRICE// </script> <Input type = 'Radio' Name ='compcase' onchange="casePrice(this.value)" value= '1' />NZXT Phantom Enthusiast USB3.0 Full Tower Case - White <br /> <Input type = 'Radio' Name ='compcase' onchange="casePrice(this.value)" value= '2' />Corsair Obsidian 750D Large Tower Case Black <br /> <Input type = 'Radio' Name ='compcase' onchange="casePrice(this.value)" value= '3' />Cooler Master CM Storm Trooper Black Full Tower Gaming Case <br /><br /> <Input type = 'Radio' Name ='caselight' onchange="caselightPrice(this.value)" value= '1' />Red<br /> <Input type = 'Radio' Name ='caselight' onchange="caselightPrice(this.value)" value= '2' />Green <br /><br /> <div id="txtHint"><b>Processor listed here</b></div> <div id="txtTest"><b>Processor listed here</b></div> as you can see, the price is displaying separately. Can I somehow calculate it as a subtotal instead of separate totals? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/290870-possible-to-merge-these-and-also-can-this-code-display-a-subtotal-if-so-how/ Share on other sites More sharing options...
mac_gyver Posted September 5, 2014 Share Posted September 5, 2014 as to merging the code. in general, your application/web-site should have only ONE database. the two tables you have appear to be for different categories/quantities of items. you should only have one items table that holds the information for all your items/products. and in fact, your file names and functions names (in the javascript) shouldn't be specific to the type of information that's being displayed. it's all just item/product information. as to your sub-total question, that's a javascript coding question and should be posted separately in the javascript forum section. Quote Link to comment https://forums.phpfreaks.com/topic/290870-possible-to-merge-these-and-also-can-this-code-display-a-subtotal-if-so-how/#findComment-1490045 Share on other sites More sharing options...
ginerjm Posted September 5, 2014 Share Posted September 5, 2014 Minor point but significant. Do Not Indent your php start tags. That means you are outputting chars to your client which will interrupt any headers or cookies that you wish to send later. Then you'll be looking all over for where you generated output that you didn't intend to. 2 Quote Link to comment https://forums.phpfreaks.com/topic/290870-possible-to-merge-these-and-also-can-this-code-display-a-subtotal-if-so-how/#findComment-1490046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.