0

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

2
Contributors
3
Replies
28
Views
2 Years
Discussion Span
Last Post by diafol
1

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

Edited by diafol

Votes + Comments
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

2

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.