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]);
}
Related
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.
any help you offer on this will be useful. Am working on my school project and this error code keeps popping up on the application "Undefined index: client_id" on line 1001"
Here is the code
public function onNewclientaddress(){
$addShipmentForm = Settings::get('addShipmentForm',true);
$data = post();
= \Spot\Shipment\Models\Address::where('user_id', $data['client_id'])->update(['default' => 0]);
if ( $addShipmentForm == "add_form_normal"){
$subitem = new \Spot\Shipment\Models\Address;
$subitem->name = htmlspecialchars($data['street_addr']);
$subitem->user_id = htmlspecialchars($data['client_id']);
$subitem->street = htmlspecialchars($data['street_addr']);
$subitem->city = htmlspecialchars($data['city_id']);
$subitem->zipcode = htmlspecialchars($data['postal_code']);
$subitem->country = htmlspecialchars($data['country_id']);
$subitem->default = 1;
$subitem->created_at = \Carbon\Carbon::now();
$subitem->updated_at = \Carbon\Carbon::now();
$subitem->save();
}
else{
line 1001 is the first line of code in the post. please pardon my English
This error indicates that $data['client_id'] is not being set. You will need to ensure that whatever form is providing this data, is passing client_id correctly.
You can see what is currently in $data with a line like:
die(var_dump($data));
This will output the data on the page.
Ensure your client_id field in your form has a name attribute:
name="client_id"
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Using LIKE in bindParam for a MySQL PDO Query [duplicate]
(2 answers)
Closed 3 years ago.
Why isn't this working? I've viewed the past posts on the subject and nothing worked for me.
$sql_strongsdef_ses = "SELECT id, strongs, etym_strongs FROM ".$tablename." WHERE etym_strongs LIKE ?";
$params = array("'%".$getSearchEtymStrongs."%'");
$result_strongsdef_ses = $conn->prepare($sql_strongsdef_ses);
$result_strongsdef_ses->execute($params);
echo "<span style=\"color: red;\">".$sql_strongsdef_ses."</span><br /><br />\n";
//while ($row_ses = $result_strongsdef_ses->fetch()){
while($row_ses = $result_strongsdef_ses->fetch(PDO::FETCH_ASSOC)){
$id_ses = $row_ses['id'];
$strongs_ses = $row_ses['strongs'];
$etymstrongs_ses = $row_ses['etym_strongs'];
}
var_dump($strongs_ses);
I get the error:
Notice: Undefined variable: strongs_ses in \edit.php on line 52
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.
php code:
1. if (isset($data['city_id']))
2. {
3. $city_id = "city_id='". $data['city_id']. "', ";
4. }
And I get:
Notice: Undefined index: city_id on line 3
How can this be?
Just ran your code sample and it works perfectly, I do not get 'undefined index' error - taking us to the big apple
<?php
$data['city_id']='New York';
if (isset($data['city_id']))
{
$city_id = "city_id='". $data['city_id']. "', ";
echo $city_id;
}
?>
output: city_id='New York',
Surely, without the $data['city_index']='New York'; I just get a blank screen, as the if condition is not met - no errors.
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.
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 need to make a blog using a TXT file for a school project.
In the text file $blog [0] is the title of the message, $blog1 is the username and $blog[2] is the message itself.
$file = fopen('blogs.txt', 'r');
while(!feof($file)) {
$blog = fgets($file);
$blog = explode("*", $blog);
echo "
<p><strong>". $blog[0]. "</strong>
<br>By: ". $blog[1].
"<br>". $blog[2];
}
The page shows all the messages. But at the bottom I have a couple of 'Undefined Offsets: 1' and 'Undefined offset: 2's. It also says 'By: ' (as shown in the echo) a couple of times.
This is what the page looks like
Check if explode returns more than one element:
$blog = explode("*", $blog);
id (count($blog) >= 3) {
echo "
<p><strong>". $blog[0]. "</strong>
<br>By: ". $blog[1].
"<br>". $blog[2];
} else {
//do some other stuff
}