Undefined variable: total in [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 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;

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

ErrorException (E_NOTICE) Undefined variable: actualLabels [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.
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;
}

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.

Mysql pagination doesn't return pages, undefined variable [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.
Good morning everyone. I have the following method which creates me some trouble. Since i have 19 products in that category it return me:
Notice: Undefined variable: pages.
I checked to see from where the problem comes and it's because of $total_pages which returns me 0.95. How could i fix this problem and how could i make it when it reaches at the end of pages to stop because for the moment i have 2 - 3 pages that are empty with no products on them.
public function getTotalProductsByCategory($page_number, $categoryId) {
$this->database->query('SELECT COUNT(ProductId) FROM products WHERE CategoryId = :categoryId');
$this->database->bind(':categoryId', $categoryId);
$count = $this->database->resultSet();
$total_pages = ceil($count['0']['COUNT(ProductId)'] - 1) / 20;
for($i = max(1, $page_number - 5); $i <= min($page_number + 5, $total_pages); $i++) {
$pages[] = $i;
}
return $pages;
}
Define $pages as an array before using indexing syntax to assign to it:
$pages = array()
Do this outside of the loop.

Categories