Blog entry display help

I’m building a blog. This is the code to display the entries. I only need the variables to display if they exist. Not all bog posts have an image, video, ect.

[php]<?php

$blogpost = “”;
$sql = mysql_query(“SELECT * FROM Entries ORDER BY date_added DESC”);
$EntriesCount = mysql_num_rows($sql);
if ($EntriesCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row[“id”];
$title = $row[“title”];
$entry = $row[“entry”];
$video = $row[“video”];
$entry2 = $row[“entry2”];
$link = $row[“link”];
$date_added = strftime("%A %B %e, %G %l:%M%p %Z", strtotime($row[“date_added”]));
$blogpost .= ’


’ . $title . ’
        <p id="entry">' . $entry . '</p>
		
		' . $video . '
		
		<br />
		<br />
		<img src="uploads/' . $id . '.jpg" alt="' . $title . '" id="postimage" />
		
		<p id="entry2">' . $entry2 . '</p>
		
		<a href="' . $link . '" target="_blank">' . $link . '</a> 
		
		<br />
		
		<div id="date"><a href="entry.php?id=' . $id . '">Read More</a> | ' . $date_added . '</div>
        
		</div>';
}

}

else {
$blogpost = “No blog posts to display.”;
}

mysql_close();

?>[/php]

First you should be using mysql, but instead you should be using either mysqli or PDO (My Recommendation) that is if you really want to learn PHP. I’ll save you sometime if you already know this, by saying I heard all the excuses and I am just saying this in case you don’t know.

As for you main question, simple have an if statement on the fields that might not have any content (I’m assuming you assigning the blank fields with NULL in the database table?).

for example

[php]if ($row[‘image’]) {
// Show image…
}[/php]

if there is nothing in $row[‘image’] then the if statement will be false and won’t show anything…

OK, I’m going to put money where mouth is and show you how it’s done (I can’t guarantee it will work, but this is how I would go about doing it.

first I would declare my read query as such

[php]/* I personally don’t like using *, but instead declare each table column that is in the database table */
$sql = “SELECT title, entry, video, entry2, link2, date_added FROM Entries ORDER BY date_added DESC”;[/php]

Then I would do something like the following:
[php]try {
$stmt = $pdo->prepare($sql); // Prepare the query using pdo (I’ll show you a PDO connection string later):
$stmt->execute(); // Execute the Query:
} catch (Exception $ex) {
print $ex->getMessage(); // Catch the error and display it:
}[/php]

Then you could do something like the following (I’m only going to write a small portion of it):

[php]/* I use objects for I think it’s easier to see, but you can use associate arrays /
while ($entry = $stmt->fetch(PDO::FETCH_OBJ)) {
echo “<p class=“entry”>” . $entry->entry . “

\n”;
$mydate = new DateTime($entry->date_added);
echo "<p class=“myDate”>Today’s date is " . $myDate->format(‘F j, Y’) . “\n”;
/ More code goes here */
} // End of While Statement:[/php]

and here is a pdo connection string as promised:
[php]$pdo_options = [
/* important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/
throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/
fetch associative arrays (default: mixed arrays) */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo= new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=’ . DATABASE_NAME . ‘;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);[/php]

Hope This Helps ~ John

and to prove that I practice what I preach this is what I’m current working on it’s a cms blog:

[php]<?php
require_once ‘lib/includes/utilities.inc.php’;
$stmt = $blog->read();
require_once ‘lib/includes/header.inc.php’;
?>

<?php while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { ?>

July 20, 2015

<?php echo $row->title; ?>

John R. Pepp


<?php echo $row->content ?>


EditDelete <?php } ?> <?php require_once 'lib/includes/footer.inc.php';[/php]

It’s a work-in-progress and I’m using OOP and namespaces, but it’s basically uses the same coding principles.
What I do is first is the HTML/CSS that way I know how it’s going to look and then I go back to add the php code in the areas that need it. This is how I do my workflow and I’m sure (I know) there are people who do it differently, for there is really no right or wrong way in the development process.

Hi,

Thanks for all your help/suggestions. I’m very comfortable with HTML/CSS, but new to PHP and still learning. The above code I posted is from an old online tutorial I found.

Here’s the site so far.
http://whatsnotontheradio.com/

Nice, is that straight CSS or is there javascript thrown it with it? Either way very nice.

Thanks.

HTML/CSS & PHP

[code]

What's Not On The Radio

Ever wonder where all the “real music” is? Not on the radio, that’s for sure. This blog is dedicated to the unknown and underrated. I’m not here to review or criticize. From original music to covers, if it brings a little joy to my heart it’ll be shared here with you.

<?php echo $blogpost; ?>
[/code]

$blogpost is the first code I posted.

Sponsor our Newsletter | Privacy Policy | Terms of Service