0

OK, so, this may seem like a dumb question. I've looked through the other threads with this thing and I'm not finding QUITE what I'm looking for.

I get this error:

Warning: Invalid argument supplied for foreach() in entry.php on line 118

line 118 is the foreach that I'm using here:

<select id="Location" name="Location" class="text">
<option selected="selected"> - Choose Location - </option>
<?php 
    $locations = getLocationList();//this returns an array from separate function
    foreach($locations as $location) {//<-Line 118
        echo "<option value=". $location['locationID'] .">".$location['locationName']."</option> ";
    }
?>
</select>

It's not populating, it's only throwing the error. Thoughts?

OK EDIT This is how I'm pulling the data in a separate function:

function getLocationList()
{
    $mydb = new myDBC();//<-this calls my secure connection class
    $table = "LocationTable";
    $sql = "SELECT `locationID`, `locationName` FROM " .$table;
    $rez = $mydb->runQuery($sql);//<-this connects runs my query
    if(isset($rez))
    {
        $newRow = mysqli_fetch_array($rez);//<-is this not returning an array?
        return $newRow;
    }
}
5
  • 1
    What does a var_dump of $locations tell you? Does it really returns an array or maybe a null value? Commented Jun 15, 2015 at 17:47
  • It's not returning anything - which means, it's probably not pulling the data correctly...? Commented Jun 15, 2015 at 17:50
  • make sure that you are connected to the database if you are getting data from a database and also check the getLocationList() as if it is empty it means that it is not returning anything Commented Jun 15, 2015 at 17:51
  • Ok, so I went and changed the way it was pulling, and when i do a var_dump I get only one location out of 12 array(4) { [0]=>string(1) "1" ["locationID"]=> string(1) "1" [1]=> string(7) "Atlanta" ["locationName"]=>string(7) "Atlanta" } Commented Jun 15, 2015 at 18:00
  • To me, it sounds like your method getLocationList(); is all messed up. Provide the code for this function so we can inspect. Commented Jun 15, 2015 at 18:12

2 Answers 2

1

When you have errors like Warning: Invalid argument supplied for foreach() it literally means that whatever variable you are using in your foreach() is not an array nor an object. Verify that $locations is either an array or an object. One you fix that, it would work.

Ok, after checking your update, try making these changes:

Change this:

if (isset($rez)) {
    $newRow = mysqli_fetch_array($rez); //<-is this not returning an array?
    return $newRow;
}

To this:

if (isset($rez)) {
    return $rez->fetch_assoc();
}

OK, seems like your class/library is not a full library and because of this you have to add your own code to enhance it. In order to make this work how you would like it to, add this function somewhere in your DBClass.php

function convert_result_to_arr($result) {
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $arr[] = $row;
        }
    }
    return $arr;
}

This will convert your results into an appropriate muilti dimensional array so that you can iterate and get your info.

Now you can use it like this:

$rez = $mydb->runQuery($sql); //<-this connects runs my query
$rez_converted = $mydb->convert_result_to_arr($rez);
if(isset($rez_converted )){
        return $rez_converted ;
}

Now you can iterate like you originally wanted to:

foreach($locations as $location) {//<-Line 118
        echo "<option value=". $location['locationID'] .">".$location['locationName']."</option> ";
    }

I would also highly recommend that you transition into an MVC framework. I recommend Codeigniter to start. You wouldnt need to worry about all this along with other problems that come along.

Sign up to request clarification or add additional context in comments.

14 Comments

now that the data is being pulled appropriately, I get this info: Illegal string offset 'locationID' in entry.php on line 120 which is my echo statement.
probably because you are not getting an array. What do you get when you var_dump($location); ?
hang on... I'm going to show you how i'm pulling the data in the original post...
is myDBC a custom library? if so, where can we find the documentation?
Yes, this is my connection class - which also runs my queries
|
0

Have you tried doing a print_r() or var_dump() of $locations. There's nothing wrong with your foreach loop, so most likely the getLocationList() function is returning a non-array, or an empty array.

Befor foreach() you can check count($locations)

    <?php 
    $locations = getLocationList();//this returns an array from separate function
    if(count($locations)>0)
    {
        foreach($locations as $location) 
        {
              echo "<option value=". $location['locationID'] .">".$location['locationName']."</option> ";
        }
    }
    ?>

6 Comments

Have to tried to put foreach in if(count($locations)>0) { } ?
I get this info: Illegal string offset 'locationID' in entry.php on line 120 which is my echo statement
Paste the result of print_r($locations);
array(4) { [0]=>string(1) "1" ["locationID"]=> string(1) "1" [1]=> string(7) "Atlanta" ["locationName"]=>string(7) "Atlanta" }
This is var_dump() result. Please paste print_r() result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.