PHP update my Data keeps Appending whats wrong with my code [closed] - php

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

Related

Using '&&' inside foreach? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am looking for smart solution, maybe you could help me out. Currently I am doing an own Authentication (Separate Class) system for my Webshop project. My problem is, that I need conditional statement inside foreach loop, to return the code (see below). Any suggestions?
My code currently look like this
public function regiAuth($email, $password, $firstname, $lastname)
{
$authContainer = [$email, $password, $firstname, $lastname];
foreach ($authContainer as $a) {
return !empty($_POST[$a]);
}
}
And I want to result this (With &&)
return !empty($_POST[$email]) && !empty($_POST[$password]) &&
!empty($_POST[$firstname]) && !empty($_POST[$lastname])
I believe you could do simply by do
foreach ($authContainer as $a) {
if (empty($_POST[$a])
return false;
}
return true;
instead of checking if all of them are full, you look if there is at least one empty.
it is a good practice to stop iteration if you find one element that is not as expected, imagine if you had an array of hundreads of assertions to do.
making an full if statement would look like this, here it checks if the $_POST are not empty. && means that both have to true or false
foreach ($authContainer as $a) {
if((!$_POST[$email]) && (!$_POST[$password])){
return false;
}
}
return true;

Loop until variable is isset then excute a code [closed]

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 6 years ago.
Improve this question
I try to do a while loop but something is missing I need to loop until variable is isset then execute a code something like dis :
$counter=o;
while(null!==($var)){
$counter ++;
}
if (isset($var)){
excute code ....
}
You mean like this?
$counter = 0;
while (!isset($var)) {
$counter ++;
echo $counter, PHP_EOL;
if ($counter == 10) {
$var = true;
}
}
echo 'Done', PHP_EOL;
I suggest you don't use the existance of a variable to control your logic flow - using something like a break command to kill your potentially infinite loop might be better, for example:
while (true) {
// do something
if ($someCondition) {
break;
}
}

Compare values in a single array and return the key in php [closed]

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 7 years ago.
Improve this question
I have an array like this:
$age=array("Peter"=>43,"Ben"=>67); .
The array contains only two key value pairs. First i need to check if the values of these two keys are same. If same then it returns the key of these two value, otherwise return false. So here value 43 and 67 are not same so it should return false. If the two values would be same like this :
$age=array("Peter"=>43,"Ben"=>43); .
It should return the key "Peter" and key "Ben" and maybe store the keys in another array The reason is to find if two people are of same age if same age then i would like to do several other things. I will appreciate the help.
Just get the unique values and see if there is only 1:
if(count(array_unique($age)) === 1) {
return array_keys($age);
} else {
return false;
}
Because I was bored, here are two others.
Assuming only 2 elements:
if(($v = array_values($age)) && $v[0] === $v[1]) {
return array_keys($age);
} else {
return false;
}
Also, should work with multiples:
if((array_sum($age) % count($age)) === 0) {
return array_keys($age);
} else {
return false;
}

String count in $_POST [closed]

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.

Error on line that doesnt exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.

Categories