I'm bashing my head against my desk trying to figure out why this PHP code is causing this error: Undefined index: arr. I'm using Laravel, and this code works like gold outside of it, but inside Laravel, it's returning the undefined index error.
Here's the code:
function set_pilots_array($line_array)
{
$airports = $this->airports;
$pilots = $this->pilots;
foreach($airports as $airport)
{
if($airport == $line_array[11] || $airport == $line_array[13])
{
if($airport == $line_array[11])
{
$deparr = "dep";
}
if($airport == $line_array[13])
{
$deparr = "arr";
}
$this->pilots[$deparr][] = array($line_array[0], $line_array[11], $line_array[13], $line_array[7], $line_array[5], $line_array[6], $line_array[8]);
}
}
}
function get_pilots_count()
{
$count = count($this->pilots['dep']) + count($this->pilots['arr']);
return $count;
}
This sort of goes with my other question: Grab and Explode Data It's pulling the data from the data file using this code:
elseif($data_record[3] == "PILOT")
{
$code_obj->set_pilots_array($data_record);
}
Which later does this:
$code_count = $code_obj->get_pilots_count();
You do not have $this->pilots['arr'] set. In other words, if you look at the output of var_dump($this->pilots);, you shall see there is no arr key-value pair. I suggest you this fix:
$count = count((isset($this->pilots['dep']) ? $this->pilots['dep'] : array())) + count((isset($this->pilots['arr']) ? $this->pilots['arr'] : array()));
Actually, this is not a fix - this is more like a hack. To make your code correct i suggest you to set the default values for those $pilots['arr'] and $pilots['dep'] values:
function set_pilots_array($line_array)
{
$airports = $this->airports;
$pilots = $this->pilots;
foreach (array('dep', 'arr') as $key)
{
if (!is_array($pilots[$key]) || empty($pilots[$key]))
{
$pilots[$key] = array();
}
}
// ...
}
Well there is too little code to really figure out what is going on, but based on what I see:
if($airport == $line_array[13])
this condition is never being met and so $deparr = "arr"; never happens and because of this
count($this->pilots['arr']);
is giving an undefined index error
You can easily suppress this by:
$count = count(#$this->pilots['dep']) + count(#$this->pilots['arr']);
your problem is that you are accessing all of your indexes directly without checking if they exist first.
assume that in laravel something is causing the array to not be populated.
in order to fix this, you should either iterate through the array with a foreach, or do a if(!empty($line_array[13])) {} before accessing it.
Related
I have a page that takes a lot of $_GET parameters but it's not always guaranteed for all fo them to be used. So i get a lot of warnings right now for undefined variables. I obvously need to check if it's set with isset so i avoid warnings but i have literally 80!!!
Just showing few for example (but they are much more than that, maybe 80)
$pt2r2c1 = htmlspecialchars($_GET['pt2r2c1']);
$pt2r2c2 = htmlspecialchars($_GET['pt2r2c2']);
$pt2r2c3 = htmlspecialchars($_GET['pt2r2c3']);
$pt2r3c1 = htmlspecialchars($_GET['pt2r3c1']);
$pt2r3c2 = htmlspecialchars($_GET['pt2r3c2']);
$pt2r3c3 = htmlspecialchars($_GET['pt2r3c3']);
$pt2r4c1 = htmlspecialchars($_GET['pt2r4c1']);
$pt2r4c2 = htmlspecialchars($_GET['pt2r4c2']);
$pt2r4c3 = htmlspecialchars($_GET['pt2r4c3']);
$pt2r5c1 = htmlspecialchars($_GET['pt2r5c1']);
$pt2r5c2 = htmlspecialchars($_GET['pt2r5c2']);
So i started doing this manually ... but at the 40th i felt stupid and i thought if there is a better way to do it.
if (isset($_GET['pt1r5c3'])) {
$pt1r5c3 = htmlspecialchars($_GET['pt1r5c3']);
}
if (isset($_GET['pt1r6c1'])) {
$pt1r6c1 = htmlspecialchars($_GET['pt1r6c1']);
}
if (isset($_GET['pt1r6c2'])) {
$pt1r6c2 = htmlspecialchars($_GET['pt1r6c2']);
}
if (isset($_GET['pt1r6c3'])) {
$pt1r6c3 = htmlspecialchars($_GET['pt1r6c3']);
}
if (isset($_GET['pt2r1c1'])) {
$pt2r1c1 = htmlspecialchars($_GET['pt2r1c1']);
}
if (isset($_GET['pt2r1c2'])) {
$pt2r1c2 = htmlspecialchars($_GET['pt2r1c2']);
}
if (isset($_GET['pt2r1c3'])) {
$pt2r1c3 = htmlspecialchars($_GET['pt2r1c3']);
}
Ok thanks to some hints in the comments i realised i can just try with a simple array and foreach. So after some trial and error i managed to do it that way. Hope it help others.
$tree_variables_array = ['pt1r1c1', 'pt1r1c2', 'pt1r1c3', 'pt1r2c1'];
foreach ($tree_variables_array as $var) {
if(isset($_GET[$var])) {
$$var = htmlspecialchars($_GET[$var]);
}
}
if (isset($data['start']['tan']) AND $data['start']['tan'] == true) {
$get_data[] = 'tan';
}
if (isset($data['start']['ban']) AND $data['start']['ban'] == true) {
$get_data[] = 'ban';
}
if (isset($data['start']['pan']) AND $data['start']['pan'] == true) {
$get_data[] = 'pan';
}
Try to take only tan equal to "a111" values. Tested the code below, but I could not get even tan values.
if (isset($data['start']['tan']) AND $data['start']['tan'] == true AND $data['start']['tan'] == 'a111') {
$get_data[] = 'tan';
}
Have you tried to debug this yourself? Is it possible that the data you are looking for is not in the $data variable? Or that it's in a different format?
Try adding this:
print_r($data)
before your IF statements to help you debug the issue. Your code MOSTLY looks correct. Though, I know that I have always used .= when potentially appending to arrays, instead of just = . Like:
$get_data=array();
if ($tan) $get_data[] .= $data;
Of course, I was generalizing in my above example, but you get the idea.
Im writing a page in HTML/PHP that connects to a Marina Database(boats,owners etc...) that takes a boat name chosen from a drop down list and then displays all the service that boat has had done on it.
here is my relevant code...
if(isset($_POST['form1'])){//if there was input data submitted
$form1 = $_POST['form1'];
$sql1 = 'select Status from ServiceRequest,MarinaSlip where MarinaSlip.SlipID = ServiceRequest.SlipID and BoatName = "'.$form1.'"';
$form1 = null;
$result1 = $conn->query($sql1);
$test = 0;
while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$values1[] = array(
'Status' => $row['Status']
);
$test = 1;
}
echo '<p>Service Done:</p><ol>';
if($test = 1){
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
echo '</ol>';
}else{
echo 'No service Done';
}
the issue im having is that some of the descriptions of sevice are simply Open which i do not want displayed as service done, or there is no service completed at all, which throws undefined variable: values1
how would I stop my script from adding Open to the values1 array and display a message that no work has been completed if values1 is empty?
Try this
$arr = array();
if (empty($arr))
{
echo'empty array';
}
We often use empty($array_name) to check whether it is empty or not
<?php
if(!empty($array_name))
{
//not empty
}
else
{
//empty
}
there is also another way we can double sure about is using count() function
if(count($array_name) > 0)
{
//not empty
}
else
{
//empty
}
?>
To make sure an array is empty you can use count() and empty() both. but count() is slightly slower than empty().count() returns the number of element present in an array.
$arr=array();
if(count($arr)==0){
//your code here
}
try this
if(isset($array_name) && !empty($array_name))
{
//not empty
}
You can try this-
if (empty($somelist)) {
// list is empty.
}
I often use empty($arr) to do it.
Try this instead:
if (!$values1) {
echo "No work has been completed";
} else {
//Do staffs here
}
I think what you need is to check if $values1 exists so try using isset() to do that and there is no need to use the $test var:
if(isset($values1))
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
Or try to define $values1 before the while:
$values1 = array();
then check if it's not empty:
if($values1 != '')
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
All you have to do is get the boolean value of
empty($array). It will return false if the array is empty.
You could use empty($varName) for multiple uses.
For more reference : http://php.net/manual/en/function.empty.php
In Javascript I would do something like this-
var time_array = [];
if ((previousTime > time_array[i]) || (time_array[i] === undefined) )
{
//Do Something
}
I want to do something similar in PHP
$time_array = array();
if (($previousTime> $time_array[$i]) || ($time_array[$i] === undefined) ))
{
//Do Something
}
I can do this very easily in Javascript , but I am a little confused about it in PHP.
$time_array = array();
if (!isset($time_array[$i]) || $previousTime > $time_array[$i])
{
//Do Something
}
It will first check if the variable is set and if it is not, the second condition will never be evaluated, so it's safe to use.
Edit: isset() checks whether a variable is declared and is set to a non-null value. You may want to use: if ($time_array[$i] === null) instead (that would be probably more similar to JS's undefined).
Just use this:
http://www.w3schools.com/php/func_array_key_exists.asp
if (array_key_exists($i,$time_array))
{
echo "Key exists!";
}
I've been trying to work my way to it simply must check it with all inputs in the present that I might have 100 lines so got it done less to just check it all together for an error or what?
What I think about that: instead of taking 14 lines of code that make it less but may be the same,
if($_POST["email"] == "")
{
$error = 1;
}
if($_POST["pass1"] == "")
{
$error = 1;
}
if($_POST["pass2"] == "")
{
$error = 1;
}
if($_POST["fornavn"] == "")
{
$error = 1;
}
if($_POST["efternavn"] == "")
{
$error = 1;
}
if($_FILES["file"] == "")
{
$error = 1;
}
Just create an array of field names and loop over them...
foreach(array('email','pass1','pass2',...) as $field) {
if(empty($_POST[$field])) {
$error = 1;
}
}
$_FILES you will have to handle separately. You can create another loop if you want. The structure of $_FILES is different though; I think you should be checking the "error" field. Check the docs.
Another way to approach this, considering the fact that you don't seem to be performing different tests for each field, or responding with specific error messages, is to use array_diff_key:
$names = array('email', 'pass1', 'pass2', 'fornavn', 'efternavn', 'file');
$names = array_keys($names); // convert $names to keys
$posts = array_filter($_POST); // filter out any falsy values
$fails = array_diff_key($names, $posts); // check for the differences
$error = $fail ? 1 : 0;
If you then wish to respond with specific messages depending on what is missing, you already have a list of the problem parameters in $fails. If you wish to extend the above to take into account specific value checks, you could write your own array filter callback function and pass it's name as the second parameter to array_filter... however this wont allow you to choose a different filter response based on array key, only on array value. All because rather annoyingly array_filter doesn't receive a key value when filtering an array.