Thursday, 30 August 2018

The Foreach loop does not echo anything

I'm working on my project. I have table with racers (ID, name, surname etc.) and I stored it in an array. Then i used foreach loop to echo this data, but nothing show up. This is my code:

$zavodnici_array = array();
while(false !== ($row = mysql_fetch_assoc($result))) {
$zavodnici_array[] = $row;
}
foreach($zavodnici_array as $key) {
  echo $zavodnici_array[$key][id] ."<br>";
  echo $zavodnici_array[$key][jmeno] ."<br>";
  echo $zavodnici_array[$key][prijmeni] ."<br>";
}

Can anybody help me? :)

THere are a few things wrong with your example.
when using foreach as $key key is the value of each item in the array not the key
ass uming your mysql query fetched results
foreach($zavodnici_array as $key => $value) {
  echo $zavodnici_array[$key]['id'] ."<br>";
  echo $zavodnici_array[$key]['jmeno'] ."<br>";
  echo $zavodnici_array[$key]['prijmeni'] ."<br>";
}

or
foreach($zavodnici_array as $value) {
      echo $value['id'] ."<br>";
      echo $value['jmeno'] ."<br>";
      echo $value['prijmeni'] ."<br>";
    }

keys in php are strings or integers $value[id] is not valid. I assumed you were trying to type the index id

0 comments:

Post a Comment