Title: $_Session inside while
Last modified: August 21, 2016

---

# $_Session inside while

 *  Resolved [madsrh123](https://wordpress.org/support/users/madsrh123/)
 * (@madsrh123)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/_session-inside-while/)
 * I need to bring the $i to my target page.
    This gives me a list of track titles
   with a permalink, but on my target page I only get the last $i.
 *     ```
       <?php $args = array( 'post_type' => 'album' );
       $loop = new WP_Query( $args );
       while ( $loop->have_posts() ) : $loop->the_post();
       $i = 1;
   
       	the_title();
   
       	// Gets track ID foreach track
       	while ($i > 0) {
        	   $track = get_field ('track'.$i);
   
        	$permalink = get_permalink( $id );
        	session_start(); // STARTS VARIABLE TO SEND I TO NEXT PAGE
                  $_SESSION['variable_name'] = $i;
   
        	   if ($track != '') {
       		echo '<br><a href="' . $permalink . '"><strong>' . $i . ' <strong>' . get_field ('track'.$i) . '</a>';
       		$i++;
       	    }
       	    else {
       	        $i = 0;
       	    }
       	}
   
       endwhile; ?>
       ```
   
 * And the target page contains:
 *     ```
       session_start();
       echo $_SESSION['variable_name'];
       ```
   

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/_session-inside-while/#post-4654041)
 * Collect the tracks into an interim indexed array. Save the entire array as a 
   session variable when the loop completes. On the target page step through the
   array with a `foreach` loop and echo each element in turn.

Viewing 1 replies (of 1 total)

The topic ‘$_Session inside while’ is closed to new replies.

## Tags

 * [$_session](https://wordpress.org/support/topic-tag/_session/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [while](https://wordpress.org/support/topic-tag/while/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/_session-inside-while/#post-4654041)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
