0

I'm having trouble sending userID's from a list to a different php page. Here's my code:

$eventName = $row['Name'];
$_SESSION['event'] = $eventName;
$users= array();
    while($row2 = mysqli_fetch_array($result2)){

        echo "<li>".$row2['FirstName']." ".$row2['LastName']; // print the members

        $userID = $row2['ID'];
        $users[] = $row2['ID'];             
        $_SESSION['users'] = $users; //creates session array
        echo "<a href ='removeFromEvent.php?id=$userID&event=$eventName'><span class = 'REMOVE'>Remove</span></a></li><br>";

    }


    echo implode('<br>',$_SESSION['users']); //prints the IDs
    echo "<br>". $_SESSION['event'];
    echo "<a href ='test.php'><span class = 'REMOVE'>ADD CREDITS</span></a>";

So with this code, it will print out the ID's and the name of the event at the end of each list of names on the page. But when I click on the link to move to test.php, it only shows the ID's and event name for the last event in the list. Here's all the code in test.php:

<?php
    session_start();
    echo implode('<br>',$_SESSION['users']);
    echo $_SESSION['event'];

?>

Here's how the page looks on the site:

EVENT1 NAME
   1. Member1                            REMOVE
   2. Member2                            REMOVE
                                         ADD CREDITS (call this the first link)
   member1ID
   member2ID
     event1name
EVENT2 NAME
  1. Member1                                REMOVE
                                         ADD CREDITS (and this is the second)
   member1ID
     event2name

When I click the first add credits link, it opens up test.php, but prints event2name and member1 for event2. When I click the second add credits link, it opens up test.php, and also prints the same event name and members. The number of events and members is dependent on how many are created in the database, so there could be 2 events with 2 members in each, or 20 events, all with a different number of members.

So my question is, how can I get the first link to show the first event's members and event name, and the second link to show the second event's members and event names?

S Salty
  • 11
  • 2

1 Answers1

0

Currently you're overwriting the _SESSION['event'] and _SESSION['users'] elements, therefore only the last event and its associated users will be stored.
One solution is to make $_SESSION['events'] an array, each element being the data for one event. The key is the event name (not the event id?) and each value again being an array cotaining all the users associated with this particular event.

// initialize the 'events' array in _SESSION
$_SESSION['events'] = array();
while( $row=mysqli_fetch_array($result) ) {
    // $user will store the userIDs for this event
    $users = array();
    while($row2 = mysqli_fetch_array($result2)) {
        $users[] = $row2['ID'];
        printf('<li>%s %s <a href="removeFromEvent.php?id=%s&event=%s"><span class="REMOVE">Remove</span></a></li>'
            htmlspecialchars($row2['FirstName']),
            htmlspecialchars($row2['LastName']),
            urlencode($row2['ID']),
            urlencode($row['name'])
        );
    }
    // store the userIDs under the event name in the array $_SESSION['events']
    $_SESSION['events'][ $row['Name'] ] = $users;
}

in removeFromEvent.php you can access the data like so:

if ( !isset($_GET['event'],$_GET['id']) ) {
    echo 'missing GET parameter';
}
else {
    $event = $_GET['event'];
    $id = $_GET['id'];
    if ( !isset($_SESSION['events'][$event]) ) {
        echo 'no such event in _SESSION';
    }
    else if ( !isset($_SESSION['events'][$event][$id]) ) {
        echo 'no such user in event';
    }
    else {
        ...
  }

Notes:
* Maybe a JOIN query instead of nested queries would be a good idea.
* The data/parameters for a DELETE operation should not be transfered via GET, but rather via POST.

Community
  • 1
  • 1
VolkerK
  • 92,020
  • 18
  • 157
  • 222
  • Thanks for the help, but my question was specifically about the ADD CREDITS button, not the Remove, because that already works the way it is, but I will take your advice. If I am overriding the $_SESSION variables, wouldn't it show the info for the last add credits button clicked? Would there be a way to have it do that without storing everything in a large array, because some events can get up to 200 people? Thanks for any input you can offer! – S Salty Aug 24 '15 at 15:00
  • I just tried to implement your code, and I still am having the same issue where it will print out only the ID's for the second event. – S Salty Aug 24 '15 at 15:18