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

时间:2016-05-03 11:17:55

标签: php mysql

我想制作一个新闻栏节目我需要显示最后5个查询, 我在同一个数据库mySql中有5个类别(表格),如历史,自然,生活方式,科学和旅行 所有表都具有相同的结构 ID,标题,descriere,数据,图片,alt,已批准,我想通过数据查看最后5个订单,我怎么能这样做?以及显示查询的PHP代码

我的代码显示了同一个表中数据的最后5个查询顺序是这个......

$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 个答案:

答案 0 :(得分:0)

您可以将类别数组静态或将所有类别放入一个表中,并动态生成类别数组

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

&GT?;