Tabs with different PHP Query’s

Asked

Viewed 143 times

-2

I’m having difficulty creating a page with two tabs but each with a different query.

    <ul>
    <li><a href="#tabs-1">TAB1</a></li>
<li><a href="#tabs-2">TAB2</a></li>
    </ul>
    <?php

      include("conectar.php");

echo'<div id="tabs-1">
$sql = "select * from Tabelas
where Campo1 and campo2"';
echo'   $qr = mysql_query($sql) or die(mysql_error())';
echo'   while($exibe = mysql_fetch_array($qr)){

     <li class="ui-widget-content"><h3><a href="NaoautorizadoMostrar.php?id='.$exibe['id'].'">'.$exibe['Nome'].'</h3></a></li>;
  } </div>';


    echo'<div id="tabs-2">
$sql = "select * from Tabelas
where Campo3 and campo4"';
echo'   $qr = mysql_query($sql) or die(mysql_error())';
echo'   while($exibe = mysql_fetch_array($qr)){

     <li class="ui-widget-content"><h3><a href="NaoautorizadoMostrar.php?id='.$exibe['id'].'">'.$exibe['Nome'].'</h3></a></li>;
  } </div>';

error:

Notice: Undefined variable: displays in.

Thank you

  • by the error you posted, the "displays" variable was not defined. Where you set the "displays"?

  • There are many syntax errors because you treat php as a string and want to display them

2 answers

1

It is unclear what content you want to display in the tab. It would also be interesting for you to inform which javascript library you are using to create the tabs.

Aim to be a little more organized, this will facilitate the identification of errors and maintenance of your code.

  1. Avoid using echo to generate HTML content, this makes it difficult to read the code.
  2. Also avoid inserting queries in the middle of your HTML code, try to centralize queries at the beginning of the script or preferably adopt one MVC framework.
  3. From the point of view of HTML, your code is wrong. It has Lis without OL/UL parents.
  4. Since the HTML of each tab is the same, you can place the contents of each tab in an array and generate the different tabs through a foreach, without having to repeat code.

Follow my suggestion of organization:

<?php
include("conectar.php"); 

// Dados da tab1
$sql = "select * from Tabelas where Campo1 and campo2";
$rs1 = mysql_query($sql) or die(mysql_error());

// Dados da tab2
$sql = "select * from Tabelas where Campo3 and campo4";
$rs2 = mysql_query($sql) or die(mysql_error());

// Tabs
$tabs = array($rs1, $rs2);     
?>
<html>
...
   <!-- Geração do HTML dos links para as tabs -->
   <ul>   
        <?php $c = 1;?>
        <?php foreach($tabs as $rs):?>
            <li><a href="#tab-<?php echo $c;?>">Aba <?php echo $c; ?></a></li>
        <?php endforeach; ?>
   </ul>

   <!-- Geração do HTML do CONTEUDO das tabs -->
   <?php $c = 1;?>
   <?php foreach($tabs as $rs):?>
      <div id="tabs-<?php echo $c++;?>">
          <!-- Conteúdo da aba. -->
          <?php while($row=mysql_fetch_array($rs)): ?>                    
              <?php echo $row['Nome'];?>
          <?php endwhile;?>
      </div>
   <?php endforeach; ?>
...
</html>

-1

Your code is unstructured, I think that solves your problem:

<?php

      include("conectar.php");

echo '<div id="tabs-1">';
$sql = "select * from Tabelas where Campo1 and campo2";
$qr = mysql_query($sql) or die(mysql_error());
while($exibe = mysql_fetch_array($qr)){

    echo '<li class="ui-widget-content"><h3><a href="NaoautorizadoMostrar.php?id='.$exibe['id'].'">'.$exibe['Nome'].'</h3></a></li>';
  }
 echo '</div>';


   echo'<div id="tabs-2">';
 $sql = "select * from Tabelas where Campo3 and campo4";
 $qr = mysql_query($sql) or die(mysql_error());
 while($exibe = mysql_fetch_array($qr)){

    echo '<li class="ui-widget-content"><h3><a href="NaoautorizadoMostrar.php?id='.$exibe['id'].'">'.$exibe['Nome'].'</h3></a></li>';
  } 

  echo '</div>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.