PHP变量在相等的循环上更改自己的值

时间:2015-01-21 19:35:19

标签: php codeigniter

我有一个变量,用于输出地址的坐标。我正在打印名称之前的坐标以进行测试。在奇数循环(它在foreach循环中)它工作正常,将变量放在data-latLng属性中。在偶数循环上,它给出了不同的值 - 而不是坐标。值类似于:2.

这就是我的意思:

奇数编号的循环会打印出来:

odd

同时在偶数循环数上,data-latLng属性会输出不同的值:

even

even

以下是代码:

$area_lat_long = isset($area_lat_long[$mapCounter])?$area_lat_long[$mapCounter]:"-26.2041028, 28.047305100000017";
echo $area_lat_long;
echo '<strong>area: '. $streetAdd[$count] .'         <a class="glyphicon glyphicon-new-window" type="button" data-toggle="modal" data-target="#mapModal" data-latLng="'. $area_lat_long .'" style="cursor:pointer;"></a><br>';
$mapCounter++;

PHP代码在表格代码上方运行,上面的代码给出了坐标线,然后是区域。

正如您所看到的,$area_lat_long在每个区域之前给出坐标,但是当data-latLng使用EXACT相同的变量时,它会在偶数循环上发生变化吗?

修改 对于那些想要评论中整个循环的人来说:

foreach ($streetAdd as $key){
                print_r($area_lat_long);
                //LAT LONG
                $area_lat_long = isset($area_lat_long[$count])?$area_lat_long[$count]:"-26.2041028, 28.047305100000017";
                echo $area_lat_long;
                echo '<strong>area: '. $streetAdd[$count] .'         <a class="glyphicon glyphicon-new-window" type="button" data-toggle="modal" data-target="#mapModal" data-latLng="'. $area_lat_long .'" style="cursor:pointer;"></a><br>';
                $mapCounter++;?>
                <input type="hidden" id="street_address" name="street_address[<?php echo $count; ?>]" value="<?php echo $streetAdd[$count];?>">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>Media Type</th>
                        <th>Quantity Required</th>
                        <th>Average Asset Price</th>
                        <th><!-- Remaining Total --></th>
                        <th>More Options</th>

                    </tr>
                    </thead>
                    <tbody class="assetCounter">
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <?php

                    $j           = 0;
                    $total_used  = 0;
                    $total_bal   = isset($budget)?$budget:0;
                    $qty         = 1;
                    $i = 0;
                    foreach ($my_categories as $key) { //loop thru chosen media types
                        foreach ((array)$key as $data) {
//                                print_r($data);
                            //check valid description
                            $j++;
                            $data_description = isset($data->mec_description)?$data->mec_description:'';

                            $latitude_longitude = explode(",",$area_lat_long); //print_r($latitude_longitude);
                            $latitude = $latitude_longitude[0];
                            $longitude = isset($latitude_longitude[1])?$latitude_longitude[1]:$latitude;
                            //pricing
                            $min_price = isset($data->asg_min_price)?$data->asg_min_price:0;
                            $max_price = isset($data->asg_max_price)?$data->asg_max_price:0;
                            $average_p = ($min_price + $max_price)/2;

                            $total_used += $average_p;
                            $total_bal  -= $average_p;

                            if($total_bal < 0){
                                $total_bal = 0;
                            }

                            if($average_p == 0){
                                $title = "Pricing information not yet available from Media Owners";
                            } else {
                                $title = "NOTE: These are just estimates/guidelines, latest pricing information will be received from Media Owners quotations";
                            }

                            ?>
                            <tr class="asset_<? echo $counterForAsset; ?>">
                                <td><?php
                                    echo strtoupper($mec_stuff[$i]);
                                    ?>
                                    <input type="hidden" id="media_category" name="mec_id[]" value="<?php
                                        foreach($mec_stuff as $ms) {
                                            echo $ms . ',';
                                        }
                                    ?>">
                                    <input type="hidden" id="media_category" name="media_category[]" value="<?php echo $data_description; ?>"></input></td>
                                    <td><input type="text" class="form-control q_asset_<? echo $counterForAsset; ?> med_quantity" name="med_quantity[]" id="med_quantity" placeholder="Quantity Required" value="1"/></td>
                                    <td><input type="text" readonly="true" name="avg_total[]" id="asset_<? echo $counterForAsset; ?>" class="form-control avg_asset_<? echo $counterForAsset; ?>" value="<?php echo number_format($total_bal,2); ?>" title="<?php echo $title;?>"/></td>
<!--                                    <td><input type="text" readonly="true" name="avg_total[]" id="avg_total--><?php //echo $j; ?><!--" class="form-control asset_--><?// echo $i; ?><!--" value="--><?php //echo number_format($total_bal,2); ?><!--" title="--><?php //echo $title;?><!--"/></td>-->
                                    <td><input type="text" readonly="true" name="rem_total[]" id="asset_<? echo $counterForAsset; ?>" class="form-control rem_asset_<? echo $counterForAsset; ?>"  value="<?php echo number_format($total_bal,2); ?>"/></td>
<!--                                    <td><input type="text" readonly="true" name="rem_total[]" id="rem_total--><?php //echo $j; ?><!--" class="form-control --><?// echo $i; ?><!-- asset_--><?// echo $i; ?><!--"  value="--><?php //echo number_format($total_bal,2); ?><!--"/></td>-->
                                    <td><?php echo "<a class='js-fire-modal btn btn-info' type='button' data-toggle='modal' data-mecid='$mec_stuff[$i]' href='#' name='size_button'  onclick=\"sizeModal2(1, $j, '$latitude','$longitude','$description')\">>>></a>";?></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td colspan="4" id="<?php echo $j; ?>"></td>
                            </tr>
                        <?php $i ++; $counterForAsset++; }


                    }?>
                    <tr>
                        <td> </td>
                        <td> <input type="hidden" id="hidSubtotal<?php echo $j;?>" value="<?php echo number_format($total_used,2); ?>"></td>
                        <td> Subtotal</td>
                        <td> <span id="lblSubtotal<?php echo $j; ?>"><?php echo number_format($total_used,2); ?></span> </td>
                    </tr>
                    </tbody>
                </table>
                <?php $count++;
            }

1 个答案:

答案 0 :(得分:1)

foreach($a as $b) {
    $x = isset($x[$c]) ? $x[$c] : "foo7";
    echo $x;
}

让我们“运行”那段代码,假设     $ x = array('c'=&gt;'lat,lng'); 在循环之前。

所以循环#1打印'lat,lng'。

循环#2检查isset('lat,lng'[$c])是否评估为false,因此$x将为'foo'

'foo'将被打印。

循环#3与#2相同,但检查是否isset('foo'[$c]),再次评估为false

再次打印'foo'。

这就是原因。

我不知道你在那个循环之外做了什么,大多数代码似乎什么也没做,或者意图