Can't figure out why this isn't working? [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
<?php
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
i f ($totalqty == 0) {
echo "You did not enter anything in the boxes on the previous page.";
}
else {
echo "<p>Order processed at ".date('H:i, jS F Y')."</p><br /><br />";
echo '<p>Your order is as follows:</p>';
echo $tireqty.' tires<br />';
echo $oilqty.' bottles of oil<br />';
echo $sparkqty.' spark plugs<br />';
echo "Items ordered: ".$totalqty."<br />";
if ($tireqty < 10) {
$discount = 0;
}
elseif ($tireqty >= 10) && ($tireqty <= 49) {
$discount = 0.05;
}
elseif ($tireqty >= 50) && ($tireqty <= 99) {
$discount = 0.10;
}
elseif ($tireqty >= 100) {
$discount = 0.15;
}
$totalamount = ($tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE) * (1+$discount);
echo "Subtotal (Discount applied here): $".number_format($totalamount, 2)."<br />";
$taxrate = 0.10;
$totalamount = $totalamount * (1+ $taxrate);
echo "Total including Tax: $".number_format($totalamount,2)."<br />";
}
?>
Any help would be appreciated.

I am a beginner so I might wrong but here elseif ($tireqty >= 10) && ($tireqty <= 49) I would use and extra bracket:
elseif (($tireqty >= 10) && ($tireqty <= 49)) {
...
}
Hope this is it:)

