通过从数据库中读取自动创建新框

时间:2013-10-08 08:53:46

标签: php mysql

我有一个信息框,我从数据库(新闻)中读取信息,但我有另一个数据库(类别)与该框的类别。如果类别是7我想自动制作7盒。

<?php
    mysql_connect("localhost", "root","") or die(mysql_error());
    mysql_select_db("tnews2") or die(mysql_error());
    mysql_query("set names 'utf8'");  

    $rowsPerPage = 2;


    $query1 = "SELECT id,name FROM categories  ORDER BY ID";
    $result1 = mysql_query($query1) or die(mysql_error()."[".$query1."]");

    $query2 = "SELECT id,name,text,img,cat_id FROM news WHERE cat_id=2 ORDER BY ID DESC LIMIT  $rowsPerPage";
    $result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
 ?>

    while($row = mysql_fetch_array($result1)){ ?>
            <?php for($i=0; $i<$row['id']; $i++){ ?>


            <div class="focusBussines" >
            <?php while ($rowB = mysql_fetch_array($result2)){ ?>

        <a class="titleMini" href="categories.php?id=<?= $rowB['id'] ?>&cat_id=<?=$rowB['cat_id']?>">Бизнес</a>
        <?php } ?>

        <table class="table" width="100%">
            <?php while ($rowB2 = mysql_fetch_array($result2)){ 

                if($rowB2['cat_id'] == 2){ ?>

                <tr>
                    <td  align="left" width="150" >
                        <img class="pic" src="<?php echo $rowB2["img"];?>" height="120" width="120">
                    </td>
                <td  align="left" colspan="100%" id="title" ><a id="Zaglavie" href="novina.php?id=<?= $rowB2['id'] ?>&cat_id=<?=$rowB2['cat_id']?>" >  <?php echo $rowB2['name'];  ?></a></td>

                </tr>
                    <tr >
                        <td colspan="100%"><hr/> </td>
                    </tr>
                    <?php } } ?>
            </table>
    </div>
<?php } } ?>

但它不起作用。

1 个答案:

答案 0 :(得分:0)

你的问题源于那些不必要的开启/关闭php语句(强调添加):

$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
>>>>> ?> <<<<<
while($row = mysql_fetch_array($result1)){ >>>>> ?> <<<<<
    >>>>> <?php <<<<< for($i=0; $i<$row['id']; $i++){ >>>>> ?> <<<<<

这与写作

相同
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");

while($row = mysql_fetch_array($result1)) {
    for($i=0; $i<$row['id']; $i++){ ?>

并提高可读性并减少由于缺少打开/关闭标记而可能出现的错误。