while循环使用变量2次

时间:2017-06-02 15:12:47

标签: php mysql for-loop

大家好我有一个for循环的cicle打印我的div和来自SQL的信息,我用mysql上的设置打印滑块字段,如滑块编号,字段位置等等,问题是我有一个Modal Bootstrap要打印,但我不能打印在当前循环内。 我的问题是,无论如何要存储来自for cicle的变量,以便它可以重新使用吗?

有代码

$ID=$row['ID'];
$sql = "SELECT NUM_Slides as valmax FROM slider_settings,Paginas, slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID=$ID";
$sqlconnect =$connect->query($sql);
$sqlresult =$sqlconnect->fetch_assoc();
for ($k = 1 ; $k <= $sqlresult['valmax']; $k++){	
    echo "<div class='slider1'>";
    $sql1 = "SELECT P$k as campos, tituloP$k as titulo FROM slider_settings, Paginas,slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID = $ID";				
    $sqlconnect1 =$connect->query($sql1);
    $sqlresult1 =$sqlconnect1->fetch_assoc();
    
    echo "<div class='titulo'>
        <h2>$sqlresult1[titulo]</h2>
    </div>";
    
    for ($l = 1 ; $l <= $sqlresult1[campos]; $l++){	
        $campo = "SELECT Butao,Titulo,Texto FROM slider_config, Paginas, slider_settings where slider_config.ID = $ID and Paginas.ID = $ID and slider_settings.ID =$ID and P_NUM = $k and Campo = $l";
        $sqlconnect2 = $connect->query($campo);
        $sqlresult2 = $sqlconnect2->fetch_assoc();

        echo "<div class='part' id='part".$l."'>
            <div id='imagem' class='button' data-toggle='modal' data-target='#myModal".$l."'>
                <img src='data:image/png;base64," . base64_encode($sqlresult2['Butao']) . "'/>
            </div>
            <div id='titulo'>
                <h4>$sqlresult2[Titulo]</h4>
            </div>
            <div id='texto'>
                $sqlresult2[Texto]
            </div>
        </div>";
    }	
    echo "</div>";
}

并且有一些代码无法打印在div或modal中不会显示

$modal = "SELECT Titulo_modal , Imagem_modal , Texto_modal FROM modal_settings , Paginas where modal_settings.ID = $ID and Paginas.ID= $ID and P_NUM_modal = $k and Campo_modal = $l";
$sqlconnect33 =$connect->query($modal);
$sqlresult33 =$sqlconnect33->fetch_assoc();
for ($n = 1 ; $n <= $sqlresult1[campos]; $n++){
    
    echo "<div class='modal fade' id='myModal".$n."'>
        <div class='modal-dialog modal-lg'>
            <div class='modal-content'>
                <div class='modal-header'>
                    <h4 class ='titulopopup'>$sqlresult33[Titulo_modal]</h4>
                    <button type='button' class='close' data-dismiss='modal'>
                    <span aria-hidden='true'>&times;</span></button>
                </div>
                <div class='modal-body'>
                    <div class='imagem'>
                        <img src='data:image/png;base64," . base64_encode($sqlresult33['Imagem_modal']) . "'/>
                    </div>
                    <div class='texto'>
                        $sqlresult33[Texto_modal]
                    </div>
                </div>
            </div>
        </div>
    </div>";
}

注意:$ ID来自另一个文件:)并且它在代码的前半部分工作正常

1 个答案:

答案 0 :(得分:0)

也许像这样创建数组

$array = [];
for ($n = 1 ; $n <= $sqlresult1[campos]; $n++) {
    $array[$k] = $i;
}

这样你可以重用你在其他for循环中存储在数组中的var,如

foreach ($array as $k => $i) {
    // and u get all your var :)
}

使用你的代码你可以做些事情,比如

$ID=$row['ID'];
// here
$array = [];
//
$sql = "SELECT NUM_Slides as valmax FROM slider_settings,Paginas, slider_config where slider_settings.ID = $ID and Paginas.ID= $ID
and slider_config.ID=$ID";
$sqlconnect =$connect->query($sql);
$sqlresult =$sqlconnect->fetch_assoc();
for ($k = 1 ; $k <= $sqlresult['valmax']; $k++){    
    echo "<div class='slider1'>";
        $sql1 = "SELECT P$k as campos, tituloP$k as titulo FROM slider_settings, Paginas,slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID = $ID";              
        $sqlconnect1 =$connect->query($sql1);
        $sqlresult1 =$sqlconnect1->fetch_assoc();
            echo "<div class='titulo'>
                <h2>$sqlresult1[titulo]</h2>
            </div>";
        for ($l = 1 ; $l <= $sqlresult1[campos]; $l++){ 
            // here
            $array[$k] = $l;
            $campo = "SELECT Butao,Titulo,Texto FROM slider_config, Paginas, slider_settings where slider_config.ID = $ID and Paginas.ID = $ID and slider_settings.ID =$ID and P_NUM = $k and Campo = $l";
            $sqlconnect2 = $connect->query($campo);
            $sqlresult2 = $sqlconnect2->fetch_assoc();

            echo "
            <div class='part' id='part".$l."'>
            <div id='imagem' class='button' data-toggle='modal' data-target='#myModal".$l."'>
            <img src='data:image/png;base64," . base64_encode($sqlresult2['Butao']) . "'/>
            </div>
            <div id='titulo'>
                <h4>$sqlresult2[Titulo]</h4>
            </div>
            <div id='texto'>
                $sqlresult2[Texto]
            </div>
            </div>";
        }   
    echo "</div>";
}