ErrorException (E_NOTICE) Undefined variable: actualLabels [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 3 years ago.
Help, i got an error "ErrorException (E_NOTICE) Undefined variable: actualLabels" in my code
$title = "Data Confusion Matrix";
$testing_data = DataTesting::count();
$klasifikasi = Klasifikasi::with('sentimen')->get();
foreach($klasifikasi as $kelas){
$predictedLabels[] = $kelas->sentimen->kategori;
$testing = DataTesting::where('id_testing',$kelas->id_testing)->first();
$twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();
$actualLabels[] = $twitter->sentimen->kategori;
}
$getPrecision = new ControllerConfusionMatrix($actualLabels, $predictedLabels);
$accuracy = ControllerConfusionMatrix::score($actualLabels, $predictedLabels);
$recall = $getPrecision->getRecall();
$precision = $getPrecision->getPrecision();

Add this line to beginning of your code : $actualLabels = [];
You are getting error because when $klasifikasi is empty, then the statement inside loop is not executed. So $actualLabels variable is not created. In this case you get errro of (E_NOTICE) Undefined variable: actualLabels.
Hope you understand.

define Array()
$predictedLabels = array();
$actualLabels = array();
foreach($klasifikasi as $kelas){
$predictedLabels[] = $kelas->sentimen->kategori;
$testing = DataTesting::where('id_testing',$kelas->id_testing)->first();
$twitter = TwitterStream::with('sentimen')->where('id_crawling',$testing->id_crawling)->first();
$actualLabels[] = $twitter->sentimen->kategori;
}

Related

Undefined Index in array from xml [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 1 year ago.
I am getting "Undefined Index FruitOrder" in an if statement for an array which is created from XML/Soap Response
Code:
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $resp);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapBody ')[0];
$array = json_decode(json_encode($body), TRUE);
if($array['FruitOrder']['FruitCode'] == 'FruitSuccess' || $array['FruitOrder']['FruitCode'] == 'FruitSuccessWarnings')
{
To avoid the Undefined Index error you should check if the item ['FruitOrder']['FruitCode'] exists in the array with isset.
if(isset($array['FruitOrder']['FruitCode'])
&& ($array['FruitOrder']['FruitCode'] === 'FruitSuccess'
|| $array['FruitOrder']['FruitCode'] === 'FruitSuccessWarnings'))
{
echo 'process the data';
}

Undefined index: client_id" on line 1001 [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.
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"

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]);
}

Undefined offset error in PHP at last line [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 4 years ago.
I'm getting an undefined offset error
<?php
function ara($one, $two, $three)
{
#preg_match_all('/' . preg_quote($one, '/') .
'(.*?)'. preg_quote($two, '/').'/i', $three, $m);
return #$m[1];
}
$link = "example.com";
$article = file_get_contents($link);
$sport = ara('data-bin="','"',$article);
$channel = ara('data-videobin="','"',$article);
for ($i=0;$i<50;$i++)
echo"<span class='linko'><a target='_blank' href='example.org/live1.php?id=".($channel[$i]."'>$sport[$i]</a>"."<br></span>"); // error is here
?>
How can I fix it? I'am waiting for your helps. Thanks.
These codes are for a live channel website.
The channel variable has less than 50 elements.
Usually in a loop, you should set the end condition to the size of the array.
for ($i=0;$i<sizeof($channel);$i++)

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