I have images generated by while loop php based on how many course user have
when user press on one of those images it will send him to that course page
but all images send to one course page
this my php code in** home page **

 <?php
    $con=mysqli_connect("localhost","FYP","123","FYP");
    $sql= mysqli_query($con, "SELECT C_Code FROM F_COURSES WHERE F_ID=".$_SESSION['userid']);
       while($row = mysqli_fetch_array($sql)){
       echo " <a href='course.php'><div class='cc' id='tres'><img class='icon' src='bookicon1.png'><p   class='pra'>".$row['C_Code']."</p></div></a>&nbsp;";
       $C_Code = $row['C_Code'];
       $_SESSION['course'] = $C_Code;}


  ?>

and this code for **course page **

 this is    <?php
            echo      $_SESSION['course'];
      ?> course. 

I hope my question make sense and I appreciate your help

Member Avatar for diafol

You are most likely missing the

session_start();

at the top of EVERY page, right after the open php tag (<?php).

No hold on - had another look.

You are overwriting the session veriable on every iteration of the loop, so the course will always be that of the last person in the loop

commented: I already have the session_start(); at the top of both pages what is the right way to do this and thank you for you help +0

I already have the session_start(); at the top of both pages
what is the right way to do this
and thank you for you help

Member Avatar for diafol

No hold on - had another look.
You are overwriting the session veriable on every iteration of the loop, so the course will always be that of the last person in the loop

THat was what I said. Example of the type of thing you're doing:

while($row = ...){

    echo $row['field1'];
    $_SESSION['secret'] = $row['field2'];

}

After first loop, you have:

echoed field1 of row = 1
set $_SESSION['secret'] to field2 of row = 1

After second loop, you have:

echoed field1 of row = 1
set $_SESSION['course'] to field2 of row = 1
echoed field1 of row = 2
set $_SESSION['course'] to field2 of row = 2 [YOU HAVE OVERWRITTEN THIS]

After second loop, you have:

echoed field1 of row = 1 to the screen
set $_SESSION['course'] to field2 of row = 1
echoed field1 of row = 2 to the screen
set $_SESSION['course'] to field2 of row = 2 [YOU HAVE OVERWRITTEN THIS]
echoed field1 of row = 3 to the screen
set $_SESSION['course'] to field2 of row = 3 [YOU HAVE OVERWRITTEN THIS AGAIN]

etc

So when you click ANY link to take you to course.php the value of $_SESSION['course'] will always be the LAST value assigned to it in the loop, in the example above, it will be field2 of row3.

An easy solution would be to have something like:

<a href="course.php?id=4">Biology</a>
<a href="course.php?id=1">Chemistry</a>
<a href="course.php?id=9">Physics</a>

So on click, you'd go to the urls with the querystring parameter 'id'. You then extract that in the courses.php page with

$course_id = $_GET['id'];

Simpler by far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.