How to calculate unknown array values in php - php

I have this form but would like to get the total.
Please how can I calculate this array?
...While(ticket table row)
{
<input type="text" name="ticket[][price]" id="ticket[][price]" value="12">
<input type="text" name="ticket[][fee]" id="ticket[][fee]" value="3462">
<select name="ticket[][qty]">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
</select>
}
Eg. $total = ticket[][price] + ticket[][fee] * ticket[][qty];

Maybe something like this?
$total = 0;
foreach ($_POST['ticket'] as $ticket) {
$total += $ticket['price'] + $ticket['fee'] * $ticket['qty'];
}
print $total;

Related

How can I achieve years on drop down menu

Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>

how to auto calculate price and quantity and then display the result? AJAX PHP

I am creating this simple project that includes auto calculate of the price and quantity and then display it only after two < select > tags has been triggered,set or selected using AJAX without using submit button. I can achieve this but does not automatically calculates and display it without using submit button.
this is my code in HTML:
<form method="post">
<select name="days">
<option value="2">2 days</option>
<option value="3">3 days</option>
</select>
<select name="persons">
<option value="2">2 persons</option>
<option value="4">4 persons</option>
<option value="6">6 persons</option>
<option value="8">8 persons</option>
</select>
</form>
Assume that in my Database
day 2 == 1000
day 3 == 2000
this is my code in PHP:
if(isset($_POST['submit'])) {
$days = $_POST['days'];
$persons = $_POST['persons'];
// get database of the 'price' from rates tbl
$sql = "SELECT * FROM rates WHERE duration='$days'";
$res = mysqli_query($conn,$sql);
$rates_tbl = mysqli_fetch_assoc($res);
$rates = $rates_tbl['price'];
$total_price = ($rates*$persons);
echo 'total: ' . number_format($total_price) . '.00';
}
Should i use this type of ajax?? I dont even know if this is right.
<script type="text/javascript">
$.ajax({
url:"autocalculate.php",
method:"POST"
})
</script>
If the goal is to display the values on screen you can use this.
<form method="post">
<select name="days">
<option value="2">2 days</option>
<option value="3">3 days</option>
</select>
<select name="persons">
<option value="2">2 persons</option>
<option value="4">4 persons</option>
<option value="6">6 persons</option>
<option value="8">8 persons</option>
</select>
and your jQuery script
var readyToCalculate = false;
var optionChanges = 0;
$('select').on('change', function (e) {
if(readyToCalculate==true){
calculate();
} else {
optionChanges ++;
if (optionChanges==2){
readyToCalculate = true;
calculate();
}
}
});
function calculate(){
var val1 = parseInt($('select[name=days]').val());
var val2 = parseInt($('select[name=persons]').val());
var result = (val1 + val2);
$(".results").html(result);
}

SELECT multiple WHERE in same column

