2015-10-19 81 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个表格行 –