In php file while loop is used to populate data in array in a following
way,

$result = mysql_query($sql ) or die ('Error, list failed. ' . mysql_error());

while($row = mysql_fetch_array($result))
 {
  $row[tableLegends];
  $link[]=$row;
  }

and for loop is used in printing data in theme file

for ($j=0; $j<count($link); $j++) 
{
echo $link[$j]['f_name']; 
echo $link[$j]['l_name'];

------code here-------

}

Actually, I want to use foreach loop in place of for loop.

Any idea please..

Recommended Answers

All 2 Replies

Try the following:

foreach ($link AS $links) {
echo $links['f_name']; 
echo $links['l_name'];
}

Hi,

You could replace your for loop with a foreach loop such as:

foreach($link as $arrLink) {

    echo $arrLink['f_name']; 
    echo $arrLink['l_name'];
    ------code here-------

}

Hope this helps.

R.

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.