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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