This is a terrible question but here goes... without knowing what's "not working" I can only guess:
i f ($totalqty == 0) {
That's a syntax error. You probably meant:
if ($totalqty == 0) {
Likewise, here:
if ($tireqty < 10) {
$discount = 0;
}
elseif ($tireqty >= 10) && ($tireqty <= 49) {
$discount = 0.05;
}
elseif ($tireqty >= 50) && ($tireqty <= 99) {
$discount = 0.10;
}
elseif ($tireqty >= 100) {
$discount = 0.15;
}
The entire conditions need to be enclosed in parens:
elseif (($tireqty >= 10) && ($tireqty <= 49)) {
$discount = 0.05;
}
elseif (($tireqty >= 50) && ($tireqty <= 99)) {
$discount = 0.10;
}
elseif ($tireqty >= 100) {
$discount = 0.15;
}
It's possible there's a lot more things wrong here. Please edit your question and describe specifically what is not working, and what you've done to fix it, and what you need help with.

Related

Finding certain percentages from function

I'm making a grade calculator where the user inputs their earned points, points possible, then its weight within the class. Afterwards it then calculates and prints the percentage earned for each portion along with its weight. Finally, it prints your total calculated score along with the letter grade. I'm able to print the very last line completely, along with each categories' weight, but I can't print out the percentage earned in each category
<?php
$par_Earned = $_POST['earnedParticipation'];
$par_Possible = $_POST['maxParticipation'];
$par_Weight = $_POST['weightParticipation'];
$q_Earned = $_POST['earnedQuiz'];
$q_Possible = $_POST['maxQuiz'];
$q_Weight = $_POST['weightQuiz'];
$l_Earned = $_POST['earnedLab'];
$l_Possible = $_POST['maxLab'];
$l_Weight = $_POST['weightLab'];
$p_Earned = $_POST['earnedPracticum'];
$p_Possible = $_POST['maxPracticum'];
$p_Weight = $_POST['weightPracticum'];
function percentage($a,$b){
$percent = ($a/$b)*100;
return $percent;
}
function weightValue($c,$d,$e){
$weight = (percentage($c,$d) * $e)/100 ;
return $weight;
}
$parWeight = weightValue($par_Earned,$par_Possible,$par_Weight);
$quizWeight = weightValue($q_Earned,$q_Possible,$q_Weight);
$labWeight = weightValue($l_Earned,$l_Possible,$l_Weight);
$pracWeight = weightValue($p_Earned,$p_Possible,$p_Weight);
$total = $parWeight+$quizWeight+$labWeight+$pracWeight;
function lettergrade($total) {
if ($total >= 95) {
$lettergrade = "A+";
}
else if ($total < 95 && $total >= 90 ) {
$lettergrade = "A";
}
else if ($total < 90 && $total >= 85) {
$lettergrade = "B+" ;
}
else if ($total < 85 && $total >= 80) {
$lettergrade = "B" ;
}
else if ($total < 80 && $total >= 75) {
$lettergrade = "C+" ;
}
else if ($total < 75 && $total >= 70) {
$lettergrade = "C" ;
}
else if ($total < 70 && $total >= 60) {
$lettergrade ="D" ;
}
else if ($total < 60 && $total >= 0) {
$lettergrade = "F" ;
}
else {
$lettergrade = "Grade is not valid";
}
return $lettergrade;
}
echo "<p>You earned a X with a weighted value of " .$par_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$q_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$l_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$p_Weight. "% </p>";
echo "<p><b>Your final grade is " .$total. "%, which is a ".lettergrade($total)."</b></p>";
?>

Why is this integer considered a float?

I have created the following script for determining which page numbers to display for navigation on my website.
// query the database
$statement = $connect->prepare("SELECT COUNT(report_number) FROM reports");
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
$rowCount = $result["COUNT(report_number)"];
$pgCount = ceil($rowCount / 10);
// fetch the corresponding entries from the database
if (!isset($pgParamArray["page"]) || $pgParamArray["page"] === $pgCount) {
$pgParamArray["page"] = $pgCount;
$sql = "SELECT * FROM reports ORDER BY report_number DESC LIMIT 10";
} elseif (isset($pgParamArray["page"])) {
$offset = ($pgCount - $pgParamArray["page"]) * 10;
$sql = "SELECT * FROM reports ORDER BY report_number DESC LIMIT " . $offset . ",10";
}
// calculate and display the appropriate page numbers
$upperDiff = $pgCount - $pgParamArray["page"];
$lowerDiff = $pgParamArray["page"] - 1;
for ($i = 0; $i < 5; $i++) {
$pgNo = 0;
if ($pgParamArray["page"]+2 <= $pgCount && $pgParamArray["page"]-2 >= 1) {
$pgNo = $pgParamArray["page"] + ($i - 2);
} elseif ($pgCount < 5) {
$pgNo = $i+1 <= $pgCount ? $i+1 : 0;
} elseif ($upperDiff < $lowerDiff && $upperDiff < 2 && $upperDiff >= 0) {
$pgNo = ($pgCount - 4) + $i;
} elseif ($lowerDiff < $upperDiff && $lowerDiff < 2 && $lowerDiff >= 0) {
$pgNo = $i + 1;
} else {
$pgNo = ($pgCount - 4) + $i;
}
if ($pgNo > 0) {
if ($pgNo !== $pgParamArray["page"]) {
$pgNoArray[] = ""
. "<li><div><a href='http://www.somewebsite.com/index.php/?page="
. $pgNo
. "'>"
. $pgNo
. "</a></div></li>";
} else {
$pgNoArray[] = ""
. "<li><div style='background:#333'>"
. $pgNo
. "</div></li>";
}
}
}
When I set $pgParamArray["page"] = $pgCount = ceil($rowCount/10), I have noticed that the foremost variable is still treated as a float. My question is why is $pgParamArray["page"] of type float even though ceil($rowCount/10) obviously returns an integer?
PHP's ceil returns a float according to their documentation.
Their rationale:
value rounded up to the next highest integer. The return value of ceil() is still of type float as the value range of float is usually bigger than that of integer.
This isn't just PHP that does this - C++ and Java both return floating point values for their ceiling functions.

MySQL/PHP - page number pagination Only show 10 pages at times

I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>
function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}
You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}

Use PHP while loop to create advance filter

