Naveed786 Posted August 6, 2020 Share Posted August 6, 2020 Dear All, I am trying to fetch the sum of a column for current date and subtract a value from the sum for currently selected record. The query i build works fine in MySql but when the same query i placed in php no result it's blank. Below is the query. <?php //Calculation for no of paxs. $query2=mysqli_query($con,"SELECT SUM(noopaxs-gattl) FROM tblsupvisitor where date(EnterDate) = CURDATE() || gst = 'INH' || gst = 'IN' GROUP BY confno"); $calc_no_paxs_for_today=mysqli_fetch_row($query2); $gattl=$calc_no_paxs_for_today['gattl']; ?> and her is the assignment to the textbox. <div class="row form-group"> <div class="col col-md-3"> <label for="textarea-input" style="color: purple" class=" form-control-label"><strong>Total No. Of Adults</strong></label> </div> <div class="col-12 col-md-9"> <input name="gattl" id="gattl" rows="9" class="form-control" required="" value="<?php echo $gattl;?>"> </div> </div> Any help in this regard will be highly appreciated. Kind Regards, Naveed. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/ Share on other sites More sharing options...
kicken Posted August 6, 2020 Share Posted August 6, 2020 You need to alias the result column to the name you are using to access it within PHP. SELECT SUM(noopaxs-gattl) as gattl Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580411 Share on other sites More sharing options...
Naveed786 Posted August 6, 2020 Author Share Posted August 6, 2020 1 hour ago, kicken said: You need to alias the result column to the name you are using to access it within PHP. SELECT SUM(noopaxs-gattl) as gattl Hi kicken, I have tried this but still the text box is blank no value it's empty. Below is code with advised changes. <?php //Calculation for no of paxs. $query2=mysqli_query($con,"SELECT SUM(noopaxs-gattl) AS gattl FROM tblsupvisitor where date(EnterDate) = CURDATE() || gst = 'INH' || gst = 'IN' GROUP BY confno"); $calc_no_paxs_for_today=mysqli_fetch_row($query2); $gattl=$calc_no_paxs_for_today['gattl']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580414 Share on other sites More sharing options...
requinix Posted August 6, 2020 Share Posted August 6, 2020 What if you try running that query manually? Exactly as it is, copy and paste. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580416 Share on other sites More sharing options...
Naveed786 Posted August 6, 2020 Author Share Posted August 6, 2020 (edited) 16 minutes ago, requinix said: What if you try running that query manually? Exactly as it is, copy and paste. It's producing the result in mysql properly no issues once i am using in php it's not bringing any reult but now i modified the query it brings the result but it's not calculating total properly for example with confno there are 3 records and each has i noopaxs it should bring 3 but it's bringing back 2 as a sum and also it's happening for each group of confno, please see below code. <?php $eid=$_GET['editid']; $query2=mysqli_query($con,"SELECT SUM(noopaxs) as gattl FROM tblsupvisitor where date(EnterDate) = CURDATE() || gst = 'INH' || entt = 'IN' || ID='$eid' GROUP BY confno"); $calc_no_paxs_for_today=mysqli_fetch_row($query2); while ($row=mysqli_fetch_array($query2)) { ?> and allias part is: <input name="gattl" id="gattl" rows="9" class="form-control" required="" value="<?php echo $row['gattl'];?>"> Edited August 6, 2020 by Naveed786 Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580417 Share on other sites More sharing options...
Naveed786 Posted August 7, 2020 Author Share Posted August 7, 2020 Hi, Still waiting if someone can help me on this please. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580441 Share on other sites More sharing options...
mac_gyver Posted August 7, 2020 Share Posted August 7, 2020 22 hours ago, Naveed786 said: should bring 3 but it's bringing back 2 this is because your code is fetching the first row from the result set, see the first line in the following code, not using it, then looping over the remaining rows in the result set, starting at the second line in the following - 22 hours ago, Naveed786 said: $calc_no_paxs_for_today=mysqli_fetch_row($query2); while ($row=mysqli_fetch_array($query2)) { 1 Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580442 Share on other sites More sharing options...
Naveed786 Posted August 7, 2020 Author Share Posted August 7, 2020 2 hours ago, mac_gyver said: this is because your code is fetching the first row from the result set, see the first line in the following code, not using it, then looping over the remaining rows in the result set, starting at the second line in the following - Hi mac_gyver, Thanks for pointing it out, i did some study on this and changed the code now it's retrieving the correct count but now am getting one box for each noofpaxs count but i only want only one perticular count for the record which i need to edit, please see below code and screen shot. <?php $cnfno=$_GET['confno']; $query1=mysqli_query($con,"SELECT SUM(noopaxs) AS gattl FROM tblsupvisitor where date(EnterDate) = CURDATE() AND (gst = 'INH' AND entt = 'IN') || (confno='$cnfno') GROUP BY confno"); while ($row=mysqli_fetch_array($query1)) { ?> Please help me i am stuck on this. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580451 Share on other sites More sharing options...
Naveed786 Posted August 9, 2020 Author Share Posted August 9, 2020 Hi Everyone, Any help will be highly appreciated. Still trying to find solution for this. Thanks in advance. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580490 Share on other sites More sharing options...
requinix Posted August 9, 2020 Share Posted August 9, 2020 Either you should not be GROUPing BY the confno, or you should not be GROUPing BY and instead be searching only for a particular confno, or you should be including more data in the form so it's obvious that those three boxes are for three different confno values. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580495 Share on other sites More sharing options...
Naveed786 Posted August 14, 2020 Author Share Posted August 14, 2020 Hi requinix, Sorry for late reply, I already tried above method, i was thinking might be there is some way to only edit the select record, i am using the method to show more fields to identify the record and update it, can u please help me with below code, i want to disable the update button in case if the record date is in past. Can anyone help me with example, like <?php If($gst='OUT'){ disable textbox1 and textbox2 } >? I saw several examples but not able to understand properly, if conditions true then text boxes will be disabled if conditions is false then both text boxes are enabled. Thanks in Advance. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580619 Share on other sites More sharing options...
requinix Posted August 14, 2020 Share Posted August 14, 2020 Do you know how to mark a textbox as disabled? Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580622 Share on other sites More sharing options...
Naveed786 Posted August 15, 2020 Author Share Posted August 15, 2020 8 hours ago, requinix said: Do you know how to mark a textbox as disabled? Yes please i know how to mark the textbox readonly or disabled. <input name="entby" id="entby" rows="9" class="form-control" required="" readonly="" value="<?php echo $entby; ?>"> if i will put disable instead of readonly it will be disabled. I read in in one article if we use readonly the value will be updated in the database if we update a record but if it's disabled the value will not be updated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580641 Share on other sites More sharing options...
requinix Posted August 15, 2020 Share Posted August 15, 2020 10 hours ago, Naveed786 said: I read in in one article if we use readonly the value will be updated in the database if we update a record but if it's disabled the value will not be updated. What article? Because if that's what it says then you should stop reading it. Do you think the browser is doing that? Saying "oh, this textbox is disabled, I shouldn't update the database", or "oh, this textbox is readonly, so if the user changes it then I need to update the database"? It isn't. Which is why what the article says is technically wrong. And it's wrong in a way that is important for you to understand. You know how to check a condition. You know how to mark a textbox as disabled. Combine the two. If you have problems making that work then post what you've tried. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580645 Share on other sites More sharing options...
Naveed786 Posted August 15, 2020 Author Share Posted August 15, 2020 11 minutes ago, requinix said: What article? Because if that's what it says then you should stop reading it. Do you think the browser is doing that? Saying "oh, this textbox is disabled, I shouldn't update the database", or "oh, this textbox is readonly, so if the user changes it then I need to update the database"? It isn't. Which is why what the article says is technically wrong. And it's wrong in a way that is important for you to understand. You know how to check a condition. You know how to mark a textbox as disabled. Combine the two. If you have problems making that work then post what you've tried. Thanks for the guidance here is the code: <?php if($gst="OUT"){ <input name="noopaxs" id="noopaxs" readonly = "" value="<?php echo $row['noopaxs'];?>"> else{ <input name="noopaxs" id="noopaxs" value="<?php echo $row['noopaxs'];?>"> }} ?> and the error is: Parse error: syntax error, unexpected '<' Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580647 Share on other sites More sharing options...
requinix Posted August 15, 2020 Share Posted August 15, 2020 How familiar are you with PHP? Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580648 Share on other sites More sharing options...
Naveed786 Posted August 15, 2020 Author Share Posted August 15, 2020 2 minutes ago, requinix said: How familiar are you with PHP? I am beginner and on learning stage, your question is right i understand. I know the method i wrote is wrong most probably. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580649 Share on other sites More sharing options...
requinix Posted August 15, 2020 Share Posted August 15, 2020 <?php if($gst="OUT"){ <input name="noopaxs" id="noopaxs" readonly = "" value="<?php echo $row['noopaxs'];?>"> else{ <input name="noopaxs" id="noopaxs" value="<?php echo $row['noopaxs'];?>"> }} ?> Couple issues in there: 1. One = means assignment. Two == means comparison. Make sure you use the correct one. 2. You cannot put HTML directly into PHP. If you want to write HTML like you did above then you have to first leave PHP mode with a ?> and then, when you're done, go back into PHP more with a <?php. Like how those two echos look: the <?php switched from HTML to PHP mode, then the ?> switched from PHP to HTML mode. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580651 Share on other sites More sharing options...
Naveed786 Posted August 15, 2020 Author Share Posted August 15, 2020 6 minutes ago, requinix said: <?php if($gst="OUT"){ <input name="noopaxs" id="noopaxs" readonly = "" value="<?php echo $row['noopaxs'];?>"> else{ <input name="noopaxs" id="noopaxs" value="<?php echo $row['noopaxs'];?>"> }} ?> Couple issues in there: 1. One = means assignment. Two == means comparison. Make sure you use the correct one. 2. You cannot put HTML directly into PHP. If you want to write HTML like you did above then you have to first leave PHP mode with a ?> and then, when you're done, go back into PHP more with a <?php. Like how those two echos look: the <?php switched from HTML to PHP mode, then the ?> switched from PHP to HTML mode. Thanks for the guidance and help I will give it a try and will update back. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580652 Share on other sites More sharing options...
Naveed786 Posted August 16, 2020 Author Share Posted August 16, 2020 Thanks @requinix You always encourage me to learn when i was reading some articles i found the better way to achieve this is javascript. I use the java script to achieve the result onmousemove event below is the code. <script type="text/javascript"> function EnableDisable(entt) { var btnsubmit = document.getElementById("btnsubmit"); if (entt.value.trim() != "OUT") { btnsubmit.disabled = false; entt.disabled = false; } else { btnsubmit.disabled = true; entt.disabled = true; } }; Is there any other event instead of mousemove event which i can use to enable or disable the field and button. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580667 Share on other sites More sharing options...
requinix Posted August 16, 2020 Share Posted August 16, 2020 53 minutes ago, Naveed786 said: i was reading some articles i found the better way to achieve this is javascript. I use the java script to achieve the result onmousemove event below is the code. That is silly. Unless there is something important that you haven't described, there is absolutely no reason whatsoever to use Javascript to disable an element (no less to disable it using a mouseover event) when you could be using your PHP to output a textbox that is already disabled. You know that you can mark a textbox as disabled within its HTML. You know how to check a condition in PHP. I don't understand where the difficulty is in combining those two together to get a disabled textbox when some variable has some value. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580669 Share on other sites More sharing options...
Naveed786 Posted August 17, 2020 Author Share Posted August 17, 2020 Yup you are right let me give it a try also. Thanks for the recommendations. Quote Link to comment https://forums.phpfreaks.com/topic/311286-calculation-in-php/#findComment-1580672 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.