2015-10-19 75 views
0

我有這種大的表單,我通過槽後的方法到PHP頁面..其中$POST變量有2個數組,這可能會有點不同,取決於用戶如何填寫表單,以便我從$POST變量中進行迭代,以查看它是否爲數組,如果它是從表中創建一個表的值,則有時.next的值似乎不會讀取值一個數組,但在另一個數組中,它完全讀取它。.next()在php中顯示隨機空白值

這是我對每個代碼:

foreach($_POST as $var){ 
if(is_array($var)){ 
    foreach($var as $x => $value){ 
     if($value != ''){ 
     switch ($x) { 
case "nombreOtrosMeds": 

$otrosMedicos.=' 
    <table class="tg"> 
     <tr>  
      <td class="tg-yw4l"><strong>Nombre: </strong> '.$value.'</td> 
      <td class="tg-yw4l"><strong>Dirección: </strong>'.next($var).'</td> 
     </tr> 
     <tr> 
      <td class="tg-yw4l"><strong>Teléfono: </strong>'.next($var).'</td> 
      <td class="tg-yw4l"><strong>Fax: </strong>'.next($var).'</td> 
     </tr> 
     </table> 
    '; 
    break; 
case "tipoCirugia": 
$algunaCirugia.=' 
    <table class="tg"> 
     <tr>  
      <td class="tg-yw4l"><strong>Tipo de cirugía: </strong> '.$value.'</td> 
      <td class="tg-yw4l"><strong>Hospital: </strong>'.next($var).'</td> 
      <td class="tg-yw4l"><strong>Fecha de cirugía: </strong>'.next($var).'</td> 
     </tr> 
     </table> 
    '; 
    break; 
} 
     } 

    } 

} 
} 

,你可以看到它在所有的情況下,但結果即時得到相同的是:

Tipo de cirugía: cirugia1 Hospital: Fecha de cirugía: 
Tipo de cirugía: Cirugia2 Hospital: Hospital2 Fecha de cirugía: 2015-10-15 

和數組都是這樣:

[1] => Array 
    (
     [nombreOtrosMeds] => 
     [dirOtrosMeds] => 
     [telOtrosMeds] => 
     [faxOtrosMeds] => 
     [tipoCirugia] => cirugia1 
     [hospital] => hospital1 
     [fechaCirugia] => 2015-10-07 
     [tipoNoCirugia] => hospitalizacion1 
     [Nohospital] => hospital hospitalizacion1 
     [fechaNoCirugia] => 2015-10-04 
     [tomaMedNombre] => 
     [tomaMedDosis] => 
     [tipoDroga] => droga1 
     [cantidadDroga] => cantidad droga 1 
     [tiempoDroga] => timepo droga 1 
     [tipoDieta] => 
     [cantidadPesodDieta] => 
     [fechaDieta] => 
    ) 

[cirugias] => Sí 
[2] => Array 
    (
     [tipoCirugia] => Cirugia2 
     [hospital] => Hospital2 
     [fechaCirugia] => 2015-10-15 
     [tipoNoCirugia] => Hospitalizacion2 
     [Nohospital] => Hospital Hospitalizacion2 
     [fechaNoCirugia] => 2015-10-13 
     [tipoDroga] => droga 2 
     [cantidadDroga] => cantidad droga 2 
     [tiempoDroga] => tempo droga 2 
    ) 

回答

1

UPDATE(評論):

您可以使用下面的代碼。遍歷所有的帖子數據,並檢查第一個元素是否爲tipoCirugia,以便適當回顯。如果是其他列,那麼這意味着用戶發佈了不同的數據,以便回顯不同的內容。

$data = $_POST; 
$i = 0; 
foreach($data as $var) { 
    if (is_array($var)) { 
     $element = 0; 
     foreach($var as $key => $value) { 
      if ($key == 'tipoCirugia' && $element == 0) { 
       $otrosMedicos .= ' 
        <table class="tg"> 
         <tr>  
          <td class="tg-yw4l"><strong>Nombre: </strong> '.$data[$i]['tipoCirugia'].'</td> 
         </tr> 
         <tr> 
          <td class="tg-yw4l"><strong>Dirección: </strong>'.$data[$i]['hospital'].'</td> 
         </tr> 
         <tr> 
          <td class="tg-yw4l"><strong>Teléfono: </strong>'.$data[$i]['fechaCirugia'].'</td> 
         </tr> 
        </table>'; 
      } elseif ($key == 'nombreOtrosMeds') { //Here you have to make a change according what data you want to show 
       $algunaCirugia .= ' 
        <table class="tg"> 
         <tr>  
          <td class="tg-yw4l"><strong>Nombre: </strong> '.$data[$i]['nombreOtrosMeds'].'</td> 
         </tr> 
         <tr> 
          <td class="tg-yw4l"><strong>Dirección: </strong>'.$data[$i]['hospital'].'</td> 
         </tr> 
         <tr> 
          <td class="tg-yw4l"><strong>Teléfono: </strong>'.$data[$i]['fechaCirugia'].'</td> 
         </tr> 
         <tr> 
          <td class="tg-yw4l"><strong>Fax: </strong>'.$data[$i]['Nohospital'].'</td> 
         </tr> 
        </table>'; 
      } 
      $element++; 
     } 
    } 
    $i++; 
} 
echo $otrosMedicos.'<br /><br />'; 
echo $algunaCirugia.'<br /><br />'; 

以上應該呼應:

Nombre: Cirugia2 
Dirección: Hospital2 
Teléfono: 2015-10-15 


Nombre: 
Dirección: hospital1 
Teléfono: 2015-10-07 
Fax: hospital hospitalizacion1 


評論前回答。

我不明白你爲什麼在這裏使用一個功能,你這樣複雜的事情,但如果添加next($var)

foreach($_POST as $var) { 
    if (is_array($var)) { 
     next($var); 
     //Rest remains the same 

它會響應:

Tipo de cirugía: cirugia1 Hospital: 2015-10-07 Fecha de cirugía: hospitalizacion1 
Tipo de cirugía: Cirugia2 Hospital: 2015-10-15 Fecha de cirugía: Hospitalizacion2 

另外請注意,接下來的()http://php.net/manual/en/function.next.php

在返回之前將內部數組指針前進一位 元素值

我認爲你在這裏有一些誤用了這個特殊的功能。

+0

原因首先,我必須迭代通過$ _POST變量,在那裏我發現了2個數組,我必須重複一次,我在第二個foreach中使用下一個($ var),這是一個檢查如果$ _POST的值是一個數組(如果是),那麼它執行該表,然後下一個幫助我,因爲它表示將內部數組指針向前推進一個位置以獲取元素值,我需要 –

+0

我認爲這必須是更簡單的方法來做到這一點你想爲每種類型的cirugía獲取數組元素? –

+0

如果$ _POST變量是它的一個數組,那麼我想首先獲取[tipoCirugia]中的值放在一個表格行中,然後我現在[tipoCirugia]之後的下2個值是[hospital]和[fechaCirugia]我想把它們放在接下來的2個表格行 –