I want choose month and year from the same column. How can I separate month and year from the column?
<label>Bulan :</label>
<select name="month">
<option value=""></option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mac</option>
<!-- ... -->
</select>
<label>Tahun :</label>
<select name="year">
<option value=""></option>
<option value="2017">2017</option>
<option value="2016">2016</option>
</select>
<input type="submit" value="Hantar" >
<?php
if(isset($_POST['year']) && ($_POST['month']))
{
$tarikh = mysql_real_escape_string($_POST['month']);
$tarikh = mysql_real_escape_string($_POST['year']);
I am running this query
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikh')
and (YEAR(tarikh) = '$tarikh')";
Why doesn't the output display? What is wrong with the above query?
in query string you use same variable for month and year
add different variable names in
$tarikhMonth = mysql_real_escape_string($_POST['month']);
$tarikhYear = mysql_real_escape_string($_POST['year']);
and
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikhMonth')
and (YEAR(tarikh) = '$tarikhYear')";
You are overwriting your $tarikh variable. Naming them $month and $year should work.
I would use PDO instead of trying to escape strings manually as well.
You need to have different variable names for the month and year. Try this
<label>Bulan :</label> <select name="month">
<option value=""></option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mac</option>...
</select><label>Tahun :</label> <select name="year">
<option value=""></option>
<option value="2017">2017</option>
<option value="2016">2016</option>
</select><input type="submit" value="Hantar" ></td>
<?php
if(isset($_POST['year']) && ($_POST['month']))
{
$tarikh_month = mysql_real_escape_string($_POST['month']);
$tarikh_year = mysql_real_escape_string($_POST['year']);
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikh_month')
and (YEAR(tarikh) = '$tarikh_year')";

array in php function undefined

For training purposes i need to make a function which tells me the 'travel cost' between 2 cities. The book tells me to type this function:
<?php
function travelcost($start, $destination)
{
$travelcost = array();
$travelcost[1] = array();
$travelcost[2] = array();
$travelcost[3] = array();
$travelcost[4] = array();
$travelcost[1][1] = 0;
$travelcost[1][2] = 30;
$travelcost[1][3] = 60;
$travelcost[1][4] = 90;
echo($travelcost[$start][$destination] . " Euro's");
}
?>
In addition i've created this form to ask for a start and a destination:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Start: <select name="start" value="true">
<option value="start[]">Amsterdam</option>
<option value="start[]">Utrecht</option>
<option value="start[]">Den Haag</option>
<option value="start[]">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="destination[]">Amsterdam</option>
<option value="destination[]">Utrecht</option>
<option value="destination[]">Den Haag</option>
<option value="destination[]">Rotterdam</option>
</select>
<p><input type="submit" name="calculate" value="Calculate"</p>
</form>
Followed by:
<?php
if(isset($_POST["start"])&& isset($_POST["destination"]))
{
travelcost($_POST['start'], $_POST['destination']);
}
?>
This gives me Undefined index: start[]
I know im doing it wrong, but i just can't see the logic in the function and the array. I assume the function is correct because it's right out of the book but i'm also not sure about that.
Can someone help me out?
This is wrong,
<option value="start[]">Amsterdam</option>
^ ^
It should be
<option value="start">Amsterdam</option>
or
<option value="Amsterdam">Amsterdam</option>
Same for all options in start and destination.
According to your function `travelcost(), your select should be
Start: <select name="start" value="true">
<option value="1">Amsterdam</option>
<option value="1">Utrecht</option>
<option value="1">Den Haag</option>
<option value="1">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>

Error setting the selected value from a select list

I have a HTML select list where one can chose an option.
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' selected>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
When this list is submitted via a HTML submit button, this list shows again in another page. Now, I want the option the user selected to be new selected value. I am doing this:
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
But values are appearing twice. The option the user selected is shown as the selected and still appears in the list. For example, we now have something like this:
0-50,000 (this is the selected value)
0-50,000
50,000-100,000
100,000-150,000
150,000-200,000
200,000 and above
How do I solve this?
In other page, make sure the selected option has selected="selected"
<select name='price' id='price'>\
<option value='' >Select....</option>
<option selected="selected" value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name='price' id='price'>
<option value='0-50,000' <?php echo selectBoxSelection('0-50,000', $_POST['price']);?> >0-50,000</option>
<option value='50,000-100,000' <?php echo selectBoxSelection('50,000-100,000', $_POST['price']);?> >50,000-100,000</option>
<option value='100,000-150,000' <?php echo selectBoxSelection('100,000-150,000', $_POST['price']);?> >100,000-150,000</option>
<option value='150,000-200,000' <?php echo selectBoxSelection('150,000-200,000', $_POST['price']);?> >150,000-200,000</option>
<option value='200,000 and above' <?php echo selectBoxSelection('200,000 and above', $_POST['price']);?> >200,000 and above</option>
<option value='see all' <?php echo selectBoxSelection('see all', $_POST['price']);?> >See All</option>
</select>
instead of your code,
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try like this,
<select name='price' id='price'>
<option value='' >Select....</option>
<?php foreach ($arr as $key=>$value)
{
if($key == $_POST['price'])
{
echo "<option selected value=$key>$value</option>";
unset($arr[$key]);
}
else
echo "<option value=$key>$value</option>";
}?>
</select>
This will do it :
<form name="myform" method="post" action="testsel.php">
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="0-50,000") print "selected"; ?>>0-50,000</option>
<option value='50,000-100,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="50,000-100,000") print "selected"; ?>>50,000-100,000</option>
<option value='100,000-150,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="100,000-150,000") print "selected"; ?>>100,000-150,000</option>
<option value='150,000-200,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="150,000-200,000") print "selected"; ?>>150,000-200,000</option>
<option value='200,000 and above' <?php if(isset($_POST["price"]) && $_POST["price"]=="200,000 and above") print "selected"; ?>>200,000 and above</option>
<option value='see all' <?php if(isset($_POST["price"]) && $_POST["price"]=="see all") print "selected"; ?>>See All</option>
</select>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST["price"]))
print $_POST["price"];
?>
Save this file as "testsel.php" and view. It will serve the purpose.
<select name='price' id='price'>
<option value='' >Selec</option>
<option value='0-50,000'<?php if($_POST['price']=='0-50,000'){echo "selected==selected";}?>>0-50,000</option>
<option value='50,000-100,000'<?php if($_POST['price']=='50,000-100,000'){echo "selected==selected";}?>>50,000-100,000</option>
<option value='100,000-150,000'<?php if($_POST['price']=='100,000-150,000'){echo "selected==selected";}?>>100,000-150,000</option>
<option value='150,000-200,000'<?php if($_POST['price']=='150,000-200,000'){echo "selected==selected";}?>>150,000-200,000</option>
<option value='200,000 and above'<?php if($_POST['price']=='200,000 and above'){echo "selected==selected";}?>>200,000 and above</option>
<option value='see all'<?php if($_POST['price']=='see all'){echo "selected==selected";}?>>See All</option>
</select>

Categories