php calculator switching multiplier based on input - php

Hello I am building a simple computer in php.
It should work in the way that when the user selects number greater than 30, the variable ( in this case m1 ) becomes m2 and so on.
How can i accomplish this?
Thank you in advance
<?php
$m1 = 5 /*prezzo della maglia - price 1*/;
$m2 = 2;/*price when quantity is over a number*/
$m3 = 1;/*price when quantity is over a number*/
$s1 = 3; /*price of print 1*/
$s1 = 3; /*price of print 1 if quantity change*/
$bxs = $_GET['bxs'];
$bs = $_GET['bs'];
$bm = $_GET['bm'];
$bl = $_GET['bl'];
$bxl = $_GET['bxl'];
$b2xl = $_GET['b2xl'];
/*riga del nero*/
$nxs = $_GET['nxs'];
$ns = $_GET['ns'];
$nm = $_GET['nm'];
$nl = $_GET['nl'];
$nxl = $_GET['nxl'];
$n2xl = $_GET['n2xl'];
$somma = $m1*($bxs+$bs+$bm+$bl+$bxl+$b2xl+$nxs+$ns+$nm+$nl+$nxl+$n2xl);
?>

It is simple, but you must provide the user way enter that value.
For example by the GET parameter number
$userInput = $_GET['number'];
Then you only need to create basic if statement. You can read about it at http://php.net/manual/en/control-structures.if.php
$somma = $bxs+$bs+$bm+$bl+$bxl+$b2xl+$nxs+$ns+$nm+$nl+$nxl+$n2xl;
if($userInput <= 30){
$somma *=$m1;
}else if($userInput >30) {
$somma *=$m2;
} else if($userInput > 8779){ // your number : )
$somma *=$m3;
}

Related

Increase variable count only if it has same value on some elements of array - PHP

