Help to create an image loop with $_SESSION

Asked

Viewed 151 times

0

I’m using this code below working perfect with an image loop coming from the BD.

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

However I am trying to create a simulator and I would like to adapt this above loop to work in the code below that is bringing the images of $_SESSION.

<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 build the loop to work, or even if there’s a way to make it work that way. Looping images from $_SESSION.

If friends can give me a light, I’d be grateful.

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

Hugs to all.

2 answers

1


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 in between // Inicio da Sessão do Banner das páginas menu and the last </div> for:

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 }
}
  • Hello Miguel, but once I do not understand your reasoning, because you tell me to make the first code and then have me replace everything including the first code? I don’t get it... And Voce talks about looking for the images in the BD, but I don’t want access to the BD, because the initial images are already inserted in SESSION as img01, 02.... It would be possible to assemble this code so that it works with your reasoning, based on the data I passed, but without using the connection with the BD of how it works at the address (http://www.simuleseusite.com/).

  • Basically it is if you do not insert in Session the images will have to fetch them from the BD again to have them available in the Prox instead. I put some explanations in comment

  • OK, but I tried a lot of ways to build the code and without success. ?

  • What’s the mistake that comes?

  • I’ll publish the page on the server so you. see what’s going on, thanks?

  • This last key should not be closed with <?php }} ?>

  • I published closing the last key. See the result on (http://www.simuleseusite.com/).

  • See how the code turned out in my reply:

  • Not to see anything like this in php. There are some in javascript as well

Show 5 more comments

0

I managed to make it work by making some changes to the code of friend Miguel.

Below working code:

<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.: Don’t forget the @session_start(); and of include 'conexão.php'; at the beginning of the page before any HTML, OK?

I hope you help other people with the same problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.