Notice: Undefined offset: 0 using Array in PHP - php

When I'm executing this code-
public $speaker_list=array();
for($i=0;$i<$nbre_speaker;$i++)
{
$speaker_list[$i]=new Speaker($_POST['speaker'+$i],$_POST['pro_speaker'+$i],$_POST['bio_speaker'+$i]);
}
I'm getting the error:
Notice: Undefined offset: 0
Notice: Undefined offset: 1

Assuming $nbre_speaker is set to something sensible outside the code you provided.
Try this
public $speaker_list = array();
for($i=0; $i < $nbre_speaker; $i++)
{
if ( isset($_POST['speaker'.$i], $_POST['pro_speaker'.$i], $_POST['bio_speaker'.$i] )) {
$speaker_list[$i] = new Speaker($_POST['speaker'.$i],
$_POST['pro_speaker'.$i],
$_POST['bio_speaker'.$i]);
} else {
// report error, or do something to fix it.
}
}

Related

Notice: Undefined offset: 0 in C:\xampp\htdocs\isan\hasildata.php on line 21

Hi I receive the following error in my homework and I do not know what is the problem hasildata.php on line 21 :
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];
Screenshot
You must check whether your request is OK or not
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
if ($data['status'] !== "OK") {
// Error
echo $data['error_message'];
// Do something
} else {
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];
}

PHP Error was encountered Codeigniter

someone from freelancer created this web based software for us. By mistake i deleted all the medical examination (i'm a doctor assistant) from the control panel.
The software now gives me this error:
Message: Undefined variable: data
Filename: models/gestione_model.php
Line Number: 127
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: models/gestione_model.php
Line Number: 127
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: models/gestione_model.php
Line Number: 127
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: models/gestione_model.php
Line Number: 127
I find the line 27 here:
public function conta_visite($s)
{
$query = $this->db->get('oggetti');
if ($query->num_rows() > 0) {
$data = $query->result_array();
}
$aperte = 0;
$chiuse = 0;
foreach ($data as $d) {
if (new DateTime() > new DateTime($d['DataVisita'])) $chiuse++;
else $aperte++;
}
if($s == 1) return $aperte;
else return $chiuse;
}
Can someone help me please? Thanks
you are closing IF before foreach!
public function conta_visite($s) {
$query = $this->db->get('oggetti');
if ($query->num_rows() > 0) {
$data = $query->result_array();
} // HERE if result is ==0 then $data does not exist
$aperte = 0; $chiuse = 0; foreach ($data as $d) {
if (new DateTime() > new DateTime($d['DataVisita'])) $chiuse++;
else $aperte++;
}
if($s == 1) return $aperte;
else return $chiuse;
You need to make sure that $data is set before using it. If no rows are returned then $data is not set and you get errors when trying to process it.
if ($query->num_rows() > 0) {
$data = $query->result_array();
} else {
//do what needs to be done if the database does not return any rows
$data = array(); //empty array maybe?
}

Getting 5 error messages | PHP [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 6 years ago.
<?php
function getLeeftijdsCategorie($leeftijd){
if($leeftijd<18){
$categorie="kind";
}
elseif($leeftijd>=18&&$leeftijd<65){
$categorie="volwassen";
}else{
$categorie="bejaard";
}
return $categorie;
}
//globale array met leeftijden
$aLeeftijden = array(16,17,18,14,22,34,67,58,8,4,55,22,34,45,35);
$aantalKind = 0;
$aantalBejaard = 0;
$aantalVolwassen = 0;
for ($x=0; $x <= count($aLeeftijden); $x++) {
if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'kind') {
$aantalKind;
}
if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'volwassen') {
$aantalVolwassen++;
}
if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'bejaard') {
$aantalBejaard++;
}
}
echo "Aantal kinderen : ".$aantalKind;
echo "<br>Aantal volwassen personen : ".$aantalVolwassen;
echo "<br>Aantal bejaarden : ".$aantalBejaard;
?>
Hi, im getting 5 error messages can someone please help me i need to get how many people are children etcetra.
I already tried over an hour but i really cant find it.
The error message is:
PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 33
PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 37
PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 41
Thanks
You “function return value in write context” is related to this line:
if (getLeeftijdsCategorie($aLeeftijden[$x]) = 'bejaard') {
You have to change = in ==.
Then, there is also a Parse Error:
echo "<br>Aantal bejaarden : "$aantalBejaard;
must be:
echo "<br>Aantal bejaarden : " . $aantalBejaard;
# ↑
The undefined offset error is due to your for loop construction:
for ($x=0; $x <= count($aLeeftijden); $x++) {
must be:
for ($x=0; $x < count($aLeeftijden); $x++) {
$aLeeftijden count is 15, but last index is 14.
Give the following a try:
// Improved readability
function getLeeftijdsCategorie( $leeftijd ) {
if( $leeftijd < 18 ) {
$categorie = "kind";
} else if( $leeftijd >= 18 && $leeftijd < 65 ){
$categorie = "volwassen";
} else {
$categorie = "bejaard";
}
return $categorie;
}
//globale array met leeftijden
$aLeeftijden = array(16, 17, 18, 14, 22, 34, 67, 58, 8, 4, 55, 22, 34, 45, 35);
$aantalKind = 0;
$aantalBejaard = 0;
$aantalVolwassen = 0;
for( $x = 0; $x < count( $aLeeftijden ); $x++ ) {
if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'kind') {
$aantalKind++; // Forgot ++
}
if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'volwassen') {
$aantalVolwassen++;
}
// Forgot =
if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'bejaard') {
$aantalBejaard++;
}
}
// Writing strings like this is much less prone to errors
echo "Aantal kinderen : {$aantalKind}";
echo "<br>Aantal volwassen personen : {$aantalVolwassen}";
echo "<br>Aantal bejaarden : {$aantalBejaard}";
don't close php if you include it in some other file this may lead to other errors if you have spaces after the closing php tag.

