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.
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 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
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Can someone tell me what is wrong with my code? PHP update my Data keeps Appending
<?php
foreach($_SESSION["product"] as $index => $product)
{
if ($_SESSION["product"][$index]["item_id"] == $_POST["item-id"])
{
$found = 1;
$target_index = $index + 1;
}
}
if (true)
{
array_push($_SESSION["product"], ["item_id" => $_POST["item-id"], "item_qty" => $_POST["item-qty"]]);
}
else
{
$qty = strval(($_SESSION["product"][$target_index]["item_qty"]) + $_POST["item-qty"]);
$_SESSION["product"][$target_index]["item_qty"] = $qty;
$qty = 0;
}
}
If you are asking why it always appends to the array instead of executing the else block, it looks like you have a condition missing in the line that says:
if(true)
This will always evaluate to true no matter and as a result it will always append to your array.
You probably want to put some sort of condition there
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.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Im wondering if i can do and/or together. I'm trying to make a code that checks if both variables are wrong or if only one of them is wrong and the other one is right then I want to show a message like: "one of the "variables" are wrong.
Can anyone help me out here?
In addition to Tim Whites answer, if you don't want it nested, you could also use:
if($a == false AND $b == false) { echo "Both variables are false"; }
elseif($a == false OR $b == false) { echo "One variable is false"; }
if ($a==false OR $b==false) {
if($a==false AND $b==false) { echo "Both variables are false"; }
else { echo "On of the variables is false"; }
}
Something like that will do.
You could just use multiple IF statements, to check if 1 variable is wrong or both are wrong. Like:
if ($variable1==false && $variable2==false){
}
else{
if ($variable1==false || $variable2==false){
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I check that a PHP array is not empty?
// If $columns array is not empty...
foreach ($columns as $column) {
echo $column;
}
Why bother with functions? Empty arrays will simply return false:
if ($array) {
// array is not empty
}
Very simple: using empty() function.
if (!empty($columns)){
foreach ($columns as $column) {
echo $column;
}
}
Also can be used with other types than array.
One way is to use count()
if (count($columns) > 0) {
foreach ($columns as $column) {
echo $column;
}
}