Home > Blockchain >  PHP Defining a variable based on calculations using another variable
PHP Defining a variable based on calculations using another variable

Time:03-09

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>";
?>

CodePudding user response:

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>";
?>

CodePudding user response:

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");
}

CodePudding user response:

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>";
?>

CodePudding user response:

Solved... I needed to add "0" instead of '0' to get it working... Many thanks to all who helped!

  •  Tags:  
  • php
  • Related