【问题标题】:.next() in php shows random blank valuesphp 中的 .next() 显示随机空白值
【发布时间】:2016-01-17 06:35:29
【问题描述】:

我有一个很大的表单,我通过 post 方法将其传递给 PHP 页面。其中 $POST 变量有 2 个数组,根据用户填写表单的方式,它们可能会有所不同,所以为此,我从$POST 变量中迭代以查看它是否是一个数组,如果它是从该值创建一个表,则有时.next 值似乎没有读取一个数组中的值,而是在另一个它确实完美地阅读了它。

这是我的每个代码:

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
    )

【问题讨论】:

    标签: php html-table next


    【解决方案1】:

    更新( cmets之后):

    您可以使用以下代码。遍历所有帖子数据并检查第一个元素是否为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
    


    在cmets之前回答。

    我不明白为什么你在这里使用下一个函数并且你以这种方式复杂的事情但是如果你添加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
    

    另外,请注意 next() http://php.net/manual/en/function.next.php

    在返回之前将内部数组指针前移一位 元素值

    我认为你在这里误用了这个特定的功能。

    【讨论】:

    • 因为首先我必须遍历 $_POST 变量,在那里我找到了 2 个数组,我必须再次迭代,我在第二个 foreach 中使用 next($var),至是检查 $_POST 的值是否是一个数组的那个,如果它是一个数组,那么它会做表格,下一个帮助我,因为它说将内部数组指针向前推进一个位置以获得我需要的元素值跨度>
    • 我认为这一定是一种更简单的方法。您要为每种类型的 cirugía 获取哪些数组元素?
    • 如果 $_POST 变量是一个数组,那么我想首先将 [tipoCirugia] 中的值放在表格行中,然后我现在 [tipoCirugia] 之后的下两个值是[hospital] 和 [fechaCirugia] 我想把它们放在接下来的 2 个表格行中
    • 谢谢!那成功了,你现在是我在地球上最喜欢的人之一!
    • 哦,太好了!很高兴我帮助了你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2017-06-26
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多