PHP - 新闻杂志在线

时间:2017-04-16 09:33:01

标签: php html sql

这是我的网站:
Screenshot

正如你所看到的,我不能简单地使用一个PHP数组来互相编写每个新闻。

目前我使用这种方法。

  

HTML之前

<?php
include 'config/db.php';

// Get Article 1
$g1_query = "SELECT * FROM articles where id = 1";
$g1_result = pg_query($con, $g1_query) or die("Cannot execute query: $g1_query\n");

// Get Article 2
$g2_query = "SELECT * FROM articles where id = 2";
$g2_result = pg_query($con, $g2_query) or die("Cannot execute query: $g2_query\n");

// Get Article 3
$g3_query = "SELECT * FROM articles where id = 3";
$g3_result = pg_query($con, $g3_query) or die("Cannot execute query: $g3_query\n");

// Get Article 4
$g4_query = "SELECT * FROM articles where id = 4";
$g4_result = pg_query($con, $g4_query) or die("Cannot execute query: $g4_query\n");

pg_close($con);
?>
  

HTML后

<div class="medium-8 columns">
<?php $row = pg_fetch_assoc($g1_result); ?>
    <div class="blog-post">
        <h3><?php echo $row['title']; ?></h3>
        <p><?php echo $row['body']; ?></p>
    </div>
</div>

<div class="medium-8 columns">
    <?php $row = pg_fetch_assoc($g2_result); ?>
        <div class="blog-post">
            <h3><?php echo $row['title']; ?></h3>
            <p><?php echo $row['body']; ?></p>
        </div>
    </div>

它可以工作,但正如你所看到的,它有很多不必要的代码。 我想用另一种方法代替。

编辑: 一位朋友告诉我这个方法,但我不确定如何使用它:

<?php 

    $a_query = "SELECT * FROM articles ORDER BY id";
    $a_result = pg_query($con, $a_query) or die("Cannot execute query: $a_query\n");

    while($row = pg_fetch_assoc($a1_result)) { 

        if($row['size']==1) { // If its a big value (Value 1) the news articles gets the div class blog-post
        echo '<div class="blog-post">
                <h3>'.$row['title'].'</h3>
                <p>'.$row['body'].'</p>
            </div>
        ';
        }
        elseif($row['size']==2) { // If its a small value (Value 2) the news articles gets the div class blog-post-smaller
        echo '<div class="blog-post-smaller">
                <h3>'.$row['title'].'</h3>
                <p>'.$row['body'].'</p>
            </div>
        ';
        }
    }
?>

2 个答案:

答案 0 :(得分:1)

使用IN获取文章。

SELECT * FROM `articles` where `id` IN (1,2,3,4)

在数组中,您将收到所有文章。现在将它打印在for循环中

答案 1 :(得分:1)

你朋友给你的代码基本上解决了你的问题:     

    $a_query = "SELECT * FROM articles ORDER BY id";

这是一个查询按ID排序的所有文章的查询,因此您不必为每篇文章手动编写查询。

    $a_result = pg_query($con, $a_query) or die("Cannot execute query: $a_query\n");

    while($row = pg_fetch_assoc($a1_result)) { 

这个while循环遍历所有文章。您将在变量$row中的每篇文章都在大括号内。

        if($row['size']==1) { // If its a big value (Value 1) the news articles gets the div class blog-post
            echo '<div class="blog-post">
                    <h3>'.$row['title'].'</h3>
                    <p>'.$row['body'].'</p>
                </div>
            ';
        }
        elseif($row['size']==2) { // If its a small value (Value 2) the news articles gets the div class blog-post-smaller
            echo '<div class="blog-post-smaller">
                    <h3>'.$row['title'].'</h3>
                    <p>'.$row['body'].'</p>
                </div>
            ';
        }
    }
?>

我建议你尝试一下代码,看看它是否适合你。