Ex1t Posted July 9, 2015 Share Posted July 9, 2015 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 9, 2015 Share Posted July 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
Ex1t Posted July 9, 2015 Author Share Posted July 9, 2015 (edited) Okay, i fixed that, now this is how site look 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 July 9, 2015 by Ex1t Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 9, 2015 Share Posted July 9, 2015 I'd guess that your <div class="article"> should encapsulate each article, and thus should be inside the loop. Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted July 9, 2015 Share Posted July 9, 2015 (edited) 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 July 9, 2015 by rwhite35 Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 9, 2015 Share Posted July 9, 2015 Since view logic is not separated that doesn't really make sense here, you're just adding extra processing time. Quote Link to comment Share on other sites More sharing options...
Ex1t Posted July 9, 2015 Author Share Posted July 9, 2015 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 Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted July 9, 2015 Share Posted July 9, 2015 (edited) 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 July 9, 2015 by rwhite35 Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 9, 2015 Share Posted July 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted July 9, 2015 Share Posted July 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 9, 2015 Share Posted July 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 10, 2015 Share Posted July 10, 2015 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 Quote Link to comment Share on other sites More sharing options...
Ex1t Posted July 10, 2015 Author Share Posted July 10, 2015 Thanks, all is finished, but just 1 more questionHow 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? Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 10, 2015 Share Posted July 10, 2015 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. Quote Link to comment Share on other sites More sharing options...
Ex1t Posted July 10, 2015 Author Share Posted July 10, 2015 I did it with session, thanks for help guys! Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 10, 2015 Share Posted July 10, 2015 Session is the wrong way to do this. It either won't show anything for someone that doesn't have a valid session going or will show the data of the person viewing as the author, which is obviously wrong. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 10, 2015 Share Posted July 10, 2015 Maybe OP meant used session when creating the post, then was saved to database. Quote Link to comment Share on other sites More sharing options...
Ex1t Posted July 10, 2015 Author Share Posted July 10, 2015 Maybe OP meant used session when creating the post, then was saved to database. Yes, I did it like that Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.