Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimension Associative Arrays - Take 2 2

Status
Not open for further replies.

tobyheywood

IS-IT--Management
Apr 20, 2001
122
GB
Hi all,

Small problem... Why would I (when nesting foreach statements) get the following error message from the second foreach statement? Hopefully that question made sense!

Warning: Invalid argument supplied for foreach() in %PATHTOFILE%/allfacts.php on line 131

I think it is because the foreach statement is treating it as an array (which it is) but then falls over!

Can anyone please shed some little on the matter?

TIA.

Regards

Toby Heywood
 
Jakob,

It's not so much the way the foreach statement is used it is more a case of I don't understand why it isn't working.

I know that if you use the following

$testvars = "SELECT * FROM TESTTABLE";
$result = mysql_query($testvars);

foreach($result as $key) {
print &quot;<tr>&quot;;
foreach($key as $id => $var) {
print &quot;<td>$id</td><td>$key</td>
print &quot;</tr>&quot;;
}
}

... It should produce (if I've got it right) a table with two columns and how every many rows of data where stored in the multidimensional associative array.

The foreach instructional page unfortunately doesn't mention (from what I can see) anything about multidimensional associative arrays so has not proved that useful.

Thanks for your input so far, don't suppose you have any other suggestions or could clarify what is going on?

TIA

Toby Heywood
 
Toby,

... not quite sure what you're looking for. Can you post a very brief example code?

I'll have a look tomorrow -I'm off to bed now §;O)

Regards


Jakob
 
You realize the code sample you provided will not give you a multi-dimensional array at all, rather just a MySQL result reference, right?
Anyway, using
Code:
var_dump
you should be able to see why it fails.

//Daniel
 
sorry,

In my example code I forgot to add the line

$return = mysql_fetch_row($result);

This went between the mysql_query() & the first foreach()

TIA.

Toby Heywood
 
the code snippet is as follows:

$return = mysql_query($query);
$result = mysql_fetch_row($result);
print &quot;<table>&quot;;
foreach($result as $id) {
print &quot;<tr>&quot;;
foreach($id as $value) {
// This should print three columns and then exit
print &quot;<td>$value</td>&quot;;
}
print &quot;</tr>&quot;;
}
print &quot;</table>&quot;;

I will try the var_dump() a little later on and report back with my finds but if someone can enlighten me as to why it is currently failing it would be greatly appreciated.

Regards

Toby Heywood
 
In actual fact please do not respond as I thing I have figured it out!

I will post the outcome a little later on!

Thanks to everyone who has responded for your time and patience in what is quite a fundamental set of functions.

Regards

Toby Heywood
 
Toby,

I *think* this is it:

Code:
$return = mysql_query($query);
print &quot;<table>&quot;;
foreach($return as $id) {
    print &quot;<tr>&quot;;
    $result = mysql_fetch_row($result);  // array stripped from $return
    foreach($result as $value) {  // strip each element in $result into $value
      print &quot;<td>$value</td>&quot;;
    }
    print &quot;</tr>&quot;;
}
print &quot;</table>&quot;;

Sorry, I haven't got the time to test it right now, but I think it makes sense

Give it a go and let's hear how it turns out §;O)


Jakob
 
dkdude
The first foreach statement makes absolutely no sense.
$return is a MySQL result resource identifier, not an array. danielhozac had that pointed out some posts ago.

toby
Your SQL statement selects '*' from a table. To my knowledge there is no way to store an array directly into a MySQL table. Maybe serialized, yes, but there's no array column type.

Every record itself can be retrieved as an associative array with keys and values - if that's what you are looking for. You might say that the result identifier in principle acts like an array since it gives access to the individual rows of the result set. However, it is just a number that is used to retrieve the individual rows.
To get an individual row as an associative array use mysql_fetch_assoc(). It will provide keys, which is the column name, and values, the actual data.

The looping through the result set would be achieved with a while loop rather than a foreach:
Code:
$result = mysql_query($SQL) OR die(mysql_error());
# now the while
while ($row = mysql_fetch_assoc($result)){
   # now you can use the foreach for the columns
   foreach($row as $columnName => $value){
      # output etc.
   }
   # loop goes to the next row until all are done...
}

Hope that helps somewhat. I just did not see any multi-dimensional array in the problem.
 
Greetings DRJ478,

Ahhh, Right!! That all makes sense && I was wrong -I mostly use mysql_fetch_array() and I too was a bit confused about that (not to be found here) multi-dim array §;O)

You hereby earned yourself a star.

Reards


Jakob
 
DRJ478 and all,

Apologies for not posting back sooner, just started a new job which has meant that some of my own projects have been placed on the back burner.

However... I would just like to say a very big thank you for your explaination. I think I was looking at it from too many angles rather than each column gets processed (by row), once all columns from row one have been returned, the loop will move one to the remaining rows!

DRJ you are a star! Therefore I believe that this post should get a star (to accompany the one it already has!)

Kind regards

Toby Heywood
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top