PHP news system with mysql? - php

I am trying to make a news sytem in php.
The DB structure looks like this
"title" "author" "content"
And the PHP script looks like this
<?php
include ('config2.php');
$result = mysql_query("select * from news");
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$author = $row['author'];
$content = $row['content'];
}
echo"<div class=\"post\">
<h2 class=\"title\"><font color=\'#1e1e1e\' href=\"#\">$title</font></h2>
<p class=\"meta\">Today $author</p>
<p>$content</p>
</ul>
</div>"
?>
What I want to do is : Each time I insert a new row in my db, I want the new to be displayed on the page, and right now when I add a new row it just edit the previous new.
Any idea?

You're echoing outside the while loop. Modify it to the following:
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$author = $row['author'];
$content = $row['content'];
echo"<div class=\"post\">
<h2 class=\"title\"><font color=\'#1e1e1e\' href=\"#\">$title</font></h2>
<p class=\"meta\">Today $author</p>
<p>$content</p>
</ul>
</div>";
}

Put the echo statement inside the while loop.

Related

multiple echo statements in one echo?

I am relatively new to PHP and want to know a more efficient way of echo'ing the following in less steps or even just one statement.
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
echo "</p></div>";
As #droopsnoot has commented on above, you should try doing the next thing here.
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>". $row['uid']."<br>" . $row['date']."<br>".$row['message']."<br><br>". "</p></div>";
}
It should work perfectly.
Another option is to close php tag (?>) and use html markup with <?=$var?>:
<?php
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {?>
<div class="card">
<p class="card-text">
<?=$row['uid']?><br>
<?=$row['date']?><br>
<?=$row['message']?><br><br>
</p>
</div>
<?php
} // end while

How do I add nl2br and htmlentities in this code

I'm in need of some help. How can I output the nl2br() and htmlentities() functions into my code? Both need to be applied to 'content' but htmlentities to 'title'.
<?php
$sql = "
SELECT
post.id AS postid, post.title AS title, post.created,
DATE_FORMAT(Created, '%d-%m-%Y') AS created,
post.content AS content, post.image AS image,
post.alttext AS alttext, blog.id, blog.blogname
FROM
post
LEFT JOIN
blog ON post.blogid = blog.id
WHERE
blogid = \"" .(int)$_GET['id'] . "\"
ORDER BY
postid DESC";
$results = $db->query($sql);
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>{$row->title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$row->content}
</p>";
}
} else {
echo 'No Results';
}
?>
You can use a new variable for that:
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
$title = htmlentities($row->title);
$content = nl2br(htmlentities($row->content));
echo "<hr>
<h3>{$title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$content}
</p>
";
}
}
Or break the string:
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>". htmlentities($row->title) ."</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
". nl2br(htmlentities($row->content)) ."
</p>
";
}
}
The neatest way is to abstract the fetch process, so that the fact that data needs demangling, is abstracted away from the view process which draws the result.
Add a function :-
function demangle_row($row) {
$row->content = nl2br($row->content);
$row->content = htmlentities($row->content);
$row->title = htmlentities($row->title);
}
Now change the while statement, to use the above code so
While($row = $results->fetch_object()) {
becomes
While($row = demangle_row($results->fetch_object())) {

Creating the listset of data from array of MySql query result

I want to create the list of products in box-style based on the data loaded from the database. I am using mysqli for querying the result and here is my code
<?php
$db = new mysqli('localhost', 'root', 'root', 'shop');
$result = $db->query('select * from products');
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
?>
With this code, I got a multi-dimensional array that contains the information of all products in the database and I want to use this data to create the list of products like this code
<div class='each-product'>
<div class='img'>
<img src="images/xxx.jpg" />
</div>
<h2>TITLE</h2>
<h3>PRICE</h3>
</div>
I used to use this code for iteration but the foreach() did not return the expected result
<?php
foreach($data as $product) {
echo "<div class='each-product'>";
echo "<div class='img'>";
echo "<img src='images/".$product['image']."' />";
echo "</div>";
echo "<h2>".$product['title']."</h2>";
echo "<h3>".$product['price']."</h3>";
echo "</div>";
}
?>
When I run this code, it returned nothing on the screen. It is currently not solved.
Is there anyway to fix this?

Using echo with lists in php?

I have the following PHP code:
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "<hr>";
}
This is used to create a news feed and I use echo to print out what is within the updates.
Is there a way in which I could maybe use a list to print out each update instead of the way im currently doing it?
Or is it possible to create a div around each update that the while loop creates?
I'm sorry if the question is not clear but thanks for all the help!
My newsfeed creates updates in a news feed like twitter. Each update is printed out using echo and surrounded by . Im trying to find a way in which I can create a list or div for the entire layout of each update. Im finding very difficult to arrange whats going on in each update.
The following code should get you there. Just echo out the html as you would like it to appear.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class='news-article'>";
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "</div>";
echo "<hr>";
}
A lot of the time a lot of echo statements like that just confuse what you are trying to do.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<div class='news-article'>
<div class="title"><?php echo $title ?></div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</div>
<hr>
<?php
}
If you wanted to accomplish the same thing as an unordered list you would do this.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
echo "<ul class='article-list'>";
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<li class='news-article'>
<div class="title">$title</div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</li>
<?php
}
echo "</ul>";

