Jump to content

Blog in PHP5


Ex1t

Recommended Posts

I have problem with showing posts from database and pagination

 

I have this tables in database

 

admin (for admin login)

articles (for articles)

In articles i have 

 

id, title, srtext (short text), lgtext (longtext) and time

 

Now i want to display that in index.php page, but i have problems.. 
I tried to download some simple php blogs, but there is no blogs in PHP5, all are the older versions

 

I tried something like this

 

<?php
 
error_reporting(0);
 
include_once ('admin/includes/config.php');
 
$sql="SELECT * FROM  articles ORDER BY id ASC";
 
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
 
echo strtoupper($row['title']);
 
}
 
echo '
 
<center>
<div class="article">
<h1><?php echo strtoupper($row["title"]); ?></h1> 
<p><small>Published: 7/9/2015</small></p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="more">
<button>Read more</button>
</div>
</div>
</center>
 
 
';
 
 
?>

But dont work..

I forgot to say, i want that when someone press button "read more" to move him to page where is title, time and long text (lgtext in db), can someone help me with this

Link to comment
Share on other sites

You're selecting many articles from the database, but only displaying one. You need to move all of your HTML inside the while() loop so that it displays each article.

 

Also, your "read more" should be a <a></a> tag with a link to the specific post. Usually you'd use a query string or URI segments to link to a single article. For example:

<a href="articles?id=<?php echo $row['id']; ?>">Read more</a>
Then you'd fetch that specific article from the database with a WHERE clause.
Link to comment
Share on other sites

Okay, i fixed that, now this is how site look

 

SWlk4zl.png
 

This is the code
 

<div class="article">
<?php
 
$blog = mysqli_query($con,"SELECT * FROM articles ORDER BY id ASC");
   while($row = mysqli_fetch_array($blog)) {
    $id = $row['id'];
       $title = $row['title'];
       $srtext = $row['srtext'];
       $time = $row['time'];
 
?>
<h1><?php echo $title; ?></h1> 
<p><small>Published: <?php echo date('m/d/Y',$time);  ?> | Author: Admin</small></p>
<p><?php echo nl2br($srtext); ?></p>
<div class="more">
<a href="articles.php?id=<?php echo $id; ?>"><button>Read more</button></a>
</div>
</div>
<?php        
    }
   ?>

How to style other articles? 
Edited by Ex1t
Link to comment
Share on other sites

It might help if you separate the two operations (concerns) and think about them as database and output.  Try your code this way:

<?php //head of script
$blog = mysqli_query($con,"SELECT * FROM articles ORDER BY id ASC");
   while( $row = mysqli_fetch_array($blog)) { $records[] = $row; }
   // $records prototype Array([0]=>Array(['id], ['title']...),[1]=>Array(['id'], ['title']..))
?>

<body>
<div>
<?php 
   foreach($records as $array) {
     echo "<h1>".$array['title']."</h1>";
     // continue the rest of your output
     echo '<a href="articles.php?id='.$array["id"].'"><button>Read More</button></a>';
   } // close foreach loop
?>
</div>
</body>

Additionally, when coded this way, you can test $records to make sure your query is returning what you expect:

var_dump($records); // or use
print_r($records);
Edited by rwhite35
Link to comment
Share on other sites

I'd guess that your <div class="article"> should encapsulate each article, and thus should be inside the loop.

 

You're right, thanks

 

@rwhite35 its works, thanks, but now how to make post limit, i think for pagination

Link to comment
Share on other sites

if you want to continue with the same logic as originally proposed, you can use the LIMIT $start, $end when $start is incremented by the number of post per page.  If 5 post per page, $start would be 0, 5, 10, 15, 20 etc... and $end 4, 9, 14, 19 etc.

SELECT * FROM articles ORDER BY id ASC LIMIT $start, $end;

The issue with this though is performance.  You would re-query the database with every iteration.  I go back to my original post and work out the pagination using a multi-dim array.  Its less system resources.  

Edited by rwhite35
Link to comment
Share on other sites

The issue with this though is performance.  You would re-query the database with every iteration.  I go back to my original post and work out the pagination using a multi-dim array.  Its less system resources.

Not sure what you mean. The LIMIT you provided can be used in his original query without needing to be in a loop. The start and end would be calculated based on the current page number.

Link to comment
Share on other sites

Not sure what you mean. The LIMIT you provided can be used in his original query without needing to be in a loop. The start and end would be calculated based on the current page number.

 

Perhaps I should have typed "with each new page load" instead of iteration.  But I get where you're heading, page 1 * 5 (5); page 2 * 5 (10); page 3 * 5 (15) etc. But your still making multiple trips to the data store with each page load, and that was really my point.  An expensive proposition if you have lots of records and lots of traffic.

 

For the sake of argument, if the first page instanced a multi-dim array of ALL records in one pull, the $records array could be passed along to the next page using a session. 

 

If this were a website with a lot of traffic (hundreds of request per hour) then the second option ($records-session) is likely the better.  If only a couple request a day, I suppose either is fine.

Link to comment
Share on other sites

An expensive proposition if you have lots of records and lots of traffic.

Your way is worse, because now you're tying up a ton of memory. You're basically pulling down an unlimited amount of data, which some people might not ever even look at. If 90% of people only look at the first page with like 10-20 records, but you're pulling down thousands, you're just wasting resources.

 

Whereas, one extra query per page load is absolutely not a problem. Since it's just a blog with probably infrequent changes, you could make heavy use of caching if the database requests start becoming a problem.

Link to comment
Share on other sites

 

if you want to continue with the same logic as originally proposed, you can use the LIMIT $start, $end when $start is incremented by the number of post per page.  If 5 post per page, $start would be 0, 5, 10, 15, 20 etc... and $end 4, 9, 14, 19 etc.

SELECT * FROM articles ORDER BY id ASC LIMIT $start, $end;

LIMIT is $startrow_id,$amount_per_page

 

So if you wanted to start at id zero and 5 results would be

LIMIT 0,5

 

then next page would be

LIMIT 5,5

Link to comment
Share on other sites

Thanks, all is finished, but just 1 more question

How to make author of the article?

If u know what I mean..

There is in my design, Published: 07/10/2015 | By: SomeAuthor

 

How to make that SomeAuthor? Can I do it with session?

Link to comment
Share on other sites

Simplest would be to store that data in the db when you publish the article.  Then just display it with the other data you already are doing.  Just make sure to set the publish_date column in the db to either datetime or timestamp.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.