WordPress.org

Forums

[resolved] $_Session inside while (2 posts)

  1. madsrh123
    Member
    Posted 1 year ago #

    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'];
  2. bcworkz
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic