Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I need to use 2 $_POST. Here's the code but it's not working.
if($_POST['grade'] == 'G1') && ($_POST['district'] == 'D1')
Thanks in advance!
In my opinion, you should also check that $_POST is not empty before trying to access any key inside
<?php
error_reporting(E_ALL);
if (!empty($_POST)) {
if ($_POST['grade'] == 'G1' && $_POST['district'] == 'D1') {
#condition fulfilled.
} else {
print_r($_POST); #debug $_POST and see where you went wrong
exit;
}
} else {
die('POST GLOBAL IS EMPTY');
}
Try by this
if($_POST['grade'] == 'G1' && $_POST['district'] == 'D1'){
//Both Founded
}else{
//Not Founded
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I've got an if statement like
if ( $id == '2781' || $id == '1947' || $id == '516' || $id == '3200' || $id == '237' || $id == '3207' || $id == '3205' || $id == '2647' || $id == '516' || $id = '4571' ) {
// Do something
}
But the if statement appears to be failing because of the number of conditions (as in, it always does something, not just on specified IDs). I can remove a few of them and it works as expected. How would I fix this?
Try this instead, in_array is perfect for that kind of situation:
if (in_array((int)$id, array(2781, 1947, 516, 3200, 237, 3207, 3205, 2647, 516, 4571))) {
// Do something
}
Also, you had a missing '=' in your code.
You have a typo:
... || $id = '4571' ) ...
is assigning value of '4571' to $id. You have to use the comparison operator:
... || $id == '4571' ) ...
That's why I like to use in_array:
$availableIds = ['2781', '1947', '516', '3200', '237', '3207', '3205', '2647', '516', '4571'];
if(in_array($id, $availableIds)) {
...
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My php code is:
<?php
class Product {
var $product_name;
var $retailer;
function __constructor($product, $retailer) {
$this->product_name = $product;
$this->retailer = $retailer;
}
function getProduct() {
return $this->product_name;
}
}
$product_arr = array();
for ($f = 0; $f < 100; $f++) {
array_push($product_arr, new Product("asd", "xcxcxc"));
}
print_r($product_arr);
?>
The code is pretty simple, I have a class called "Product", I build an array consists of 100 Product object, but when I tried to print the array, I found out all the object's product_name and retailer fields are empty. Not sure why this happens.
Wrong name:
function __constructor($product, $retailer) {
^^^
PHP's standard constructor name is simply __construct (no or). So you never actually called a constructor, which means your variable assignments never executed.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to check if user is logged on site or not, so I made a function:
function isLogged()
{
if($_SESSION['logged']=='1')
{
return true;
}
elseif($_SESSION['logged']!=='1')
{
return false;
}
}
On my site Im trying to induce function like this:
if('isLogged')
{
echo 'yes';
}
else
{
echo 'no';
}
Even if $_SESSION['logged'] is set to 0 (I just checked by echo value), it returns "yes". What is wrong with this code?
You have to actual call the function, not check if the string returns true:
if(isLogged())
{
echo 'yes';
}
else
{
echo 'no';
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm fairly new to PHP so I was wondering whether or not you could have to 'commands' in an if statement, like:
$value = 1;
$values = 2;
if ($value == '1' & $values == '2') {
do something
} else {
do something else
}
Would that work? I probably have the syntax wrong however you get the gist. Haha
Thanks for the help.
if you are new to use conditional operators read first before implement.
http://php.net/manual/en/language.operators.comparison.php
$value = 1;
$values = 2;
if ($value == '1' && $values == '2') {
// do something
} else {
// do something else
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a $_POST which contain following data
$_POST['survey_que1']
$_POST['survey_que2']
$_POST['survey_que3']
....
so on.
I have to get a count of occurrence of survey_que from $_POST.
Any Ideas please.
Restructure your data.
<input type="text" name="survey_que[]" />
This will result in $_POST['survey_que'] being an array of passed values, which you can easily use count() to get the total count.
try this
$i=0;
foreach ($_POST as $name => $val)
{
if(strpos($name,'survey_que') !== false && $name != "survey_que")
$i++;
}
echo "No of times survey_que occured in $_POST[] is ".$i;
EDIT
#shri in your code if survey_que2 is not set and survey_que3 is set then your code fails.
So try this workaround which covers all worst cases also
$i=0;
foreach ($_POST as $name => $val)
{
if(strpos($name,'survey_que') !== false && $name != "survey_que")
{
$key=explode("survey_que",$name);
if(sizeof($key) == 2 && is_numeric($key[1]) && $key[0] == "")
{
$i++;
}
}
}
echo "No of times survey_que occured in $_POST[] is ".$i;
Serialize $_POST and use "substr_count" for counting ocurrences
$how_many = substr_count(serialize($_POST), $search_string));
It's the easiest way I find but will count this text even if it's not a $_POST key value
This is working for me exactly i want.
$i = 1;
while (isset($_POST['survey_que' . $i])) {
$i++;
}
echo $i-1;
Thanks everyone for quick support.