MySQL PHP CMS URL creation(need to edit)

I apologize for the length of this question, and would like to say thanks in advance.
Below is the snippet of code in question. I have a MySQL table that stores information from a custom built PHP MySQL CMS program based off of this tutorial:cms tutorial. As you can see in the List Pages and the Display Admin functions, the URL is built with the base url and the ID as well as the title. The ID is the primary key in the MySQL table that pulls all of the necessary information to be displayed on the page. How would I rewrite the code below so that it displays the title only in the URL and not the ID, but still go off of the ID as the unique identifier for displaying information in the pages?
From functions.php:
'// Display center bottom column
function centerbottomcolumn() {
if ($_GET['id']) {
$pageID = (int) $_GET['id'];
$result = mysql_query("SELECT centerbottomcolumn FROM pages WHERE id='$pageID'");
$row = mysql_fetch_array($result);
echo $row['centerbottomcolumn'];
} else {
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
$pageID = $row['value'];
$result = mysql_query("SELECT centerbottomcolumn FROM pages WHERE title='$pageID'");
$row = mysql_fetch_array($result);
echo $row['centerbottomcolumn'];
}
}
// List the pages
function listPages() {
// List the home page first
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
$homeID = $row['value'];
$result = mysql_query("SELECT title FROM pages WHERE id='$homeID'");
$row = mysql_fetch_array($result);
$homeTitle = $row['title'];
echo "<li><a href='" . BASE_URL . "/index.php'>$homeTitle</a></li>";
// List the rest of the pages
$result = mysql_query("SELECT id, title FROM pages");
while ($row = mysql_fetch_array($result)) {
// Do not list the home page twice
if ($row['title'] != $homeID) {
$pageID = $row['title'];
$pageTitle = $row['title'];
echo "<li><a href='" . BASE_URL . "/?id=$id&title=$pageTitle'>$pageTitle</a></li>";
}
}
}
// Display admin table
function displayAdmin() {
// Find the home page ID
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
$homeID = $row['value'];
// Display a table
$result = mysql_query("SELECT id, title, date FROM pages");
echo '<table width="961">';
echo '<tr height="50">
<th align="left">ID</th>
<th align="left">Title of the Page</th>
<th align="left">Date Created</th>
<th align="left">Actions</th>
</tr>';
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$date = date('M d, Y', $row['date']);
echo "<tr>
<td>$id</td>
<td><a href='". BASE_URL . "/id=$id&title=$title'>$title</a>";
if ($id == $homeID) {
echo ' <strong>(Home Page)</strong>';
}
echo "</td>
<td>$date</td>
<td><a href='edit.php?id=$id'>Edit</a></td><td>
<a href='confirm.php?id=$id'>Delete</a></td><td>
<a href='sethome.php?id=$id'>Set as Home</a>";
}
echo '</table>';
}
// Get array with page IDs
function getArray() {
$result = mysql_query("SELECT id FROM pages");
$IDs = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
$IDs[$i] = $row['id'];
$i++;
}
return $IDs;
}
From index.php:
(above head tag)
require_once 'functions.php';
connect();
$IDs = getArray();
(from body)
<?php if (in_array($_GET['id'], $IDs) || !$_GET): ?>
<?php centerbottomcolumn(); ?>
<?php else: ?>
<!-- Show a not found error -->
<p>Not found</p>
<?php endif; ?>'
You want your pages to show information based on a given ID but you don't want to pass the ID to the pages on the URL query string? You could POST the data to the target page instead of on the query string. You could invent a new ID that is a proxy for the real ID. But in general, you can't have a page do something based on user input (clicking a link with an ID in this case) without the input. If you want to hide the ID because it should be secret or you don't want people to be able to guess them, then you can replace your IDs with some kind of random guid that maps to the real (secret) ID in your database.

Categories