Jump to content

Calculation in PHP


Naveed786

Recommended Posts

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.

Link to comment
Share on other sites

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'];
 ?>

 

Link to comment
Share on other sites

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 by Naveed786
Link to comment
Share on other sites

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)) {

 

  • Thanks 1
Link to comment
Share on other sites

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.

Confno.JPG

Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.