How to decreasing a quantity with notifier - php

Hi Guys This is my newbie question...
I started learning php three days ago...
This is my problem...
for example
I have a quantity 50 when I subracted by 10 the value 50 became 40
and the notifier alert me that number is less than 50
but still subracted when I subract it again .....
Here is My codes
if(isset($_POST['submit'])){
$id = $_GET['id'];
$old = $_POST['quantity'];
$new = $_POST['quantity1'];
$total = $new - $old;
if($total < 50 ){
echo "<h1 style='color: red'><center>CRITICAL LEVEL</center></h1>";
echo "<script>window.open('inventory.php' , '_self');</script>";
}
else{
$uiqry = $mysqli->prepare("UPDATE table_inventory SET quantity = ? WHERE id = ?");
$uiqry->bind_param('ii', $total,$id);
$uiqry->execute();
$uiqry->close();
echo "<script>alert('Subracted');</script>";
echo "<script>window.open('inventory.php', '_self');</script>";
}
}
I want When I the value 50 and I subract it from 10 the value is now 40 and the alert notify me
and when i subract it again by 10 the value become 30 and the notify popup again
how to do this
thanks in advance..
Sorry for my tongue twister english.. :D

if(isset($_POST['submit'])){
$id = $_GET['id'];
$old = $_POST['quantity'];
$new = $_POST['quantity1'];
$total = $new - $old;
if($total < 50 ){
echo "<h1 style='color: red'><center>CRITICAL LEVEL</center></h1>";
echo "<script>window.open('inventory.php' , '_self');</script>";
}
$uiqry = $mysqli->prepare("UPDATE table_inventory SET quantity = ? WHERE id = ?");
$uiqry->bind_param('ii', $total,$id);
$uiqry->execute();
$uiqry->close();
echo "<script>alert('Subracted');</script>";
echo "<script>window.open('inventory.php', '_self');</script>";
}

Related

Filter negative and positive values out of one variable in PHP

