Search code examples
javascriptphpjqueryjquery-ui-tabsjquery-tabs

Dynamic Jquery Tabs based on Php foreach


I have this php script:

          <?php $bets = $wpdb->get_results( 'SELECT * FROM `bet` WHERE match_id = '. $_GET['match_id'] ); ?>
                       <?php foreach($bets as $bet): ?> 

                <?php echo $bet->bet_name; ?>

                <?php $choices = $wpdb->get_results( 'SELECT * FROM `choice` WHERE bet_id = '. $bet->id );  //fetch choices 
                ?>
                    <?php
                    if(count($choices))
                    {
                        foreach($choices as $choice)
                        {

                            echo "". $choice->choice_name . "";
                                                        }
                        }
                    else
                    {
                        echo "<h3> Not found, sorry </h3>";
                    }
                    ?>
            <?php endforeach; ?>    

I need to create a jquery tabs based on these data.

I need a tab foreach $bet that contain the relative $choice.

How can i do it ?


Solution

  • If I understand what you need, You need to create both the tabs and their content divs.

    Loop through $bets twice, once for the tabs, once for the the panes.

        <?php $bets = $wpdb->get_results( 'SELECT * FROM `bet` WHERE match_id = '. $_GET['match_id'] ); ?>
        <?php $count = 0; ?>
        <div id="tabs">
            <ul>
                <?php foreach($bets as $bet): ?> 
                <li><a href="#tabs-<?php echo $count ?>"><?php echo $bet->bet_name; ?></a></li>
                <?php $count++; ?>
                <?php endforeach; ?> 
            </ul>                
    
    
            <?php $count = 0; ?>
            <?php foreach($bets as $bet): ?> 
                <div id="#tabs-<?php echo $count ?>">
            <?php $choices = $wpdb->get_results( 'SELECT * FROM `choice` WHERE bet_id = '. $bet->id );  //fetch choices ?>
            <?php
                if(count($choices))
                {
                foreach($choices as $choice)
                {
    
                    echo "". $choice->choice_name . "";
                                                }
                }
                else
                {
                echo "<h3> Not found, sorry </h3>";
                }
            ?>
           <?php $count++; ?>
            </div>
           <?php endforeach; ?> 
    
        </div?>
    

    P.S. I think a for loop might work better for you than a foreach loop