dongpu2694 2015-07-09 13:00
浏览 16
已采纳

如何使用PHP从数据库中按顺序填充选项卡?

This is probably an easy question but does anyone know how to insert data from a database into tabs autonomously ordered by a certain field?

Let me clarify, I need each tab to display data from my database ordered by the date they were added. So the latest in tab 1, the next in tab 2, and so on.

I've had this working in an accordion style where I did the database query first with a limit of five which repeated my code that inserted the info from the database up to five times. I have been trying to figure out a way similar to this except with tabs instead. Even if I could get a database query to put the latest result in tab 1, then do another query in tab 2 to find the second newest result, and so on.

Thanks for your time. Sorry if this is a silly question. :)

Sorry for leaving out code. What I am hoping to do is to call data from a database with code simular to

<?php       
        $subject_set = mysql_query("Select * FROM database WHERE column1 like 'value' ORDER BY date_added DESC LIMIT 4", $connection);                  
        if (!$subject_set){
        die("Database connection failed: " . mysql_error());
        }
        while ($subject = mysql_fetch_array($subject_set)){?>

<ul class="tab-links">
    <li class="active"><a href="#tab-1">Tab #1</a></li>
    <li><a href="#tab-2">Tab #2</a></li>
    <li><a href="#tab-3">Tab #3</a></li>
    <li><a href="#tab-4">Tab #4</a></li>
</ul>

<div class="tab-content">
    <div id="tab-1" class="tab active">
        <?=$subject['column1']?>
        <?=$subject['column2']?>
        <?=$subject['column3']?>
        <?=$subject['column4']?> 
    </div>

    <div id="tab-2" class="tab">
        <?=$subject['column1']?>
        <?=$subject['column2']?>
        <?=$subject['column3']?>
        <?=$subject['column4']?> 
    </div>

    <div id="tab-3" class="tab">
        <?=$subject['column1']?>
        <?=$subject['column2']?>
        <?=$subject['column3']?>
        <?=$subject['column4']?> 
    </div> 

    <div id="tab-4" class="tab">
        <?=$subject['column1']?>
        <?=$subject['column2']?>
        <?=$subject['column3']?>
        <?=$subject['column4']?> 
    </div>  
</div>

I was hoping for code similar to this or probably only have one tab which is repeated depending on the limit in the database query.

  • 写回答

1条回答 默认 最新

  • duanpeng1532 2015-07-09 13:11
    关注

    First, the query:

    SELECT column1, column2 FROM tableName ORDER BY dateColumn DESC LIMIT 5;
    

    Let's assume you put this data into an array called $data.

    If your tabs are HTML <li> elements then you can create a function for creating the HTML:

    function buildList($data){
      $output = "";
      if(!empty($data)){
        $output .= "<ul>";
        foreach($data as $row){
          $output .= "<li>" . $row . "</li>";
        }
        $output .= "</ul>";
      }
      return $output;
    }
    

    I don't think I can provide any more detail without knowing the structure of your table, or the expected output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题