PHP Notice: Undefined offset: 4

I'm getting the following error
PHP Notice: Undefined offset: 4 -- LINE 190
in the chunk of code below. I've tried a couple different solutions that I found here including isset, but nothing seems to work.
Does anyone have any suggestions?
// If we don't have any data get it from the db
$GLOBALS['AKB_CLASS_HELPER']->getCatsInfo();
$parentid = 0;
if (!empty($arrCats[0])) {
foreach ($arrCats as $cat) {
foreach ($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$parentid] as $catid) { <--------- LINE190
$pcat = $GLOBALS['AKB_CLASS_HELPER']->catsById[$catid];
if (strtolower($pcat['name']) == strtolower($cat)) {
$GLOBALS['CatTrails'][] = array ($pcat['categoryid'], $cat);
$this->_catId = $pcat['categoryid'];
$parentid = $pcat['categoryid'];
break;
}
}
}
}

Notice: Undefined index: in C:\wamp\www\tests\Joomla\Website\index.php on line 37

Hey i get this : Notice: Undefined index: in C:\wamp\www\tests\Joomla\Website\index.php on line 37
And my code is this :
<?php
$remarks=$_GET['remarks'];
if ($remarks==null and $remarks=="")
{
echo '';
}
if ($remarks=='success')
{
echo 'Registration Success';
}
?>
I don't understand why i get this . Please help!
First, you don't say where is the line 37... I ain't a wizard, but I can guess from the error...
Since the error is Undefined index, that must come from the line:
$remarks=$_GET['remarks'];
You should validate that $_GET['remarks'] is not null with isset($_GET['remarks']) before trying to get it's value.
Second, that line does not make any sence, since the $remarks can never be null and "":
if ($remarks==null and $remarks=="")
So I would write the code like this:
<?php
$remarks = "";
if ( isset($_GET['remarks']) ) {
$remarks = $_GET['remarks'];
}
if ( $remarks == 'success' ) {
echo 'Registration Success';
}
?>

Categories