I don't know either its the correct title for this question. Please, help me to edit this question if I am not adding appropriate title.
I am trying to work with hall space booking for event. There are three types of space booking:
Full - 0 (Can book by one team at same time)
Half - 1 (Can book by 2 team at same time)
Shared - 2 (Can book by 4 teams at same time)
So, I write following mention code.
<?php
$obj0 = new stdClass();
$obj0->id = '1';
$obj0->start = '2019-06-28';
$obj0->end = '2019-06-28';
$obj0->start_time = '07:00:00';
$obj0->end_time = '08:00:00';
$obj0->hall_space = '1';
$obj1 = new stdClass();
$obj1->id = '7';
$obj1->start = '2019-06-28';
$obj1->end = '2019-06-28';
$obj1->start_time = '06:00:00';
$obj1->end_time = '07:00:00';
$obj1->hall_space = '1';
$obj2 = new stdClass();
$obj2->id = '2';
$obj2->start = '2019-06-29';
$obj2->end = '2019-06-29';
$obj2->start_time = '07:00:00';
$obj2->end_time = '08:00:00';
$obj2->hall_space = '1';
$obj3 = new stdClass();
$obj3->id = '8';
$obj3->start = '2019-06-29';
$obj3->end = '2019-06-29';
$obj3->start_time = '06:00:00';
$obj3->end_time = '07:00:00';
$obj3->hall_space = '1';
$obj4 = new stdClass();
$obj4->id = '3';
$obj4->start = '2019-06-30';
$obj4->end = '2019-06-30';
$obj4->start_time = '07:00:00';
$obj4->end_time = '08:00:00';
$obj4->hall_space = '1';
$obj5 = new stdClass();
$obj5->id = '9';
$obj5->start = '2019-06-30';
$obj5->end = '2019-06-30';
$obj5->start_time = '06:00:00';
$obj5->end_time = '07:00:00';
$obj5->hall_space = '1';
$data = array($obj0, $obj1, $obj2, $obj3, $obj4, $obj5);
$hall_space = 1; //this means user is trying to book half space
if(count($data) > 0) {
$half = 0;
$shared = 0;
$error = false;
$looping_date = '';
$same_date = 0;
switch ($hall_space) {
case '0':
echo "aa";
$error = true;
break;
case '1':
foreach ($data as $dk => $dv) {
if($looping_date == $dv->start){
$same_date = $same_date + 1;
}else{
$same_date = 0;
}
if ($dv->hall_space == 0) {
echo "bb";
$error = true;
} elseif ($dv->hall_space == 1) {
$half = $half + 1;
if ($half == 2) {
echo "cc";
$error = true;
}
} elseif ($dv->hall_space == 3) {
$shared = $shared + 1;
if ($shared == 2) {
echo "dd";
$error = true;
}
}
}
break;
case '2':
foreach ($data as $dk => $dv) {
if ($dv->hall_space == 0) {
echo "ee";
$error = true;
} elseif ($dv->hall_space == 1) {
$half = $half + 1;
if ($half == 2) {
echo "ff";
$error = true;
}
} elseif ($dv->hall_space == 2) {
$shared = $shared + 1;
if ($shared == 3) {
echo "gg";
$error = true;
}
}
}
break;
}
}
if( isset($error) ){
echo "error";
}else{
echo "not error";
}
This was working fine for a singe event (i.e non-repeating). But, this is not working correctly for repeating events.
Here, in this scenario user trying to book half-space(i.e value 1) for date 2019-06-28 to 2019-06-30 from 6 am to 8am.
Above mentioned $data represents all the already booked events for that time frame, that means on every day there are are two half-spaced events (one from 6-7am and next from 7-8am) on each day. Which means this new events (half-spaced) should be allowed to book.
But, my logic is not working because, it is considering all those events as they are going to held in same date and time.That means, $half variable should be increased only if the events are on same date and time and finally should not generate error.
I tried to explain everything from myside, also you can just copy and paste the code for test. And, let me know if you need any further details.
$bookings = [
[
'period' => new DatePeriod(
new DateTime('2019-06-28 07:00:00'),
new DateInterval('PT1H'),
new DateTime('2019-06-28 08:00:00')
),
'byWho' => ['1' => 0.5, '2' => 0.5]
],
[
'period' => new DatePeriod(
new DateTime('2019-06-28 08:00:00'),
new DateInterval('PT1H'),
new DateTime('2019-06-28 09:00:00')
),
'byWho' => ['2' => 0.5]
]
];
Example data structure, between 7 and 8 on 2019-06-28 team '1' booked half the hall. Between 7 and 9 team '2' booked half the hall. All bookings should be 1 hour, if they're longer than an hour such as team two then they actually have two bookings right next to each other. If 1 hour is too long then you can change the interval if you like.
So instead of having an array of who booked when which is what you have, instead here you have an array showing a timeline, you just need to look at a specific time to see if it's booked and how full it is.
This way is better than yours because you can easily keep those dates sorted, bring this data in and out of a database into this structure with ease. Now because it's sorted you can really easily find the time you're booking for and you only need to look at the space available for that time period. But for yours you need to check every single booking to see if there is space.
If you want to check if there is space for a specific time you can use a binary search (choose your own search if you want to make this easier) to find an intersecting DatePeriod, then check values either side to find any other matching date periods. Then you simply do a check to see if in those periods there's space to fit the booking. To check for space it's as easy as summing up the values in 'byWho' + your booking size and see if it's greater than 1.

Php save more Array into MySQL

