I am trying to setup a subscription site that is based upon the number of people in a table.
Example: If there are less than 1000 people the cost is $Nil, if there are between 1,000 & 2,000 people, cost is $10, if there is between 2,000 & 3,000 people, cost is $20
I can output the $totalpeople no problems but it's where I want to get the $dkpsub that is causing the issues
I must be doing something wrong because I can't get it to work. Here is where I'm having troubles... Any help would be appreciated... Thanks Rog
<?php
$query = "SELECT count(id) as pcount FROM $people_table $wherestr";
$result = tng_query($query);
$row = tng_fetch_assoc( $result );
$totalpeople = $row['pcount'];
tng_free_result($result);
$dkpsub = if {$totalpeople < 1000,'Nil'};
echo "<ul><li><strong>USD$$dkpsub per annum</strong></br></li></ul>";
?>
Update: S. Imp has more detailed answer above
You should separate definition and assignment. PHP manual
<?php
$query = "SELECT count(id) as pcount FROM $people_table $wherestr";
$result = tng_query($query);
$row = tng_fetch_assoc( $result );
$totalpeople = $row['pcount'];
tng_free_result($result);
$dkpsub = 0;
if ($totalpeople < 1000) { $dkpsub ='Nil'; }
elseif ($totalpeople < 2000) { $dkpsub ='10'; }
else { $dkpsub ='20'; }
echo "<ul><li><strong>USD$$dkpsub per annum</strong></br></li></ul>";
?>
First, you should always check the result of a query for errors. If a query fails, you can get yourself into trouble if your code assumes the query always works. I'm not familiar with the tng_query function, but you might want to check into what it does if the query fails.
Secondly, this doesn't look like valid PHP, so I'm guessing you are getting a syntax error:
$dkpsub = if {$totalpeople < 1000,'Nil'};
In your case, it looks like you'll want an if/elseif/else statement -- but I wonder what will happen if $totalpeople is greater than 3000? Anyways, something like this might work up to that point:
//If there are less than 1000 people the cost is $Nil
if ($totalpeople < 1000) {
// are you sure you want to set it to $Nil?>
$cost = $Nil; // do you maybe mean NULL? or 'Nil'? or perhaps 0?
} elseif ($totalpeople >= 1000 && $totalpeople < 2000) {
// if there are between 1,000 & 2,000 people, cost is $10
$cost = 10;
} elseif ($totalpeople >= 2000 && $totalpeople < 3000){
// if there is between 2,000 & 3,000 people, cost is $20
$cost = 20;
} else {
throw new Exception("unexpected total people encountered");
}
So do think this may work?
<?php
$query = "SELECT count(id) as pcount FROM $people_table $wherestr";
$result = tng_query($query);
$row = tng_fetch_assoc( $result );
$totalpeople = $row['pcount'];
tng_free_result($result);
if ($totalpeople < 1000) {
$dkpsub = 'Nil';
} elseif ($totalpeople >= 1000 && $totalpeople < 2000) {
$dkpsub = 10;
} elseif ($totalpeople >= 2000 && $totalpeople < 3000){
$dkpsub = 15;
} else ($totalpeople > 3000){
$dkpsub = 20;
}
echo "<ul><li><strong>USD$$dkpsub per annum</strong></br></li></ul>";
?>
Related
In PHP I want to generate random numbers from 1 to 10 in a loop.
So for example:
$factor="1"; // this will be changed in another area
for ($i=0;$i<10;$i++) {
if ($factor=="1") {$output = rand(1,10);}
else if ($factor=="2") {$output = rand(1,10);}
else {$output = rand(1,10);}
}
Now to explain this - In result I want to receive 10 random numbers, but when $factor = "2", in that case I want to receive numbers from 6 to 10 more frequently as lower numbers.
It means, from 10 numbers I need to have 80% higher random numbers (it means larger than 5) and in 20% lower numbers (5 or lower).
E.g. 1,2,6,7,8,9,7,9,8,6 (1,2 are the only lower numbers = 20%, the rest are higher = 80)
If the $factor will change, then I want to change the percentage, in that case for example 40% lower numbers, 60% higher numbers.
The idea I have is to put each output in the loop to an array, then check each result and somehow calculate, if there is no 80% of larger numbers, then get random numbers again for those, but this seems to be an overkill.
Is there a simplier solution?
Let's go with the percentages you mention and first generate a random number between 1 and 100. Then the lower number, 1 to 20, have to represent outputs 1 to 5 and the higher numbers, 21 to 100, have to represent output 6 to 10. In PHP that would look like this:
function HighMoreOften()
{
$percentage = rand(1, 100);
if ($percentage <= 20) {
return rand(1, 5);
} else {
return rand(6, 10);
}
}
That should do the trick. You can also convert the percentage you got into the output, this would probably be slightly faster:
function HighMoreOften()
{
$percentage = rand(1, 100);
if ($percentage <= 20) {
return ceil($percentage / 5);
} else {
return 6 + ceil(($percentage - 20) / 20);
}
}
but personally I think the first version is easier to understand and change.
To change frequency you gonna need an array of numbers. And a sum to this direction. frequency is the relation of something between an array of things.
$t = 0;
// $factor = 1; // let's say that this means 20%
$factor = 2; // let's say that this means 40%
if ($factor === 1) {
for ($i = 1; $i <= 80; $i++) {
$t += rand(1,10);
}
for ($i = 1; $i <= 20; $i++) {
$t += rand(6,10);
}
} else if ($factor === 2) {
for ($i = 1; $i <= 60; $i++) {
$t += rand(1,10);
}
for ($i = 1; $i <= 40; $i++) {
$t += rand(6,10);
}
} else {
for ($i = 1; $i <= 100; $i++) {
$t += rand(1,10);
}
}
echo round(($t/100), 0);
Something like that! :)
I came with a very simple (maybe creepy) solution, but this works as I wanted:
echo print_r(generate("2"));
function generate($factor) {
$nums=array();
for ($i=0;$i<10;$i++) {
if ($i<$factor) {$rnd = rand(1,5);}
else {$rnd = rand(6,10);}
array_push($nums,$rnd);
}
return $nums;
}
I can also shuffle the final array results, as the lower numbers will be on the beginning always, but in my case it doesn't matter.
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);
}
i have a simple division
$order = 14 / 10; // 1
$order = 15 / 10; // 2
result automatically convert into round figure
how can i get result like
$order = 14 / 10; // 1.4
$order = 15 / 10; // 1.5
here my actual code $quantity
if (($quantity > 0.99) && ($pro_Type== "BHRF")) {
$pro_Id = $pro_Id -10;
$quantity = $quantity/10;
for ($i=1; $i <11; $i++) {
$pro_Id = $pro_Id +10;
$insert0 = mysql_query("INSERT INTO myorders1 (Product_Id,Quantity,Product_Type,Store_Id,Order_Date,Order_Time)
VALUES ('$pro_Id','$quantity','$pro_Type','$store_Id','$o_Date','$o_Time')", $connection);
}
}
Unless i'm drunk; 14/10 can't be 1 without any rounding involved
echo 14 / 10; // 1.4 as expected
Most probably your database field, Quantity, is integer and doesnt store the fraction and you assume the real number came from division.
Fiddle
I have the following code which provides the profit results of my rows depending on what Resultat_ID is set to.
$result = mysql_query('SELECT Indsats, Odds, Resultat_ID, Kamp FROM spil');
$row = mysql_fetch_assoc($result);
echo " Gevinster: ";
$Indsats = $row['Indsats'];
$Odds = str_replace(",", ".", $row['Odds']);
$sum = 0;
while($row = mysql_fetch_array( $result ))
{
$Indsats = $row['Indsats'];
$Odds = str_replace(",", ".", $row['Odds']);
if ($row['Resultat_ID'] == 1) //Win
{
$sum += $Indsats * ($Odds-1);
}
elseif ($row['Resultat_ID'] == 2) //Loss
{
$sum += $Indsats * -1;
}
elseif ($row['Resultat_ID'] == 3) //HalfWin
{
$sum += $Indsats * ($Odds-1) * 0.5;
}
elseif ($row['Resultat_ID'] == 4) //HalfLoose
{
$sum += $Indsats * -0.5;
}
}
echo $sum;
I also have a column called Tipper_ID which contains a number for each tipper and a table that contains Tipper_ID and Tipper_Name.
Besides the total profit results above, I want to get the profit results for each tipper, so basically run the above section for "all" and for each Tipper_Name in the Tipper-table and get the profit results for each part.
How do I do that ?
Danish/English Translations:
Gevinster = Gains<br>
Indsats = Effort<br>
Odds = Odds<br>
Resultat = Results<br>
Kamp = Match/Game<br>
Spil = Games<br>
Tipper = Tips
You have 2 options:
Inside your while, perform another query for each result (depending on the number of results, performance can be not that good)
Create a VIEW with the results grouped by Tipper_Name (or whatever you need) and change your query adding an INNER JOIN using the VIEW you just created. Even if this solution is a little bit more complex, it's faster!
This actually looks like a classic SQL problem:
SELECT t.TIPPER_ID, t.TIPPER_NAME,
SUM(CASE(s.Resultat_ID WHEN 1 THEN Indsats*(Odds-1) ELSE 0 END)) AS Win,
SUM(CASE(s.Resultat_ID WHEN 2 THEN Indsats*(-1) ELSE 0 END)) AS Loss,
SUM(CASE(s.Resultat_ID WHEN 3 THEN Indsats*(Odds-1)*(0.5) ELSE 0 END)) AS HalfWin,
SUM(CASE(s.Resultat_ID WHEN 4 THEN Indsats*(-0.5) ELSE 0 END)) AS HalfLoss
FROM spil AS s
LEFT JOIN TIPPERS AS t ON t.TIPPER_ID=s.TIPPER_ID
GROUP BY s.TIPPER_ID
For the total you should simply add those in the php.
I have a simple scenario but not getting it to work. I need to pull up a value for column 'fee' specified in Mysql Database according to two values.
I want to get the value for 'fee' if I specify an amount between columns 'from' and 'to'
Example: if amount is 95, the value for row 'fee' should be 0.99.
I tried to use traditional method previously like below but now I would like MYSQL query method. Please help.
if ($amount < 100) {
$fee = '0.99';
}
else if ($amount > 100 && $amount < 500) {
$fee = '1.99';
}
else if ($amount < 500 && $amount < 1000) {
$fee = '2.99';
}
else if ($amount < 1000 && $amount < 10000) {
$fee = '4.99';
}
else if ($amount > 10000) {
$fee = '9.99';
}
A Sample PHP Code:
<?
include "dbconnect.php";
$post_amount = $_POST["amount"];
$sql = "SELECT fee FROM fees WHERE from < '$post_amount' AND to > '$post_amount' LIMIT 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array( $result )) {
$fee = $row["fee"];
}
echo "Posted Amount is $post_amount and Fee is $fee";
?>
If I get your question, you need the SQL query.
You can use > and < signs:
"SELECT fee FROM tablename WHERE from < '$amount' AND to > '$amount' LIMIT 1"
Or you can use BETWEEN (How to search between columns in mysql):
"SELECT fee FROM tablename where '$amount' BETWEEN from AND to LIMIT 1"
Two more things:
You'll need to sanitize $amount variable before using it in the query
from and to are MySQL Reserve Words and should not be used as column names
MySQL has an awesome operator called BETWEEN:
"SELECT `fee` FROM `table` WHERE ".floatval($amount)." BETWEEN `from` AND `to`"
You just had problem in > and < all other things are fine in ur code there no need for other function to check it from database table.
I have checked it with this :
$amount = 250;
if ($amount < 100) {
$fee = '0.99';
} else if ($amount > 100 && $amount < 500) {
$fee = '1.99';
} else if ($amount > 500 && $amount < 1000) {
$fee = '2.99';
} else if ($amount > 1000 && $amount < 10000) {
$fee = '4.99';
} else if ($amount > 10000) {
$fee = '9.99';
}
echo $fee;
Its works fine.