Help to create a loop of the images with $ _SESSION

0

I'm using this code below running perfect with an image loop coming from the DB.

<div align="center" u="slides" style="cursor: move; position: absolute; left: 0px; top: 0px; width: 600px; height: 300px; overflow: hidden;">
<?php
include "conexao.php";
$imagem = $_POST['imagem'];
$texto = $_POST['texto'];
$query = mysql_query("SELECT * FROM banner ORDER BY RAND() LIMIT 4");
while($res = mysql_fetch_array($query)){    
?>
    <div>
        <img src="img_banner/<?php echo $res['imagem']; ?>" alt="image slider" />
            <div u=caption t="*" class="captionOrange_"  style="position:absolute; left:20px; top: 30px; width:300px; height:30px;"> 
                <?php echo $res['texto']; ?>
            </div>
    </div>
<?php } ?>
</div>

But I'm trying to create a simulator and I would like to adapt this loop above so that it works in the code below that is bringing the $ _SESSION images.

<div align="center" u="slides" style="cursor: move; position: absolute; left: 0px; top: 0px; width: 600px; height: 300px; overflow: hidden;">
    <?php
    // Inicio da Sessão do Banner das páginas menu
    @session_start();
        if(!isset($_SESSION['banner'])){ // Se a Session não for iniciada
            $img01 = 'img_banner/01.png'; // Carrega esse conteúdo
            $img02 = 'img_banner/02.png'; // Carrega esse conteúdo
            $img03 = 'img_banner/03.png'; // Carrega esse conteúdo
            $img04 = 'img_banner/04.png'; // Carrega esse conteúdo
            $img05 = 'img_banner/05.png'; // Carrega esse conteúdo
            $img06 = 'img_banner/06.png'; // Carrega esse conteúdo
            $img07 = 'img_banner/07.png'; // Carrega esse conteúdo

    }else{ // Se não
    if(isset($_SESSION)) { // Se a Session for iniciada
    $img01 = ''.$_SESSION['banner'].''; // Carrega esse conteúdo
    }}
    ?>
    <div>
        <img src="<?php echo $img01 ?>" alt="image slider" />
    </div>
    <div>
        <img src="<?php echo $img02 ?>" alt="image slider" />
    </div>
    <div>
        <img src="<?php echo $img03 ?>" alt="image slider" />
    </div>
    <div>
        <img src="<?php echo $img04 ?>" alt="image slider" />
    </div>
    <div>
        <img src="<?php echo $img05 ?>" alt="image slider" />
    </div>
    <div>
        <img src="<?php echo $img06 ?>" alt="image slider" />
    </div>
    <div>
        <img src="<?php echo $img07 ?>" alt="image slider" />
    </div>        
</div>

But I'm not sure how to mount the loop to work, or even how to make it work that way. Using looping in images from $ _SESSION.

If friends can give me a light, I'll be grateful.

To view the banner working with images coming from $ _SESSION, go to: www.simuleseusite.com

Hugs to all.

    
asked by anonymous 06.06.2016 / 14:04

2 answers

1

You can do:

<?php
session_start(); // isto tem de estar antes de tudo na página, no topo
...
$_SESSION['banners'] = array();
while($res = mysql_fetch_array($query)){
    $_SESSION['banners'][] = 'img_banner/' .$res['imagem']; 
    ?>
    <div>
        <img src="img_banner/<?php echo $res['imagem']; ?>" alt="image slider" />
            <div u=caption t="*" class="captionOrange_"  style="position:absolute; left:20px; top: 30px; width:300px; height:30px;"> 
                <?php echo $res['texto']; ?>
            </div>
    </div>
<?php } ?>

Then you can replace everything you have between // Inicio da Sessão do Banner das páginas menu and last </div> with:

session_start(); // isto tem de estar antes de tudo na página, no topo
...

if(!isset($_SESSION['banners'])){ // caso não existam os banners na '$_SESSION' vamos busca-las à base de dados
    $conn = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
    $result = mysql_query("SELECT * FROM banner ORDER BY RAND() LIMIT 4", $conn);

    $_SESSION['banners'] = array();
    while($res = mysql_fetch_assoc($result)){ // vamos criar imagens (html) e aproveitamos para inserir na $_SESSION['banners'], para a proxima vez já as termos;
        $_SESSION['banners'][] = 'img_banner/' .$res['imagem']; ?>
        <div>
            <img src="<?php echo $banner; ?>" alt="image slider" />
        </div>
    <?php }
}
else { // aqui é se já as tivermos em $_SESSION['banners']
    foreach($_SESSION['banners'] as $banner) { ?>
        <div>
            <img src="<?php echo $banner; ?>" alt="image slider" />
        </div>
    <?php }
}
    
06.06.2016 / 14:41
0

I managed to make it work by making some changes to your friend's code.

Below code working:

<div align="center" u="slides" style="cursor: move; position: absolute; left: 0px; top: 0px; width: 600px; height: 300px; overflow: hidden;"> <?php 
// Inicio da Sessão do Banner das páginas menu 
if(!isset($_SESSION['banner01'])){ // caso não existam os banners na '$_SESSION' vamos busca-las à base de dados 
$result = mysql_query("SELECT * FROM banner ORDER BY RAND() LIMIT 4", $conexao); 

$_SESSION['banner01'] = array(); 
while($res = mysql_fetch_assoc($result)){ // vamos criar imagens (html) e aproveitamos para inserir na $_SESSION['banners'], para a proxima vez já as termos; 
$_SESSION['banner01'][] = 'img_banner/'.$imagem; ?> 
   <div> 
       <img src="img_banner/<?php echo $res['imagem']; ?>" alt="image slider" /> 
   </div> 
<?php } 
} 
else { // aqui é se já as tivermos em $_SESSION['banners'] 
foreach($_SESSION['banner01'] as $imagem) { ?> 
   <div> 
       <img src="img_banner/<?php echo $res['imagem']; ?>" alt="image slider" /> 
   </div> 
<?php }}?> 
   </div>

PS: Do not forget the @session_start(); and include 'conexão.php'; at the beginning of the page before any HTML, OK?

I hope it helps other people with the same problem.

    
07.06.2016 / 19:14