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>';
        } 
    }
    

    ?>


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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行