2014-10-01 39 views
0

我正在處理一個數組,該數組向我提供所有信息(如果print_r但它也表示它是未識別的索引)。 代碼:即使我可以獲取數據,未定義的索引通知

foreach ($_SESSION['passo4'] as $key => $value) { 
     $x = $data_ref[0]['tipo_refeicao']; //gives me the error 
     echo $x; //echoes 1 
     print_r($data_ref); 
     if($key != 'preco'){ 
      //Obter info do tipo de vestuário 
      $f_r = $dbh->prepare("SELECT tipo_refeicao, preco_acompanhante, preco_participante FROM refeicao WHERE id_extra = '$key'"); 
      $f_r->execute(); 
      $data_ref = $f_r->fetchAll(); 

      echo "<tr><td>". 
      datasearch($data_tref, 'tipo_refeicao', $x, 'descricao') 
      ."</td>"; 

      echo "<td>". $value ."</td>"; 

      echo "<td>". $data_ext[0]['preco'] * $value ."€</td></tr>"; 
     } 
    } 

通知

Notice: Undefined offset: 0 in C:\xampp\htdocs\Rot.Aventura\eventos\passo5.php on line 96 

的print_r($ data_ref):

Array ([0] => Array ([id_refeicao] => 4 [id_evento] => 11 [tipo_refeicao] => 1 [preco_participante] => 5 [preco_acompanhante] => 6 [limite_pessoa] => 2)) 

模具($ X):1

我應該隱藏與本通知@或有什麼辦法可以解決這個問題嗎? (對不起,葡萄牙語單詞)

+0

post'print_r($ data_ref)'看得更清楚 – Saqueib 2014-10-01 11:33:16

+0

將'[0]'留在代碼之外。 – Daan 2014-10-01 11:34:18

+0

這是一個循環嗎? – senK 2014-10-01 11:34:52

回答

0

由於您處於foreach循環,因此不必使用索引0foreach自動遞增的指數,所以對未來指數0是不確定的

請嘗試沒有指數或將$ X退出循環

foreach ($_SESSION['passo4'] as $key => $value) { 
    ... 
    $data_ref = $f_r->fetchAll(); 
    $x = $data_ref['tipo_refeicao']; //gives me the error 

foreach ($_SESSION['passo4'] as $key => $value) { 
... 
$x = $data_ref['tipo_refeicao']; //gives me the error 

試試看

+0

這是一個SQL錯誤,謝謝你花時間回答 – SandroG 2014-10-01 11:54:53

0

問題與我的代碼有誤:

$f_r = $dbh->prepare("SELECT tipo_refeicao, preco_acompanhante, preco_participante FROM refeicao WHERE id_extra = '$key'"); 

id_extra在哪裏它應該是id_refeicao。感謝您的時間和幫助

相關問題