Loop through ticket names

This topic contains 5 replies, has 2 voices, and was last updated by  Stephen Harris 9 years, 9 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #11725

    Hi Stephen

    I have the following code in my event-meta-event-single.php template

     $tickets = eo_get_event_tickets_on_sale( $post->ID, $post->occurrence_id );
         $ticket = array_shift( $tickets );
         $ticket_name = $ticket['name'];
         $raw_price = $ticket['price'];
         $formatted_price = eo_format_price( $raw_price, true ); //Format price with currency

    I just need to loop through all the possible ticket names for the event and display them in an unordered list.
    I’ve tried a foreach loop as follows:

    foreach($tickets as $ticket) {

                    echo '
  • ' . $ticket_name . '
  • '; }

    but it only produces one ticket name instead of the list I need.
    Can you help please

    Paul Oaten
    #11737

    Hi Paul,

    The forum ate your code :/, but does this not work?

     $tickets = eo_get_event_tickets_on_sale( $post->ID, $post->occurrence_id );
     echo '<ul>';
     foreach( $tickets as $ticket ){
         printf( '<li> %s </li>', esc_html( $ticket['name'] ) );
     }
     echo '</ul>';
    Stephen Harris
    #11744

    It almost does.

    Here’s what I have now:-

     
      <?php foreach( $tickets as $ticket ){ printf( '
    • %s
    • ', esc_html( $ticket['name'] . ' - ' . $formatted_price ) ); } ?>

    However, the first ticket name doesn’t get printed in the list and also the $formatted_price for each ticket name appears as zero.
    Is this something to do with array_shift($tickets); ?

    Can I override somehow. Continued help appreciated as ever!

    Paul Oaten
    #11745

    array_shift() removes the element from the beginning of the array and returns it.

    So you can try,

    $tickets = eo_get_event_tickets_on_sale( $post->ID, $post->occurrence_id );
    echo '<ul>';
    foreach( $tickets as $ticket ){
        $raw_price = $ticket['price'];
        $formatted_price = eo_format_price( $raw_price, true ); //Format price with currency
        printf( '<li> %s </li>', esc_html( $ticket['name'] . ' - ' . $formatted_price ) );
    }
    echo '</ul>';
    Stephen Harris
    #11746

    So I amended the array line to this:

    $ticket = array( $tickets );

    and now it works fine. Does it matter that I took out the ‘shift’ instruction?

    Paul Oaten
    #11747

    The array_shift() was from another thread, and I used it because I thought there was only one ticket available (or you just wanted the price of the first ticket).

    The thing to be aware of is that array_shift() modifies the original array.

    $array = array( 'a', 'b', 'c' );
    echo array_shift( $array ); //'a';
    //$array is now array( 'b', 'c' );

    My previous post should list all tickets (and price) for an event.

    Stephen Harris
Viewing 6 posts - 1 through 6 (of 6 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.