I have a variable in PHP where values from a database are inserted. It’s about money. I need to summarize all negative and positives values separately.
Example:
February, I have:
+ 10
− 10
+100
− 50
− 15
+ 70
+ 80
—
Credits added: 260 Euro
Credits paid: −75 Euro
The variable is named $amound in my PHP file. I really have no clue how to do that.
In Excel, this would be as follows: =SUMMEWENN(E1:E48;"<0")
But here I only have a variable, not fields.
Heres some Code:
$reportdata["tableheadings"] = array("Transaktions-ID","Kunde","Datum","Beschreibung","Betrag");
if ($startdate && $enddate) {
$query = "SELECT tblcredit.*,tblclients.firstname,tblclients.lastname FROM tblcredit INNER JOIN tblclients ON tblclients.id=tblcredit.clientid WHERE tblcredit.date BETWEEN '".db_make_safe_human_date($startdate)."' AND '".db_make_safe_human_date($enddate)."'";
$result = full_query($query);
while ($data = mysql_fetch_array($result)) {
$id = $data["id"];
$userid = $data["clientid"];
$clientname = $data["firstname"]." ".$data["lastname"];
$date = fromMySQLDate($data["date"]);
$description = $data["description"];
$amount = $data["amount"];
$currency = getCurrency($userid);
$amount = formatCurrency($amount);
$overallamount += $amount;
// $overallamountout -= $amount;
// $overallamountin += $overallamount > 0;
$reportdata["tablevalues"][] = array($id,''.$clientname.'',$date,nl2br($description),$amount);
}
Indeed, this is very simple (I assume that all of your variables are strings, e.g. coming from a text file, but even if not it will work):
$var1 = '+80';
$var2 = '-30';
$result = (int)$var1 + (int)$var2;
var_dump($result);
Will result in: "int(50)" => Working fine.
You can do it in the DB
SELECT SUM(numbers) AS sum1 FROM pepa WHERE numbers > 0;
SELECT SUM(numbers) AS sum2 FROM pepa WHERE numbers < 0;
Okay,
its done.
Heres the Code which was used:
$summeGesamt = 0;
$summeEingezahlt = 0;
$summeAusgezahlt = 0;
$summeGesamt = $summeGesamt + $data["amount"];
if ( $data["amount"] >= 0) {
$summeEingezahlt += $data["amount"];
} else {
$summeAusgezahlt += $data["amount"];
}

If-else statement to count some variable

I need some help to solved this algorithm problem with How a Person is popular in his city.
My situation
How the algorithm should work like
If a person "mark" has 500 friends in his city out of 500,000.
(500/500,000)*50,000 = 5
So 5 in 50,000 people Know him right.
But When friends count increase the 50,000 should decrease
If "sam" has 1000 friends then
(1000/500,000)*25000 = 5
So 5 in 25000 people know his name
Yes we could implement this in if/else condition
If so then i have to write 500 lines of code.
Is there another way to do this in PHP?
<?php
$totalfriends = 100;
$totali = 5000000;
$name = "Sam";
if ($totalfriends >= 100 && $totalfriends <= 499 ) {
$r = ($totalfriends/$totali)*50000;
echo round($r),' ',"in 50K People on City regonize this Profile";
}else if ($totalfriends >= 500 && $totalfriends <= 999) {
$r = ($totalfriends/$totali)*25000;
echo round($r),' ',"in 25K People on City know".$name;
}else{
echo "";
}
?>
is this what you are looking for?
foreach([100, 500, 543, 1000, 5000, 51000, 500000] as $my_friends)
echo '5 in '. getScoreOf($my_friends) . "<br>";
function getScoreOf($my_friends){
$of = 5;
$total = 5e5; //that's 500,000 ;)
$step = 100; //minimum step, so output is not "4604" but "4600"
$out_of = $total / $my_friends * $of;
return $out_of > $step? round($out_of / $step) * $step: round($out_of);
}
run it in sandbox
edit: solution merged with original code
<?php
$of = 5;
$totalfriends = 100;
$name = "Sam";
echo $of ." in ". getScoreOf($of, $totalfriends) ." people in city know ". $name;
function getScoreOf($of, $my_friends){
$total = 5e6; //that's 5,000,000 ;)
$step = 100; //minimum step, so output is not "4604" but "4600"
$out_of = $total / $my_friends * $of;
return $out_of > $step? round($out_of / $step) * $step: round($out_of);
}

How can I addition mysql numeric data with another mysql numeric data

<?php
$new_result = mysqli_query($con, "SELECT input_cost FROM data_input");
while ($new = mysqli_fetch_array($new_result)) {
$sum += $new['input_cost'];
}
echo "<h2> total cost of this month is $".$sum. "</h2>";
?>
but the result say
<br>
Notice: Undefined variable: sum in
C:\xampp\htdocs\work_shop\back_end\data_input_output\result.php on
line 57
<br>
total cost of this month is $300
which is correct result....
<br>
How can I solve this problem...??
You need to define $sum variable outside loop. Try this-
<?php
$sum = 0; // define sum outside loop
$new_result = mysqli_query($con, "SELECT input_cost FROM data_input");
while ($new = mysqli_fetch_array($new_result)) {
$sum += $new['input_cost'];
}
echo "<h2> total cost of this month is $".$sum. "</h2>";
?>
$sum is undefined because you are only adding, and not setting a value.
<?php
$new_result = mysqli_query($con, "SELECT input_cost FROM data_input");
$sum = 0;
while ($new = mysqli_fetch_array($new_result)) {
$sum += $new['input_cost'];
}
echo '<h2> total cost of this month is $'.$sum.'</h2>';
?>

how to set if price row is comming soon than show update soon

how to set if price row is comming soon than show update soon
how to set if price row is comming soon than show update soon
mysql
<?
//connect to database
mysql_connect('localhost','phone','pasword');
mysql_select_db('phone');
//////Displaying Data/////////////
$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$q=mysql_query("select * from price where id=$id AND TRIM(model) IS NOT NULL");
//Adds one to the counter
mysql_query("UPDATE price SET counter = counter + 1 where id=$id ");
//Retreives the current count
$count = mysql_fetch_row(mysql_query("SELECT counter FROM price"));
$row=mysql_fetch_object($q);
echo mysql_error();
echo "<table class='hovertable'><tr><td><img src='admin/media/$row->photo' ></td></tr>";
echo "</table>";
?>
example
my rows
2000
3000
comming soon
4900
and this code is showing like this
2000
2000
3000
Warning: number_format() expects parameter 1 to be double, string given in
4900
and i want like this
2000
2000
3000
update soon
4900
how to solve this problem please help me to solve this issue
<?php if($row->price):?><tr class=\"style1\"><td width='200'><b>Price US $:</b></td><td>Price in Dollar:
<?php
function currencyExchange($amount,$baseCurrency,$quoteCurrency) {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$baseCurrency[0]$quoteCurrency[0]=X&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchangeRate = fread($open, 2000);
fclose($open);
$exchangeRate = str_replace("\"", "", $exchangeRate);
$exchangeRate = explode(",", $exchangeRate);
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 2);
$amount = number_format ($amount);
$timeStamp = strtotime($exchangeRate[2]);
$timeStamp = date('F d, Y', $timeStamp);
$timeStamp = "$timeStamp $exchangeRate[3]";
echo "$results\n";
}
// for additional currency ticker symbols visit: http://finance.yahoo.com/currency-converter
$usd = array('USD','US Dollars');
$INR = array('INR','Indian Rupees');
$usd = array('usd','US DOLLARS');
$b = str_replace( ',', '', $row->price );
if( is_numeric( $b ) ) {
$row->price = $b;
}
currencyExchange("$b",$INR,$usd);
?></td></tr><?php endif; ?>
Test if the amount is numeric before trying to use functions that only work for numbers.
Replace:
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 2);
$amount = number_format ($amount);
with:
if (is_numeric($amount)) {
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 2);
} else {
$results = 'update soon';
}
You don't need to call number_format() on $amount because you never print that value.
What about setting the column to something like 9999999999 ( which will get around numbers problem ) then testing the incoming for these numbers and if found then echo coming soon

