I am working on a project and then I got this problem. Here is the scenario, when I insert in database, it must not insert if the value is zero ,,but then when I check my database ,there are zeros being inserted, I don't know where I go wrong or I just missed the trapping that if the value is equal zero then it must not insert.
Here is the code :
$ref_array = explode(',' , $ref_number);
$po_array = explode(',' , $po_number);
$inv_array = explode(',' , $inv_number);
$asn_array = explode(',' , $asn_number);
$adj_array = explode(',' , $adj_number);
$amount_array = explode(',' , $amount);
// count the number of po,invoice,asn and adj
if(count($po_array) != count($ref_array) || count($inv_array) != count($ref_array) || count($asn_array) != count($ref_array) || count($adj_array) != count($ref_array) || count($ref_array) != count($amount_array)){
foreach ($ref_array as $i => $ref_num){
$po_num = isset($po_array[$i]) ? $po_array[$i] : '' ; //leave blank there is no $po_array[$i]
$inv_num = isset($inv_array[$i]) ? $inv_array[$i] : '';
$asn_num = isset($asn_array[$i]) ? $asn_array[$i] : '' ;
$adj_num = isset($adj_array[$i]) ? $adj_array[$i] : '' ;
$amount_num = isset($amount_array[$i])? $amount_array[$i] : '';
if(intval($ref_num) != 0 ){
$conn->query ("INSERT INTO transaction_detail (`transaction_id`,`ref_number`,`po_number`,`inv_number`,`asn_number`,`adj_number`,`amount`)
VALUES ('$transaction_id','$ref_num','$po_num','$inv_num','$asn_num','$adj_num','$amount_num') " );
}
}
}
Can somebody help me?
You just remove intval form your condition because it is use to Get the integer value of a variable.
Second check $ref_num variable is greater then zero
you condition would be
if(($ref_num) >0 ){
Also read intval()
Check the transaction_detail table whether you have put default value as 0 for po_number field.
Related
I have two bit strings, each four bits long.
e.g.,
$role1BitString = '1010';
$role2BitString = '1001';
I want to OR them, so that in this case the result would be:
$bitString = '1011';
Here is the code that I have, but it calls substr eight times.
$selectBit = (substr($role1BitString, RBAC::SELECT_BIT, 1) == '1' || substr($role2BitString, RBAC::SELECT_BIT, 1) == '1') ? '1' : '0' ;
$insertBit = (substr($role1BitString, RBAC::INSERT_BIT, 1) == '1' || substr($role2BitString, RBAC::INSERT_BIT, 1) == '1') ? '1' : '0' ;
$updateBit = (substr($role1BitString, RBAC::UPDATE_BIT, 1) == '1' || substr($role2BitString, RBAC::UPDATE_BIT, 1) == '1') ? '1' : '0' ;
$deleteBit = (substr($role1BitString, RBAC::DELETE_BIT, 1) == '1' || substr($role2BitString, RBAC::DELETE_BIT, 1) == '1') ? '1' : '0' ;
$bitString = $selectBit . $insertBit . $updateBit . $deleteBit;
This code will be executed dozens of times with every user operation. What is the most performant way of doing this?
using an actual int with bitshifting and using the bitwise or operation
use intval($string, 2) to convert your binary string to an int and then use | to do the operation
Just use PHP's bitwise operator |:
$bitString = $role1BitString | $role2BitString;
I am trying to do:
if(x != 1 || 2) echo 'okay';
With my code here:
if($_POST["timezone"] != ("Pacific/Midway" || "America/Adak" || "Etc/GMT+10" || "Pacific/Marquesas")) {
$timezone_error = 'Invalid timezone';
}
Whereas I put in information that did not equal, and $timezone_error was still not set, what is the proper OR operator that I should be using, or is this possible at all? I would rather not write $_POST['x'] != 1, $_POST['x'] != 2 all out separately as this is quite a long list.
what you want is something like this
$array = array("Pacific/Midway" , "America/Adak" , "Etc/GMT+10" , "Pacific/Marquesas");
if (!in_array($_POST["timezone"], $array){
$timezone_error = 'Invalid timezone';
}
Correct format would be:
if(x!= 1 || x!=2) echo 'okay';
In order to fill a tab with lots of values, I would like not to display values which are equal to '0'.
For example, instead of having :
Test USER 1 0 1 0
Sample USER 1 0 1 0
I'd like to have :
Test USER 1 1
Sample USER 1 1
I display my values using printf("<td>%d</td>", $value);
I've even tried printf("<td>%0d</td>", $dl_ends[0]);
Thanks
<td><?php $value ? printf("%d", $value) : ''; ?></td>
There are two ways to solve my problem :
printf("<td>%s</td>", $value != 0 ? $value : ''); which avoid number formatting, or
$value != 0 ? printf("<td>%d</td>", $value) : printf("<td></td>"); which is longer but more precise.
Try this
printf("<td>%d</td>", $value != 0 ? $value : '');
EDIT
If you also need to catch empty strings, you can use the following code
printf("<td>%d</td>", $value != 0 && $value != '' ? $value : '');
I have encountered a logical hurdle!
I have a page with 6 actions on. when you click an action, it simply returns a true or false.
Each action has its own chance of success, and the true /false is based on whether a random generated number is higher or lower than the actions success value.
the chance values are stored in the database like: 10-20-30-40-50-60
I call these values using explode and insert them using implode.
What I want to achieve is to change these chances values to either 1 more, 1 less or the same (upto a max of 65), each time an action is clicked and the form submitted,
This is what I have attempted but to no avail.
<?PHP
//CHANCE 0 (action 1)
if ($chance[0] = 0 ){
$chance[0] = rand(0,1);
}elseif ($chance[0] = 1 ){
$chance[0] = rand(1,2);
}elseif ($chance[0] >= 2 ){
$chance[0] = $chance[0] + rand(-1,1) ;
}elseif ($chance[0] >= 65 ){
$chance[0] = 65;
}
//CHANCE 1 (action 2)
if ($chance[1] = 0 ){
$chance[1] = rand(0,1);
}elseif ($chance[1] = 1 ){
$chance[1] = rand(1,2);
}elseif ($chance[1] >= 2 ){
$chance[1] = $chance[0] + rand(-1,1) ;
}elseif ($chance[1] >= 65 ){
$chance[1] = 65;
}
etc.. for all chance[0] to chance[5]
then,
$chancearray = array($chance[0], $chance[1], $chance[2], $chance[3], $chance[4], $chance[5]);
$newchancearray = implode("-",$chancearray);
?>
and insert it into the database.
can anyone help me with the logic for adding -1, 0 or 1 to the chance values after submitting the form?
at the moment its changing all values to 2 ??
so if chance was 10-20-30-40-50-60, once submitted, it becomes 2-2-2-2-2-2.
help? :(
change the = to == at the boolean expressions used on IF's
instead of
if ($chance[1] = 0 ){
should be
if ($chance[1] == 0 ){
You know about "=="?
The code is wrong
I'm creating a very simple PHP file that searches a table in my SQLite database.
I want it to filter my table according to 2 parameters, min and max, but I want to be able to specify one parameter, both or even none. How do I translate that into a SQL query?
<?php
$db = new PDO('sqlite:database.db');
$min = $_GET['min'];
$max = $_GET['max'];
if($min == '') $min = -1;
if($max == '') $max = -1;
$res = $db->prepare('SELECT * FROM Roteiro WHERE ...');
$res->execute(array(...));
$test = $res->fetchAll();
print_r($test);
?>
where (? > a or ? is null)
and (? = foo or ? is null)
-- etc...
The trick is let the condition be satisfied if the value is null. Make sure you pass type null, not some empty string or other falsy value.
The query optimizer should optimize these extra conditions away no problem because its easy for it to see they never vary per row.
an equivalent way to say it
where (? is not null and ? > a)
and (? is not null and ? = foo)
but I prefer the first way.
Youll need to build the statement dynamically:
$db = new PDO('sqlite:database.db');
$min = isset($_GET['min']) && !empty($_GET['min']) ? (integer) $_GET['min'] : null;
$max = isset($_GET['max']) && !empty($_GET['max']) ? (integer) $_GET['max'] : null;
$base = 'SELECT * FROM Roteiro';
$params = array();
if(null != $min && null != $max) {
// im using BETWEEN here just for simplicity int he example,
// but you could (should?) use separate statements with
// typical <,> comparison ops.
$base .= ' WHERE the_column BETWEEN :min AND :max';
$params[':min'] = $min;
$params[':max'] = $max;
} elseif(null !== $max) {
$base .= ' WHERE the_column < :max';
$params[':max'] = $max;
} elseif(null != $min) {
$base .= ' WHERE the_column > :min';
$params[':min'] = $min;
}
$db->prepare($base);
$db->execute($params);
You'll want to dynamically create the query from the information you have.
So, lets say you want to filter some table by a optional minimal and an optional maximal value.
SELECT some_row
FROM some_table
WHERE filter_row < [max]
AND filter_row > [min]
An easy way to do this would be to simply store the part of your SQL-query that filters by maximum and minimum and append it to the query when needed/defined:
$sql = "SELECT some_row
FROM some_table ";
if (isset($max) && isset($min)){
$sql .= "WHERE filter_row < ?
AND filter_row > ?";
} else if (isset($max) ){
$sql .= "WHERE filter_row < ?";
} else if (isset($min) ){
$sql .= "WHERE filter_row > ?";
}
if ($stmt = $mysqli->prepare($sql)) {
// bind parameters according to the given/available
// parameters and execute
}
I'm not sure if the query will work since I have nothing to test it here, but the idea should be clear.
This is just a very simple solution which will get very large and ugly with a growing list of optional parameters. So, if you need it more often and with more parameters, consider using a "Query Builder".