I'm trying to save arrays on a MySQL database with PHP.
The code inserts only the first line, if I have an array of 5 elements it just inserts the first element and the others and 4 don't save them for me.
Can anyone tell me where I'm wrong?
Thanks a lot.
<?php
//getting user values
$day = $_POST['Day'];
$nDay = $_POST['n_Day'];
$fieldOne = $_POST['Field_one'];
$fieldTwo = $_POST['Field_two'];
$timeOne = $_POST['Time_one'];
$timeTwo = $_POST['Time_two'];
$idR = $_POST['id_ristorante'];
$day_array = explode(",",$day);
$nDay_array = explode(",",$nDay);
$timeOne_array = explode(",",$timeOne);
$timeTwo_array = explode(",",$timeTwo);
$len = count($day_array and $nDay_array and $timeOne_array and $timeTwo_array);
$output=array();
//require database
require_once('db.php');
//checking if email exists
$conn=$dbh->prepare('SELECT id_ristorante FROM Orari WHERE id_ristorante=:idR');
$conn->bindParam(':idR', $idR, PDO::PARAM_STR);
$conn->execute();
//results
if($conn->rowCount() !==0){
$output['isSuccess'] = 0;
$output['message'] = "Orario già inserito";
} else {
for($i=0;$i<$len;$i++){
$day = $day_array[$i];
$nDay = $nDay_array[$i];
$timeOne = $timeOne_array[$i];
$timeTwo = $timeTwo_array[$i];
$conn=$dbh->prepare('INSERT INTO Orari (Day, n_Day, Field_one, Field_two, Time_one, Time_two, id_ristorante) VALUES (?,?,?,?,?,?,?)');
//encrypting the password
$conn->bindParam(1,$day);
$conn->bindParam(2,$nDay);
$conn->bindParam(3,$fieldOne);
$conn->bindParam(4,$fieldTwo);
$conn->bindParam(5,$timeOne);
$conn->bindParam(6,$timeTwo);
$conn->bindParam(7,$idR);
$conn->execute();
if($conn->rowCount() == 0) {
$output['isSuccess'] = 0;
$output['message'] = "Errore, riprova.";
} elseif($conn->rowCount() !==0){
$output['isSuccess'] = 1;
$output['message'] = "Orari salvati!";
}
}
}
echo json_encode($output);
?>
When you trying to do count on multiple array as:
$len = count($day_array and $nDay_array and $timeOne_array and $timeTwo_array);
The and cause the array to be boolean evaluated so the final assign to $len is 1 and that why the loop is done only once and only the first element is inserted to DB.
If all the array in the same length you should just do count on 1 of them as:
$len = count($day_array)
Better practice will be to do count on each on them and then assign $len with the minimum value
Change this line
$len = count($day_array and $nDay_array and $timeOne_array and $timeTwo_array);
To
$len = count($day_array) + count($nDay_array) + count($timeOne_array) + count($timeTwo_array);

PHP pagination. End array has different number of values than the initial query

Up until now I've always been limiting my pages for any pagination simply using MySQL LIMIT. It's been performing well.
However... Now I've written an application that grabs the data from 3 separate MySQL tables, after that I'm using array_multisort() to sort them.
The main issue here is that it is used for SMS messages(gammu smsd). If I write a message consisting of let's say 900 characters - it is split to 7 entries in the database. It is split every 139 characters (the number might be different sometimes)
Below code somewhat fixes this issue based on 'SequencePosition' field in the database.
So if I were to LIMIT it using MySQL I would have less results than I specified since MySQL has no idea what I'm doing with the data later.
I hope it makes sense to you guys.
How would you solve this issue? Can you think of a better way to do it? I mean- grabbing all the data from the table is rarely a good idea ;(
See PHP code below.
<?php
if(isset($_SESSION['id']))
{
if(isset($_GET['contact_number']) && ($_GET['contact_name']))
{
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page = 1 ;
}
$gmessages_page = 30; //this is actually a static value, normally it resides in a config file
$start_from = ($page-1) * $gmessages_page;
$contact_number = base64_decode($_GET['contact_number']);
$contact_name = base64_decode($_GET['contact_name']);
$query_inbox = "SELECT ReceivingDateTime, SenderNumber, TextDecoded FROM inbox WHERE SenderNumber='$contact_number' ORDER BY ReceivingDateTime";
$query_sentitems = "SELECT SendingDateTime, DestinationNumber, TextDecoded, Status, SequencePosition FROM sentitems WHERE DestinationNumber='$contact_number' ORDER BY SendingDateTime";
$query_outbox = "SELECT SendingDateTime, DestinationNumber, TextDecoded FROM outbox WHERE DestinationNumber='$contact_number' ORDER BY SendingDateTime";
$result_inbox = $conn->query($query_inbox);
$result_sentitems = $conn->query($query_sentitems);
$result_outbox = $conn->query($query_outbox);
if(!$result_inbox || !$result_sentitems || !$result_outbox)
{
die("Błąd połączenia z bazą (!RESULT)");
}
$rows_inbox = $result_inbox->num_rows;
$rows_sentitems = $result_sentitems->num_rows;
$rows_outbox = $result_outbox->num_rows;
if($rows_inbox == 0 || $rows_sentitems == 0)
{
$conn->close();
}
//inbox
$inbox_person = $contact_name;
for($j = 0; $j < $rows_inbox ; ++$j)
{
$result_inbox->data_seek($j);
$row_inbox = $result_inbox->fetch_array(MYSQLI_NUM);
$inbox_date[] = $row_inbox[0];
$inbox_number = $row_inbox[1];
$inbox_text[] = $row_inbox[2];
$sent_status[] = 0;
$sent_sequence_position[] = 0;
$inbox_type[] = 1;
}
for($j = 0; $j < $rows_sentitems ; ++$j)
{
$result_sentitems->data_seek($j);
$row_sentitems = $result_sentitems->fetch_array(MYSQLI_NUM);
if($row_sentitems[4] == 1)
{
$sent_sequence_position[] = $row_sentitems[4];
$inbox_date[] = $row_sentitems[0];
$inbox_text[] = $row_sentitems[2];
$sent_status[] = $row_sentitems[3];
$inbox_type[] = 2;
}
else
{
$inbox_text[sizeof($inbox_type) - 1] .= $row_sentitems[2];
}
}
for($j = 0; $j < $rows_outbox ; ++$j)
{
$result_outbox->data_seek($j);
$row_outbox = $result_outbox->fetch_array(MYSQLI_NUM);
$inbox_date[] = $row_outbox[0];
$inbox_text[] = $row_outbox[2];
$sent_status[] = "QueuedForSending";
$inbox_type[] = 2;
}
$number_of_records = sizeof($inbox_date);
$number_of_pages = ceil($number_of_records / $gmessages_page);
array_multisort($inbox_date, $inbox_text, $inbox_type, $sent_status, $sent_sequence_position);
$smarty->assign("inbox_date", $inbox_date);
$smarty->assign("inbox_text", $inbox_text);
$smarty->assign("inbox_number", $inbox_number);
$smarty->assign("inbox_person", $contact_name);
$smarty->assign("inbox_type", $inbox_type);
$smarty->assign("sent_status", $sent_status);
$smarty->assign("start_from", $start_from);
$smarty->assign("gmessages_page", $gmessages_page);
$smarty->assign("number_of_pages", $number_of_pages);
$smarty->assign("number_of_records", $number_of_records);
$smarty->assign("sent_sequence_position", $sent_sequence_position);
$smarty->assign("page", $page);
//for $_GET after sending SMS
$smarty->assign("contact_number_enc", $_GET['contact_number']);
$smarty->assign("contact_name_enc", $_GET['contact_name']);
$smarty->display('templates/conversation_body.tpl');
}
}
else
{
$smarty->display('templates/login_body.tpl');
}