Repeat Event Monthly php mktime

I have a reservation tool that allows for repeating events to be scheduled. I have been able to figure out daily and weekly repeating, but hung up on the monthly repeat because of the varying days in a month.
The goal is to repeat the reservation every third Thursday of the month for example. I thought I had it by using some simple math and scheduling it at interval of 28, but that did not work. I am assuming my code needs some additional variable.
I have a form that passes my numeric value and that worked as I mentioned for daily and weekly repeats. You will see I passed 28 as my value for monthly, but causes issue. It does repeat on the same day, but over time on the wrong week.
<input type="radio" class="radio" name="recurring_span" value="1" checked="checked"/><img src='images/icons/calendar-select-days-span.png' alt='daily' title='Daily'>
<input type="radio" class="radio" name="recurring_span" value="7"><img src='images/icons/calendar-select-days.png' alt='weekly' title='Weekly'/>
<input type="radio" class="radio" name="recurring_span" value="28"><img src='images/icons/calendar-select-days.png' alt='montly' title='Monthly'/>
</p>
The processing php is as follows (sorry if I am including too much, but as I said not great with PHP, figured more was better than less):
//store recurring reservation
if ($recurring_date > $reservation_date){
$repeatid = querySQL('res_repeat');
$keys[] = 'repeat_id';
$values[] = "'".$repeatid."'";
}
// UNIX time
$res_dat = mktime(0,0,0,(int)$m1,(int)$d1,(int)$y1);
$recurring_date = mktime(0,0,0,(int)$m2,(int)$d2,(int)$y2);
$recurring_date = ($recurring_date<$res_dat) ? $res_dat : $recurring_date;
// daily or weekly recurring?
$recurring_span = ($_POST['recurring_span']) ? $_POST['recurring_span'] : 1;
//cut both " ' " from reservation_pax
$res_pax = substr($_SESSION['reservation_pax'], 0, -1);
$res_pax = substr($_SESSION['reservation_pax'], 1);
// check if pax not '0'; prevent 'Christof Keller' bug
if ($res_pax < 1) {
$res_pax = 1;
}
//cut both " ' " from reservation_time
$startvalue = $_SESSION['reservation_time'];
$startvalue = substr($startvalue, 0, -1);
$startvalue = substr($startvalue, 1);
// do not subtract pax and table when reservation is moved
//$res_pax = ($_SESSION['outletID'] == $_POST['old_outlet_id']) ? $res_pax : $res_pax*2;
//$res_tbl = ($_SESSION['outletID'] == $_POST['old_outlet_id']) ? 1 : 2;
// main loop to store all reservations ( one or recurring)
while ( $res_dat <= $recurring_date) {
// build new reservation date
$index = '';
$index = array_search('reservation_date',$keys);
// build for availability calculation
$_SESSION['selectedDate'] = date('Y-m-d',$res_dat);
if($index){
$values[$index] = "'".$_SESSION['selectedDate']."'";
}else{
$keys[] = 'reservation_date';
$values[] = "'".$_SESSION['selectedDate']."'";
}
$index = '';
$index = array_search('reservation_wait',$keys);
if($index){
$values[$index] = '1';
}
//Check Availability
// =-=-=-=-=-=-=-=-=
// get Pax by timeslot
$resbyTime = reservationsByTime('pax');
$tblbyTime = reservationsByTime('tbl');
// get availability by timeslot
$occupancy = getAvailability($resbyTime,$general['timeintervall']);
$tbl_occupancy = getAvailability($tblbyTime,$general['timeintervall']);
$val_capacity = $_SESSION['outlet_max_capacity']-$occupancy[$startvalue];
$tbl_capacity = $_SESSION['outlet_max_tables']-$tbl_occupancy[$startvalue];
if( $res_pax > $val_capacity || $tbl_capacity < 1 ){
//prevent double array entry
$index = array_search('reservation_wait',$keys);
if($index>0){
if ($values[$index] == '0') {
// error on edit entry
$_SESSION['errors'][] = date($general['dateformat'],strtotime($_SESSION['selectedDate']))." "._wait_list;
}
$values[$index] = '1'; // = waitlist
}else{
// error on new entry
$keys[] = 'reservation_wait';
$values[] = '1'; // = waitlist
$_SESSION['errors'][] = date($general['dateformat'],strtotime($_SESSION['selectedDate']))." "._wait_list;
}
}
// END Availability
// number of database fields
$max_keys = count($keys);
// enter into database
// -----
$query = "INSERT INTO `$dbTables->reservations` (".implode(',', $keys).") VALUES (".implode(',', $values).") ON DUPLICATE KEY UPDATE ";
// Build 'on duplicate' query
for ($i=1; $i <= $max_keys; $i++) {
if($keys[$i]!=''){
$query .= $keys[$i]."=".$values[$i].",";
}else{
$max_keys++;
}
}
// run sql query
//echo "Query: ".$query;
$query = substr($query,0,-1);
$result = query($query);
$new_id = mysql_insert_id();
$_SESSION['result'] = $result;
// setup the right ID
if( isset($new_id) || $new_id != $_POST['reservation_id']){
$history_id = $new_id;
}else{
$history_id = $_POST['reservation_id'];
}
// store changes in history
$result = query("INSERT INTO `$dbTables->res_history` (reservation_id,author) VALUES ('%d','%s')",$history_id,$_POST['reservation_booker_name']);
// -----
// increase reservation date one day or week
$d1 += $recurring_span;
$res_dat = mktime(0,0,0,$m1,$d1,$y1);
} // end while: reservation to store
I should not I am not so great with PHP and trying to learn, my experience is limited to working with stumbling around existing code. For this project I have been working to customize and modify an opensource scheduling tool for my needs. Researching the mktime without much progress. Thanks in advance!

Categories