Search code examples
phphtml-tableiteration

.next() in php shows random blank values


I have this big form that I pass trough post method to a PHP page.. in which the $POST variable has 2 arrays, that might be a little different from each other depending on how the user fills the form, so for that I iterate from the $POST variable to see if its an array and if it is then a make a table from the value, thing is sometimes the .next value doesn't seems to read the value in one array but in the other one it does read it perfectly.

Here's my for each code:

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;
}
        }

    }

}
}

as you can see it is the same in all of the cases but the result im getting is:

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

and the arrays are like this:

[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
    )

Solution

  • UPDATE (after the comments):

    You can use the following code. Iterate through all your post data and check if the first element is tipoCirugia so echo appropriately. If it's some other column then this means that user has post different data so echo something different.

    $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 />';
    

    The above should echo:

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


    Old answer before the comments.

    I cannot understand why you use next function here and you complex things in such a way but if you add next($var):

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

    It will echo:

    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
    

    Also, note that next() http://php.net/manual/en/function.next.php

    advances the internal array pointer one place forward before returning the element value

    I think that you have sort of misused of this particular function here.