php foreach error first line [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I always have error for first array in table.
foreach ($status_lines as $status_line) {
$xxx [] = $status_line -> status ;
}
if (count(array_unique($xxx)) == 1 && end($xxx) == 'REJECTED') { ?>
<b class="text-gray"> N / A </b>
<?php }
elseif (count(array_unique($xxx)) == 1 && end($xxx) == 'NOT APPROVED') { ?>
<b class="text-gray"> N / A </b>
<?php }
it resulting : Message: Undefined variable: xxx
but for the second line to the end in table is OK ...

Your variable $xxx has been defined within your foreach block. It is not defined anywhere else.
Define it outside the block as a global variable:
$xxx = array();
Then continue your foreach loop as follows:
foreach ($status_lines as $status_line) {
$xxx[] = $status_line -> status ;
}
...

Define it before use as
$xxx = array();
foreach ($status_lines as $status_line) {
$xxx[] = $status_line -> status ;
}
If you don't declare a new array, and the data that creates / updates the array fails for any reason, then any future code that tries to use the array will warning because the array doesn't exist.
For example, foreach() will throw an error if the array was not declared and no values were added to it. However, no errors will occur if the array is simply empty, as would be the case had you declared it.

Related

Message: Undefined variable: beauty_detail in CODEIGNITER [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
I have a form where user can select either book or beauty radio button. Code is working fine on book but showing an error on beauty. Code is the same except data fetch from database is different. I have tried but still stuck.
ERROR
A PHP Error was encountered Severity: Notice
Message: Undefined variable: beauty_detail
Filename: controllers/Welcome.php
Line Number: 87
if($result == 0){
echo "no recommendation";
} else{
foreach($result as $key=>$value){
$q = $this->mymodel->fetchBeautydetail($key);
foreach($q as $val){
$beauty_detail[$val->user_id]['product_id'] = $val->product_id;
$beauty_detail[$val->user_id]['product_rating'] = $val->rating;
}
}
(line number: 87) $this->load->view('beauty_dashboard', ['beauty_detail'=>$beauty_detail]);
}
Problem is scope.
Try following. (Declaring beauty_detail out of foreach)
$beauty_detail;
foreach($result as $key=>$value){
$q = $this->mymodel->fetchBeautydetail($key);
foreach($q as $val){
$beauty_detail[$val->user_id]['product_id'] = $val->product_id;
$beauty_detail[$val->user_id]['product_rating'] = $val->rating;
}
}
$this->load->view('beauty_dashboard', ['beauty_detail'=>$beauty_detail]);
}

submit data and it show Undefined offset: 3c I used forloop [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
When I am Try To use A For-loop in my controller in store function to submit a data
public function store(Request $request)
{
$tender = new TenderMaster;
$tender->bid_name = $request->bid_name;
$tender->bid_year = $request->bid_year;
$tender->shipping_mode = $request->shipping_mode;
$tender->start_date = $request->start_date;
$tender->end_date = $request->end_date;
$explodeval = explode(",",$request->origin_port);
$tender->freight_forwarder = $request->freight_forwarder;
$tender->save();
for($i=0;$i<=count($explodeval);$i++){
$tender->airPorts()->attach($explodeval[$i]);
}
return back();
}
Simple arrays start at index 0 and end at index count()-1
$my_array=['foo','bar','baz'];
// > 3
echo count($my_array)
// > 'foo'
echo $my_array[0]
// > 'baz'
echo $my_array[2]
// error
echo $my_array[3]
Try this:
for($i=0; $i<count($explodeval); $i++){
$tender->airPorts()->attach($explodeval[$i]);
}
Your error is because you used the condition $i <= count(..., which means that if count($array) == 3 you will get a loop that executes 0, 1, 2, 3. That's 4 elements instead of 3.
You don't have 4 elements in the array, and you don't have an index $array[3].
You can try reading up on php documentation: https://www.php.net/manual/en/control-structures.for.php

PHP 'illegal offset' [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I have a simple PHP blackjack script that I seem to be getting errors in.
The part of code causing the problem is this;
function evaluateHand($hand) {
global $faces;
$value = 0;
foreach ($hand as $card) {
if ($value > 11 && $card['face'] == 'a') {
$value = $value + 1;
}
else {
$value = intval($value) + intval($faces[$card['face']]); <----- error
}
}
return $value;
}
The error is "Warning: Illegal offset 'face'" on the line I've pointed to above.
What's causing this? Or how I could fix it?
Illegal offset means you are requesting an array key that doesn't exist. In this case, the array in $card has no key face when the error is thrown.

Undefined index variable in $_REQUEST in PHP, getting a boolean value when using ISSET() [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have used $value = (isset($_REQUEST['value'])) to define the index variable. However, $value variable shows as a type Boolean and value of 0 or 1 in the debugger, which results in not getting the correct results when the $value is used with a (if) statement.
$page_limit = (isset($_REQUEST["list_page"]));
$viewdate = (isset($_REQUEST["viewdate"]));
How do I correct the following so that the index is defined?
if($_REQUEST["viewdate"] == '') {
$viewdate = 'All';
} else {
$viewdate = $_REQUEST["viewdate"];
}
$targetpage = "newindex.php?viewdate=".$_REQUEST["viewdate"]."&search=Search";
$page = (isset($_REQUEST['page']));
if($page_limit == '') {
The code above works, without the isset() function, but displays Notice - E messages
You should use isset to test whether a variable has been set, then assign the value. Like so:
$something = null;
if (isset($_REQUEST['something'])) {
$something = $_REQUEST['something'];
...
}
// Later in the code
if ($something !== null) {
// Do stuff

Undefined variable: total in [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I am using foreach over an array and adding each member and assigning total to a variable.
It works like i want, but I get the error:
Notice: Undefined variable: total in C:\xampp\htdocs\preg.php on line
10
I dont quite understand how/why this works and why I get the ^^ error
<?php
$bar = [1,2,3,5,36];
foreach($bar as $value) {
$total = ($total += $value);
}
echo $total;
?>
Before the foreach you need
$total = 0;
foreach($bar as $value){....
The $total variable wasn't initialised, so it couldn't be added to itself. Also, you can rewrite the whole thing like this:
$bar = [1,2,3,4,5];
$total = 0;
foreach($var as $value) {
$total += $value;
}
echo $total;

Categories