Home > Blockchain >  dynamic tab using php
dynamic tab using php

Time:05-26

Hi everyone I'm niubbie in php.I have a problem with tab. I would like the tabs on their click to show a different topic. All this using php and calling the db.

My DB:

giorno pranzo cena
lunedi 12:00 20:00
martedi 12:00 20:00
mercoledi 12:00 20:00
giovedi 12:00 20:00
venerdi 12:00 20:00

Days are represented by tabs and when I click on a different day I want it to show lunch and dinner of that particular day.

My code:

 <section >
        <div >
            <div >
                <div >
                    <h6 >Orari</h6>
                </div>
            </div>
            <div >
                <div >
                    <div >
                   
                        <!-- start tab navigation -->
                        <ul >
                        <?php 
                                $sql = "SELECT * FROM orari_ristorante ";
                                $risultato = mysql_query($sql) or die(mysql_error()."<br>Impossibile eseguire l'interrogazione");
                                $i=0;
                                while ($riga = mysql_fetch_assoc($risultato)){  
                        ?>
                        <?php if($i == 0){?>

                        <li ><a  href="#tab-nine1" data-toggle="tab"><?php echo $riga['giorno'];?></a></li>
                        <?php }else{?>  
                        <li ><a  href="#tab-nine1" data-toggle="tab"><?php echo $riga['giorno'];?></a></li>

                        <?php }
                            $i  ;
                            }?>    
                        </ul>
                        <!-- end tab navigation -->
                    </div>
                    <div >
                        <!-- start tab content -->
                        <div  id="tab-nine1">
                            <div  id="accordion1" data-active-icon="icon-feather-minus" data-inactive-icon="icon-feather-plus">
                                <!-- start accordion item -->
                               
                                
                                <div >
                                    <div >
                               
                                <?php 

                                    $sql = "SELECT pranzo,cena FROM orari_ristorante LIMIT 1";
                                    $risultato = mysql_query($sql) or die(mysql_error()."<br>Impossibile eseguire l'interrogazione");
                                    
                                    while ($riga = mysql_fetch_assoc($risultato)){ 
                                    ?>
                                        <span >Pranzo</span>
                                        <a  data-toggle="collapse" data-parent="#accordion1">
                                            <div >
                                                <span ><?php echo $riga['pranzo'] ;?></span>
                                                
                                            </div>
                                            
                                        </a>
                                        <a href="#" ><span class='prenota'>PRENOTA</span></a>

                                        <span >Cena</span>
                                        <a  data-toggle="collapse" data-parent="#accordion1">
                                            <div >
                                                <span ><?php echo $riga['cena'] ;?></span>
                                                
                                            </div>
                                            
                                        </a>
                                        
                                        <a href="#" ><span class='prenota'>PRENOTA</span></a>
                                        <?php 
                                        }
                                        
                                    ?>  
                                
                                    </div>
                                    
                                </div>
                                 
                                
                            </div>
                            
                        </div>
                        <!-- end tab content -->
                    </div>
                </div>       
            </div>
        </div>
    </section>

My problem is that each tab shows all the rows and not the specific one for that day

My Problem

CodePudding user response:

You need to ensure you are using unique ID's for your contents and using them in your href of the tab.

Reading your code it looks like each tab is created with the same ID <li ><a href="#tab-nine1" data-toggle="tab"><?php echo $riga['giorno'];?></a></li>

I would echo out the unique id from the database eg.

href="#tab-<?echo $riga['id'];?>" (or whatever your unique column header is)

Ensure you also echo this out further down when the tab content is being created.

CodePudding user response:

Based on what you are trying to accomplish, if you limit your results to one, you will always only show the first day in the db. Here's how I would change your second while loop.

<?php
$sql = "SELECT * FROM orari_ristorante";
$risultato = mysqli_query($conn, $sql) or die(mysqli_error()."<br>Impossibile eseguire l'interrogazione");
$i = 0;
while ($riga = mysqli_fetch_assoc($risultato)){
  if($i == 0){
    $css = ""
  }else{
    $css = "display:none"
  }
?>
  <div  style="<?php echo $css ;?>">; 
    <span >Pranzo</span>
    <a  data-toggle="collapse" data-parent="#accordion1">
        <div >
            <span ><?php echo $riga['pranzo'] ;?></span>

        </div>

    </a>
    <a href="#" ><span class='prenota'>PRENOTA</span></a>

    <span >Cena</span>
    <a  data-toggle="collapse" data-parent="#accordion1">
        <div >
            <span ><?php echo $riga['cena'] ;?></span>

        </div>

    </a>

    <a href="#" ><span class='prenota'>PRENOTA</span></a>
  </div>
    <?php

    $i  ;}

?>

</div>

It is not the solution, however you can build on top of that to accomplish what you are trying to do. Hopefully that somewhat helps.

EDIT

Just to explain, I was adding $riga["giorno"] as an kind of ID in the class, however Revbo's answer would give a clearer code when it comes to an ID

  • Related