I have a problem and that is I want to create a link on a website like people can click the link to show certain products only depending on percentage. like for example, i have a column in my database with discount percentage and it will show min discount and max discount. assuming we have min and max discount. $min=12 and $max=94; and I want to put them in links to show only products with certain discounts only like filtering. below is the example of the link.
<a href="#">12% to 20%</a
21% to 30%
31% to 40% and so on until it reaches
81% to 90% and the last will be
91% to 94%
smallest and largest numbers will be coming from a column from database and they can change frequently. i came up with solution and its working fine but my code is too long and its like I took to many steps which could be done in few lines of code. I have pasted my working code below but I am sure this can be reduced to few lines of code.
$catsql25 = "SELECT MAX(down_percentage) as largest FROM hot_deals";
$catquery25 = mysqli_query($conn, $catsql25);
while ($row25 = mysqli_fetch_array($catquery25, MYSQLI_ASSOC)){
$largest_number = $row25['largest'];
}
$catsql26 = "SELECT MIN(down_percentage) as smallest FROM hot_deals";
$catquery26 = mysqli_query($conn, $catsql26);
while ($row26 = mysqli_fetch_array($catquery26, MYSQLI_ASSOC)){
$smallest_number = $row26['smallest'];
}
$array_tens = array(10,20,30,40,50,60,70,80,90,100);
foreach ($array_tens as $value){
if(($value - $smallest_number <= 10) && ($value - $smallest_number > 0)){
echo '<a href="/exp.php?fst='.$smallest_number.'&lst='.$value.'"><div class="lfmen2">';
echo $smallest_number." to ".$value."</div></a>";
$next_num = $value + 1;
$next_ten = 9;
$stop_num = floor($largest_number / 10);
$stop_num2 = $stop_num * 10;
//echo $stop_num2.'<br>';
$num_rounds = $stop_num2 - $value;
$num_rounds2 = $num_rounds / 10;
//echo $num_rounds2;
for ($i = 1; $i <= $num_rounds2; $i++){
$end_num = $next_num + $next_ten;
echo '<a href="/exp.php?fst='.$next_num.'&lst='.$end_num.'"><div class="lfmen2">';
echo $next_num;
echo " to ";
echo $end_num;
echo "</div></a>";
$next_num += 10;
$end_num += 10;
}
}
}
foreach ($array_tens as $value2){
if(($largest_number - $value2 < 10) && ($largest_number - $value2 > 0)){
$lsst = $value2 + 1;
if($lsst != $largest_number){
echo '<div class="lfmen2">'.$lsst." to ".$largest_number."</div>";
}
elseif($lsst == $largest_number){
echo '<div class="lfmen2">'.$largest_number.'</div>';
}
}
}
I know its all mess but..
Thanks.
First thing you could do is only one SQL Query :
$catsql = "SELECT MAX(down_percentage) as largest, MIN(down_percentage) as smallest FROM hot_deals";
And then you'll need only one loop :
$catquery = mysqli_query($conn, $catsql);
while ($row = mysqli_fetch_array($catquery, MYSQLI_ASSOC)){
$largest_number = $row['largest'];
$smallest_number = $row['smalest'];
}
After that, you could make only one foreach loop. The two "if" conditions could be in the same loop :
foreach ($array_tens as $value) {
if (($value - $smallest_number <= 10) && ($value - $smallest_number > 0)) {
echo '<a href="/exp.php?fst='.$smallest_number.'&lst='.$value.'"><div class="lfmen2">';
echo $smallest_number." to ".$value."</div></a>";
$next_num = $value + 1;
$next_ten = 9;
$stop_num = floor($largest_number / 10);
$stop_num2 = $stop_num * 10;
//echo $stop_num2.'<br>';
$num_rounds = $stop_num2 - $value;
$num_rounds2 = $num_rounds / 10;
//echo $num_rounds2;
for ($i = 1; $i <= $num_rounds2; $i++) {
$end_num = $next_num + $next_ten;
echo '<a href="/exp.php?fst='.$next_num.'&lst='.$end_num.'"><div class="lfmen2">';
echo $next_num;
echo " to ";
echo $end_num;
echo "</div></a>";
$next_num += 10;
$end_num += 10;
}
}
if (($largest_number - $value < 10) && ($largest_number - $value > 0)) {
$lsst = $value + 1;
if ($lsst != $largest_number) {
echo '<div class="lfmen2">'.$lsst." to ".$largest_number."</div>";
} elseif ($lsst == $largest_number) {
echo '<div class="lfmen2">'.$largest_number.'</div>';
}
}
}
To make it more readable, you could also comment your code to know what do what.
This and a good indentation and you're right.
Hope it helps.

Prime numbers up to a certain number

here is the problem, i need to find prime numbers up to a certain number and here is my code:
$a = 56;
for($i = 2; $i<=$a; $i++)
{
if($i == 2)
{
echo "2</br>";
}
if($i == 3)
{
echo "3</br>";
}
if($i == 5)
{
echo "5</br>";
}
if($i == 7)
{
echo "7</br>";
}
for($j =3; $j <= ceil($i/2); $j = $j + 2)
{
if($i % 2 == 0)
{
break;
}
if($i % 3 == 0)
{
break;
}
if($i % 5 == 0)
{
break;
}
if($i % 7 == 0)
{
break;
}
else
{
echo "$i</br>";
break;
}
}
}
It works fine, but it kinda seems like a brute force algorithm, doesnt it? Is there any other way to do this?
Thanks for help!!!
Suppose x is the limit (till which you want prime number)..
for($n=2;$n<=$x;$n++)
{
$i=2;
while($i<=$n-1)
{
if($n % $i == 0)
break;
$i++;
}
if($i==$num)
echo $n; // $n is the prime number...
}

Categories