xxxx xxxx - 2 years ago 40
HTML Question

PHP - News Magazine Online

This is my website:

Screenshot

As you can see I can't simply use a PHP Array that writes each news after each other.

Currently I use this method.


BEFORE 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);
?>



AFTER 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>


It works but as you can see, it has a lot of unnecessary code.
I would like too use another method instead.

Edit:
A friend told me about this method, but I'm not sure how to use it:

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

Answer Source

The code your friend gave you basically solves your problem:

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

This is a query that reads ALL your articles ordered by id, so you don't have to write a query manually for every article.

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

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

This while loop iterates over all articles. You will have every article in the variable $row inside the curly brackets.

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

I would advise you to try the code out and see if it works for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download