hi friends,

my code is as follow,

------sql query here------------
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
$row[tableLegends];
$link[]=$row; 
}

when i use foreach loop in template, it give the inavlid argument "Invalid argument supplied for foreach() error"

foreach($link AS %links){
--------code here-------
}

i am not understanding where i comit mistake.

If the query has no result, $link will be null and that is the invalid argument. If you initialize

$link = array ();

before the query, this will solve that problem.

I used following code as per your suggestion,

------sql query here------------
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
$row[tableLegends];
$link = array ();
$link =$row; 
}

but the script is giving the same error.

foreach($link AS %links){
--------code here-------
}

i am not understanding where i comit mistake.

If that code was a copy and paste from your actual code (as opposed to if you manually typed it into here), I hate to inform you but it's just a typo. You typed: foreach($link as [B][U]%[/U][/B]links) There's a percent sign (%) there instead of a dollar sign ($).

The proper use of this would be declaring the $link variable as an array before you start looping through the array results of the query. Your code should look like this:

$result = mysql_query($sql); //Make sure to use a semicolon at the end of a line, not a colon!
$link = array(); //Here is where you declare the array
while ($row = mysql_fetch_array($res))
{
//$row[tableLegends]; <---Not sure what this is for, and since it doesn't appear to have a function I commented it out
$link[] = $row; //You were right by approaching this with $link[] and not $link, if you used $link then link would only contain the value of row and won't be an array of the rows
}

Also looking at your foreach code, what is %links . Don't you mean $links :

foreach($link as $links)
{
   ...
}

My suggestion stated _before_ the query...

$link = array ();
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
  $link[]=$row; 
}

My suggestion stated _before_ the query...

$link = array ();
$result = mysql_query($sql):
while ($row = mysql_fetch_array($res))
{
  $link[]=$row; 
}

Exactly..though it does need one minor fix! (But I'm sure you knew this!):

$link = array();
$result = mysql_query($sql); //<-- Semicolon
while ($row = mysql_fetch_array($res))
{
  $link[] = $row; 
}

Also, I believe the problem was that instead of $links , %links as used, but your solution provides a safety if there are no rows returned (Which is a good thing to have!).

Glad you saw that typo too, FlashCreations. I was starting to worry that maybe %<varnamehere> was some PHP syntax I'd never heard of before =P

Well observed... missed the colon.

I left the template alone, because I was unsure if it was supposed to be php, or a specific templating syntax.

Glad you saw that typo too, FlashCreations. I was starting to worry that maybe %<varnamehere> was some PHP syntax I'd never heard of before =P

I've never heard of it before and like pritaeas, thought it was some kind of templating application, but assumed you weren't using one.

Well observed... missed the colon.

I left the template alone, because I was unsure if it was supposed to be php, or a specific templating syntax.

At first I though the same thing, as that seems very logical!

If all your questions have been answered I ask that you mark the thread as solved to avoid further confusion! :D

I think you're confusing me for the OP ^^;
That would servis, not myself. He hasn't responded yet =)

thank you very much for your responses. when i declare arry() before the loop according to your advise, script run fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.