dongmao4486 2016-05-03 11:17
浏览 45
已采纳

如何选择db的所有表mySql并按数据显示查询顺序,表具有相同的结构

I want to make a newsbar show i need to show the last 5 query, I have 5 categories(tables) like history, nature, lifestyle, science and travel in the same database mySql all the tables have the same structure id, title, descriere, data, picture, alt, approved and I want to selet the last 5 order by data, How i can do this? and the php code to show the query

my code for a show the last 5 query order by data in the same tables is this...

$sql = "SELECT id, titlu, link, poza, alt FROM istorie WHERE approved='1' ORDER BY RAND() LIMIT 3";
$query = mysqli_query($db_conx, $sql);
$istorielist = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $id = $row["id"];
    $titlu = $row["titlu"];
    $link = $row["link"];
    $poza = $row["poza"];
    $alt = $row["alt"];
    $istorielist .= '<div id="articol-content-more"><a href="/istorie/'.$link.'"><img src="/images/'.$poza.'.jpg"class="articol-content-more-image" alt="'.$alt.'"><p class="articol-content-more-title">'.$titlu.'</p></a><span><a class="articol-content-more-afla" href="/istorie/'.$link.'">Citește mai multe</a></span><span class="articol-content-more-fl"><div class="fb-share-button" data-layout="button_count" data-href="http://esticurios.ro/istorie/'.$link.'"></div></span></div>';
}
?>
<p class="moreArticle">Mai multe articole:<br>
<?php echo $istorielist;?> <br>
<?php mysqli_close($db_conx);?>
  • 写回答

1条回答 默认 最新

  • dongshuofu0039 2016-05-03 11:30
    关注

    You can take category array static Or put all categories into one table and make category array dynamically also

    <?
    $catArray = array("history","nature", "lifestyle", "science","travel");    
    $istorielist = '';
    foreach($catArray as $k=>$v)
    {    
        $sql = "SELECT id, titlu, link, poza, alt FROM ".$v." WHERE approved='1' ORDER BY RAND() LIMIT 3";
        $query = mysqli_query($db_conx, $sql);   
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $id = $row["id"];
            $titlu = $row["titlu"];
            $link = $row["link"];
            $poza = $row["poza"];
            $alt = $row["alt"];
            $istorielist .= '<div id="articol-content-more"><a href="/istorie/'.$link.'"><img src="/images/'.$poza.'.jpg"class="articol-content-more-image" alt="'.$alt.'"><p class="articol-content-more-title">'.$titlu.'</p></a><span><a class="articol-content-more-afla" href="/istorie/'.$link.'">Citește mai multe</a></span><span class="articol-content-more-fl"><div class="fb-share-button" data-layout="button_count" data-href="http://esticurios.ro/istorie/'.$link.'"></div></span></div>';
        } 
    }
    

    ?>


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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度