Choose a string based on random number

I am working on a bit of PHP and I've come upon a bit of issues.
I am using PHP to randomly choose a number from 1-360. I am trying to compare the answer to a list of value determined by range.
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC = range(0,21) {
$result = "Orange";
}
elseif ($NumberC = range(22,42) {
$result = "Red";
}
elseif ($NumberC = range(43,63) {
$result = "Blue";
}
//This goes on for a while ...
else {
$result = "Green";
}
echo = $result;
Anytime i do this, the result always assigns the value of "Orange" to $result .
Im sure im doing something wrong here, please help!
First of all, you used just one '=' to compare while it should have been '=='. Second range() generates an array and you cannot compare an integer to an array. Third why generating the range every single time when you can check that $NumberC lies between the minimum and the maximum numbers of the range?
Change your code to:
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC >= 0 && $NumberC <= 21) {
$result = "Orange";
} elseif ($NumberC >= 22 && $NumberC <= 42) {
$result = "Red";
} elseif ($NumberC >= 43 && $NumberC <= 63) {
$result = "Blue";
} else {
$result = "Green";
}
echo $result;
Shall work. Hope this helps.

else if not working in

if condition matches but else if condition is not working when i insert $total_salary = 10000 and $salary_type = NO.
if ($this->request->is('post')) {
$DATA = $this->data;
$employee_salary = $this->Salary->save($DATA);
$total_salary = $employee_salary['Salary']['salary'];
$salary_type = $employee_salary['Salary']['salary_in_ctc'];
echo $total_salary . $salary_type;
if (($total_salary > 15000) && ($salary_type === 'YES')) {
$pf_company = 1500;
$pf_employee = 1500;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
} elseif (($total_salary < 15000) && ($salary_type === 'NO')) {
echo'hello';
$pf_company = 1200;
$pf_employee = 1200;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
}
try to match the uppercase condition and also check for white space in you conditions.
strtoupper(trim($salary_type))
The possible second issue could be:
As you are checking the datatype also in the condition $salary_type ==='NO' and in the post you stated you are passing $salary_type = NO. which doesn't seems to be a string type.
Please try removing one = from condition or passing NO as a string to validate the condition

Categories