Question

EDIT* I have updated with some codes that should solve my problem, but now I cant see any img when page 2 loads. I am still doing something wrong.

Here is how it looks like

http://i664.photobucket.com/albums/vv8/Lapplander890/stackHelp2_zpseb38de6e.jpg

I am struggeling for hours to get my php photogallery to work. It is an assignment for school so I am not allowed to use any JS at all.

The problem is that I dont know how to achive this. On page one there is 6 picture of space ships. If you click on one of the shpips you come to the second page, and that space ship is displayed. Beside the spaceships is two arrows for navigation so you can chose back and forth image.

Everything working except that bloody ship you click on on first page does not get displayed direct on second page!

Here is my photogallery with fault. http://lavis.se/ass_6/labb_del2_2.php

Here is the first page

<?php
session_start(); 
//$_SESSION['ship'] = 1;
/*
$_SESSION['ship'] = ((isset($_SESSION['ship'])) ? 
$_SESSION['ship'] : 0); 


if(isset($_GET['add'])){ 
     $_SESSION['ship']++; 

}
if(isset($_GET['sub'])){ 
     $_SESSION['ship']--; 

}

*/
?>

<!DOCTYPE html>


<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>ass6</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->

        <link rel="stylesheet_del2" href="css/normalize.css">
        <link rel="stylesheet" href="main_del2.css">
        <script src="js/vendor/modernizr-2.6.2.min.js"></script>
        <script src="main_del2.min.js"></script>
         <script src="jquery-1.2.6.pack.js.js"></script>
    </head>
    <body>

        <div id="imgwrap">
                <h1>Spaceships</h1>

                    <a href="basicForm2.php?value=1"><img src="img/space/planetes_thumb.jpg" alt="serenity"/></a>
                    <a href="basicForm2.php?value=2"><img src="img/space/serenity_thumb.jpg" alt="serenity"/></a>
                    <a href="basicForm2.php?value=3"><img src="img/space/battlestar_thumb.jpg" alt="battlestar"/></a>
                    <a href="basicForm2.php?value=4"><img src="img/space/enterprise_thumb.jpg" alt="enterprise"/></a>
                    <a href="basicForm2.php?value=5"><img src="img/space/millenium_thumb.jpg" alt="millenium"/></a>
                    <a href="basicForm2.php?value=6"><img src="img/space/integrity_thumb.jpg" alt="integrity"/></a>


        </div>
   <body>   



    </body>
</html>
>

And second page

<?php
session_start(); 
if(isset($_GET['value'])){ 
     $_SESSION['ship'] = $_GET['value'];
}
if(!isset($_SESSION['ship']))
      $_SESSION['ship'] = 1;


if(isset($_GET['add'])){ 
     $_SESSION['ship']++; 

}
if(isset($_GET['sub'])){ 
     $_SESSION['ship']--; 

}

?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>ass6</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->

       <link rel="stylesheet" href="main_del2.css" type="text/css"/>

    </head>

<body>  



</form> 
<?php
if ($_SESSION['ship']  > 6)
{
    $_SESSION['ship']  = 1; 
}
if ($_SESSION['ship']  < 1)
{
    $_SESSION['ship']  = 6; 
}
echo $_SESSION['ship'] 
?>
<a href="labb_del2_2.php"><h2> Spaceships </h2></a>
        <div id="imgcontainer">
            <form action="basicForm2.php" method="get"> 
<input type="submit" name="add" value="add" id="right"/> 
</form> 

<!--
<img src="fileDir2.php" alt="Image created by a PHP script" width="100" height="80">
-->
<form action="basicForm2.php" method="get"> 
<input type="submit"  name="sub" value="sub" id="left"/> 
                <?php


if ( $_SESSION['ship']  === 1 ) {
        echo "<img src=\"img/space/planetes.jpg\"/>";   }
    if ( $_SESSION['ship']  === 2 ) {
        echo "<img src=\"img/space/serenity.jpg\"/>";   }   
      if ( $_SESSION['ship']  === 3 ) {
        echo "<img src=\"img/space/battlestar.jpg\"/>"; }

if ( $_SESSION['ship']  === 4 ) {
    echo "<img src=\"img/space/enterprise.jpg\"/>"; }   
     if ( $_SESSION['ship']  === 5 ) {
            echo "<img src=\"img/space/millenium.jpg\"/>";  }
if ( $_SESSION['ship']  === 6 ) {
             echo "<img src=\"img/space/integrity.jpg\"/>"; }   
        ?>


        </div>

</html>
Was it helpful?

Solution

You are forgetting to read the parameter "value" of the 2nd page:

You should do something like that at the beginning of your second page:

if(isset($_GET['value'])){ 
     $_SESSION['ship'] = $_GET['value'];
}

Also, your html for the images in the first page should be:

 <a href="basicForm2.php?value=1"><img src="img/space/serenity_thumb.jpg" alt="serenity"/></a>
 <a href="basicForm2.php?value=2"><img src="img/space/battlestar_thumb.jpg" alt="battlestar"/></a>
 <a href="basicForm2.php?value=3"><img src="img/space/enterprise_thumb.jpg" alt="enterprise"/></a>
 <a href="basicForm2.php?value=4"><img src="img/space/millenium_thumb.jpg" alt="millenium"/></a>
 <a href="basicForm2.php?value=5"><img src="img/space/integrity_thumb.jpg" alt="integrity"/></a>
 <a href="basicForm2.php?value=6"><img src="img/space/planetes_thumb.jpg" alt="planets"/></a>

And the session asignation on the second page should be corrected like that:

$_SESSION['ship'] = ((isset($_SESSION['ship'])) ? 
$_SESSION['ship'] : 1);    

"1" instead of "0"

or better:

 if(!isset($_SESSION['ship']))
      $_SESSION['ship'] = 1;

And last, the following line on your code in page 1, is not useful, so you can delete it:

$_SESSION['ship'] = 1;// this makes second page start with the first image (and I want that pic to be //the one that is clicked.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top