Foreach:如果当前项值为空,则获取最后一次迭代的值

时间:2015-10-18 20:45:46

标签: php foreach

我正在使用foreach在数据库中保存表单数据。

并且,有时某些值为空,因为它与输入的最后一个值相同。我想(如果当前迭代值为空)获取最后一个迭代值并打印它。

你有解决方案吗?

谢谢!

<?php
                    $html = file_get_contents("Intranet link");
                    function parseTable($html){

                        $dom = new domDocument;
                        $dom->loadHTML($html);
                        $dom->preserveWhiteSpace = true;

                        $tables = $dom->getElementById('tblResultats');
                        $rows = $tables->getElementsByTagName('tr');

                        return $rows;
                    }

                    $rows = parseTable($html);

                    $i = 1;
                    $count = 0;
                    foreach ($rows as $row){

                     if($i <> 1 ){

                        $cols = $row->getElementsByTagName('td');
                        if (preg_match('/visite/i',$cols->item(0)->nodeValue)) { 

                        $count = $count-1; 

                        }else{ 

                        $cols->item(6)->nodeValue = str_replace('  ', '', $cols->item(6)->nodeValue);
                        $train_nbr = preg_replace("/[^0-9,.]/", "", $cols->item(6)->nodeValue);

                        if($train_nbr == ""){
                            $train_nbr = "00000";
                        }
                        ?>
                        <div id="criter_<?php echo $count; ?>" class="panel <?php if($cols->item(4)->nodeValue == " -Z"){ echo "panel-z-series"; }elseif($cols->item(4)->nodeValue == " -X"){ echo "panel-x-series"; }elseif($cols->item(4)->nodeValue == " BB"){ echo "panel-bb-series"; }elseif($cols->item(4)->nodeValue == " -B"){ echo "panel-b-series"; } ?>">
                            <input type="hidden" name="trainid_<?php echo $count; ?>" value="<?php echo $train_nbr; ?>">
                            <input type="hidden" name="traintype_<?php echo $count; ?>" value="<?php echo $cols->item(4)->nodeValue; ?>">
                            <input type="hidden" name="userid_<?php echo $count; ?>" value="<?php echo $_SESSION["Auth"]["username"]; ?>">
                            <input type="hidden" name="entrydate_<?php echo $count; ?>" value="<?php echo date("d/m/Y"); ?> <?php echo $cols->item(2)->nodeValue; ?>">
                            <input type="hidden" name="leftdate_<?php echo $count; ?>" value="<?php echo $cols->item(8)->nodeValue; ?>">
                            <input type="hidden" name="openeddate_<?php echo $count; ?>" value="<?php echo date("d/m/Y H:i:s"); ?>">
                            <input type="hidden" name="entrynumber_<?php echo $count; ?>" value="<?php echo $cols->item(0)->nodeValue; ?>|<?php echo $cols->item(1)->nodeValue; ?>">
                            <input type="hidden" name="leftnumber_<?php echo $count; ?>" value="<?php echo $cols->item(7)->nodeValue; ?>|<?php echo $cols->item(9)->nodeValue; ?>">

                            <div class="panel-heading">

                                <button onclick='addValue();$("#criter_<?php echo $count; ?>").remove();' class="btn btn-default"><i class="fa fa-trash"></i></button>
                                &nbsp;<strong><?php echo $cols->item(2)->nodeValue; ?> </strong>
                                <div style="font-size:18px;margin-top:-30px"><?php if($cols->item(4)->nodeValue == " -Z"){ echo "<strong>Z</strong>"; }elseif($cols->item(4)->nodeValue == " -X"){ echo "<strong>X</strong>"; }elseif($cols->item(4)->nodeValue == " BB"){ echo "<strong>BB</strong>"; }elseif($cols->item(4)->nodeValue == " -B"){ echo "<strong>B</strong>"; } echo $train_nbr; ?></div>
                                <span class="pull-right">

                                    <div class="input-group date picker" >

                                        <input type="text" class="form-control" value="<?php echo $cols->item(8)->nodeValue; ?>"/>
                                        <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-calendar"></span>
                                        </span>

                                    </div>

                                </span>
                                <br>
                                <!-- If values from current iteration are empty, get the values from the -1 iteration -->
                                Train numéro <strong><?php echo $cols->item(0)->nodeValue; ?></strong> en provenance de <strong><?php echo $cols->item(1)->nodeValue; ?></strong> et à destination de <strong><?php echo $cols->item(9)->nodeValue; ?></strong> (<strong><?php echo $cols->item(7)->nodeValue; ?></strong>)
                            </div>
                            <div class="panel-body">

                                <div id="room_criter_<?php echo $count; ?>">
                                    <p>
                                        Commentaire sur l'engin :<br>
                                        <input type="text" style="width:100%;display: inline-block;margin-bottom:8px;" name="comments_<?php echo $count; ?>" placeholder="Commentaire éventuel sur cet engin (imprévu par exemple)" class="form-control">

                                        Planification des événements :<br>
                                        <input type="text" style="width:70%;display: inline-block;margin-bottom:8px;" name="eventcriter_<?php echo $count; ?>[]" placeholder="Evènement ou intervention planifiée sur cet engin" class="form-control">
                                        <input type="hidden" name="lastmodified_<?php echo $count; ?>[]" value="<?php echo $_SESSION['Auth']['username']; ?>" class="form-control">
                                        &nbsp;&nbsp;
                                        <select style="width:19%;display: inline-block;" class="form-control" name="eventcriterstatus_<?php echo $count; ?>[]">
                                            <option value="0" selected>Non effectuée</option>
                                            <option value="1">En cours</option>
                                            <option value="2">Terminée</option>
                                        </select>
                                        &nbsp;&nbsp;&nbsp;
                                        <a style="display: inline-block;border-radius: 4px;" class="btn btn-success" onclick="add_fields(<?php echo $count; ?>);"><span class="fa fa-plus"></span> &nbsp;Ajouter</a>
                                    </p>
                                </div>
                            </div>
                        </div>

                    <?php
                        }

                        } 
                        $i++;
                        $count++;

                    }


                ?>

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确的任务,但这是我的尝试。

for ($i = 2, $n = count($rows); $i < $n; ++$i) {
    $row = $rows[$i - 1];
    $prevRow = $rows[$i - 2];

    $cols = $row->getElementsByTagName('td');
    $prevCols = $prevRow->getElementsByTagName('td');

    $values = [];
    for ($j = 0; $j < ITEMS_COUNT; ++$j) {
        $value = $cols->item($j)->nodeValue;
        if (empty($value)) {
            $value = $prevCols->item($j)->nodeValue;
        }
        $values[$j] = $value;
    }

    //And then you can use $values[$j] instead of $cols->item($j)->nodeValue.
}