How to limit the number of 'Recent Blogs' created in CakePHP? - php

I'm trying to create a blog, and in every single blog it shows the recent blogs that has been created on the bottom of the page. Is there a way i can limit this number to 4 recent blogs? Because currently all the blogs that has been created show's up on the "Recent Blogs" area when i generate it.
<div class="container" id="newsextra">
<h4>MORE NEWS</h4>
<div class="row">
<?php
if(!empty($error)){
echo $error;
}
if (!empty($blogsinfos)) {
foreach ($blogsinfos as $blogs): ?>
<div class="col-md-3">
<a href="/news-single/<?= h($blogs->id)?>">
<img src="<?= h($blogs->mainimg)?>" class="img-responsive">
<h5><?= h($blogs->title)?></h5>
<h6><?= h($blogs->created)?></h6>
</a>
</div>
<?php
endforeach;
}
?>
</div>
</div>

From the controller you should limit the number of posts generated. Thanks #Rik.esh for the answer.
$this->loadModel('Blogs');
$opts1['conditions'] = array('Blogs.status' => 1);
$opts1['limit'] = 4;
$opts1['order'] = array('Blogs.created' => 'desc');
$blogsinfos = $this->Blogs->find('all',$opts1);
$this->set('blogsinfos', $blogsinfos);
$this->set('_serialize', ['blogsinfos']);
foreach ($blogsinfos as $blogs) {
$proid = $blogs['id'];
}

try this
$opts1['order'] = 'Blogs.created desc';

Related

pagination with bootstrap 4 cards

I'm creating a sort of blog area on my website using bootstrap 4 cards, i fill my cards with data from my database. And i want to put the cards inside of a pagination with a category filter
my category select is working fine, but I'm not sure how I'm supposed to tackle the part for calculating how many rows and columns are supposed to be on the webpage. I know i can make a variable which holds the maximum amount of rows and columns and calculate the offset from that with the ceil function. Then put that into a SQL query with the LIMIT being the offset and maximum items but I am not sure how this works with rows. this is the code i have at the moment. `
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li><li class="page-item"><a class="page-link" href="blog.php?category=<?php if (isset($_GET["category"])){echo $_GET["category"];}?>&pagenumber=<?php // this is also where I'm stuck ?>">1</a>
</li>
<li class="page-item"><a class="page-link" href="blog.php?category=<?php if (isset($_GET["category"])){echo $_GET["category"];}?>&pagenumber=<?php // this is also where I'm stuck ?>">2</a></li>
<li class="page-item"><a class="page-link" href="blog.php?category=<?php if (isset($_GET["category"])){echo $_GET["category"];}?>&pagenumber=<?php // this is also where I'm stuck ?>">3</a></li>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
`
the category works so for example if i select category 2 the url will be
http://localhost/HCDistributie/blog.php?category=2&pagenumber=
the only thing that is needed is to get the pagenumber.
this is my back-end code where i loop through each each item in the database and put that inside the card
function getNews()
{
// get current category
if (isset($_GET["category"]) && is_numeric($_GET["category"])) {
// bind get category to categoryId
$categoryId = $_GET["category"];
// if category is 1
if ($categoryId == 1) {
if (isset($_GET["pagenumber"]) && is_numeric($_GET["pagenumber"])) {
$pageNumber = $_GET["pagenumber"];
try {
$itemLimits = 9;
$rowLimit = 3;
$db = new Connection();
$database = $db->openConnection();
$stm = $database->query("SELECT * FROM blog where category = $categoryId");
$stm->execute();
$rowNews = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($rowNews as $items) {
?>
<div class="card-group">
<div id="classContainer" class="col-md-4">
<div class="card">
<img src=" <?php $items['img'] ?>" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title"><?php echo $items["title"]; ?></h5>
<p class="card-text"><?php echo $items["content"] ?></p>
<p class="card-text"><small
class="text-muted"><?php $items["author"]; ?></small>
</p>
</div>
</div>
</div>
</div>
<?php
}
} catch
(PDOException $e) {
echo sprintf("Something went wrong. $e->errorInfo");
}
}
}
}
}
I sort of have an idea what i should do the problem is I'm not sure how to apply that to my code, the tutorials i have read only deal with variable for max items while i have 2 max columns and max rows.
I am not sure how to calculate both of them separately.
This is the first time I worked with any sort of pagination so i apologize if some things seem weird and or stupid.
This is not weird or stupid, first contact with pagination is always a hassle ;)
With bootstrap, you could get away with just knowing the maximal occurrences of your card records. You could wrap everything while using rows and cols, check out this example:
<div class="container">
<div class="row">
<div class="col-4">
1 of 5
</div>
<div class="col-4">
2 of 5
</div>
<div class="col-4">
3 of 5
</div>
<div class="col-4">
4 of 5
</div>
<div class="col-4">
5 of 5
</div>
</div>
</div>
Fiddle
Thanks to .col-4, you will always get three cards in a row. In your example:
<div class="card-group">
<div class="row">
<?php foreach ($item as $items) { ?>
<div class="col-4">
<div class="card">
// Your card content goes here, use $item
</div>
</div>
<?php } ?>
</div>
</div>
More importantly, it seems you writing something in plain PHP. If you want to go public, you should consider a framework that has already some validation capabilities and function helpers - even build in pagination! I would pick Laravel for that.
EDIT:
Fixed mistake. Thanks #hakiko

How To Display random ads in my web site

Hi I want to display random ads in my product page, my all products call in the while loop to display in home page, i want to add ads from backend in between these products randomly in same design as product display like in freekaamaal
How I display these ads in between products can you give me the idea ??
see this for more info
<div class="products-grids">
<div class="col-md-12">
<?php
$id=$_GET['id'];
$result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage");
while($ro = mysqli_fetch_array($result1))
{
$nam1=substr($ro['pne'],0,60);
$url1=$ro['url'];
$description1=substr($ro['description'],0,200);
$price1=$ro['price'];
$price11=$ro['price1'];
$bid1=$ro['company'];
$image=$ro['image_name'];
$time=$ro['time'];
$tag=$ro['tag'];
?>
<div class="col-md-3">
<div class="hentry post1 id="post-225396">
Display Product in loop
</div>
</div>
<?php } mysqli_close($con);?>
<!-- <div class="col-md-3">
<div class="hentry post1 id="post-225396"><img src="ads.jpg"></div>
</div> -->
</div>
<div class="clearfix"> </div>
Anyways your question is too broad.
Here is step by step solution.
FRONTEND HTML + PHP
// TWO HARD DIV'S + THREE RANDOM ADS , BUT ONLY ONE OF THE AD GONNA TRIGGER.
<?php $random_number = rand(0,2); ?>
<?php ads($random_number,0); ?>
<div>your image or whatever</div>
<?php ads($random_number,1); ?>
<div>your image or whatever</div>
<?php ads($random_number,2); ?>
<?php $random_number = rand(0,2); ?>
<?php ads($random_number,0); ?>
<div>your image or whatever</div>
<?php ads($random_number,1); ?>
<div>your image or whatever</div>
<?php ads($random_number,2); ?>
COMPARISION FUNCTION
// Now create a function to compare if random number generated is equal to ad's placeholder.
function ads($rand_num, $placeholder){
if($rand_num == $placeholder){
echo "<div> YOUR ADS CONTENT </div>";
}
}
Note : It's not meant to run perfectly , it's just a outline on how to achieve that specific goal.
Do something like this (i tried to code it to be easy to understand):
<?php
function show_an_ad()
{
$the_ad = '';
switch (mt_rand(0,2))
{
case 0:
$the_ad = 'ad0.jpg';
break;
case 1:
$the_ad = 'ad1.jpg';
break;
case 2:
$the_ad = 'ad2.jpg';
break;
}
echo '<div class="col-md-3"><div class="hentry post1"><img src="'.$the_ad.'"></div></div>';
}
$ads_every_how_many_products = 3;
<div class="products-grids">
<div class="col-md-12">
<?php
$id=$_GET['id'];
$result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage");
$counter = 1;
while($ro = mysqli_fetch_array($result1))
{
$nam1=substr($ro['pne'],0,60);
$url1=$ro['url'];
$description1=substr($ro['description'],0,200);
$price1=$ro['price'];
$price11=$ro['price1'];
$bid1=$ro['company'];
$image=$ro['image_name'];
$time=$ro['time'];
$tag=$ro['tag'];
?>
<div class="col-md-3">
<div class="hentry post1" id="post-225396">
Display Product in loop
</div>
</div>
<?php
if(!($counter%$ads_every_how_many_products))
{
show_an_ad();
}
$counter++;
}
mysqli_close($con);
?>
</div>
</div>
<div class="clearfix"> </div>
i don't have a php system here so there's a chance there could be a syntax error or something

PHP WordPress - exclude by category description

This is my footer. There is parade of categories with most articles in. I need to exclude here categories with description that starts with "XXX".
So If some categories have description that starts with "XXX", it may don´t show here.
Is it possible please? Im newbie in PHP so I dont know if can I declare category discreption here.
<?php global $teo_options;?>
<footer role="contentinfo">
<?php if(isset($teo_options['enable_popular_companies']) && $teo_options['enable_popular_companies'] == 1) { ?>
<div class="stripe-regular">
<div class="row">
<div class="column">
<h2><?php _e('Name', 'Couponize');?></h2>
</div>
</div>
<div class="row collapse">
<div class="column">
<div class="popular-companies flexslider">
<ul class="rr slides">
<?php
$args['hide_empty'] = 1;
$args['orderby'] = 'count';
$args['order'] = 'desc';
if(isset($teo_options['blog_category']) && $teo_options['blog_category'] != '')
$args['exclude'] = implode(",", $teo_options['blog_category']);
$categories = get_categories($args);
foreach($categories as $category) {
$image = get_option('taxonomy_' . $category->cat_ID);
$image = $image['custom_term_meta'];
?>
<li>
<a href="<?php echo get_category_link( $category->term_id );?>" class="wrapper-5 small">
<img src="<?php echo aq_resize($image, 130, 130, true); ?>" alt="<?php echo $category->name;?> coupons">
</a>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
<?php } ?>
Everything is possible but you're just complicating your problem.
Why would you identify a category by a piece of text in the Description?
Also, searching in the description as it's text could end to be a slow and unnecessary query, if you have a lot of categories.
To solve it, I recommend you take a look at the documentation about Including & Excluding Categories.
What I would do is to make sub-categories and either hide them manually or do a trick between the child and parent categories.

Pagination in PHP blog

I am creating a blog in PHP (trying to keep it close to OOP) and am struggling with getting pagination to work on the page that displays all posts.
The code for the blog.php in question is
<?php
require_once("includes/init.php");
$pagetitle = "Blog";
//include header.
require_once("includes/template/header.php");
// initialise script
$blog = new Blog($db);
$parsedown = new Parsedown();
// load blog posts
$posts = $blog->get_posts();
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home</h1>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?php foreach ($posts as $post) { ?>
<h1><?php echo $post['title']; ?>
</h1>
<p class="lead">by <?php echo $post['author']; ?>
</p>
<hr>
<p><i class="fa fa-clock-o"></i> Posted on <?php echo date("d/m/Y", strtotime($post['date'])); ?> At <?php echo date("H:i", strtotime($post['date'])); ?> in <?php
$categories = $blog->get_categories($post['id']);
$links = array();
foreach ($categories as $category) {
$links[] = "<a href='blog-categories.php?id=".$category['category_slug']. "'>".$category['category_name']."</a>";
}
echo implode(", ", $links); ?></p>
<hr>
<p><?php $content = $parsedown->parse($post['content']); echo substr($content,0,445) ; ?></p>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>">Read More <i class="fa fa-angle-right"></i></a>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>#disqus_thread"><i class="fa fa-angle-right"></i></a>
<hr>
<?php }
?>
</div>
What can I add either to the Blog class or put in a new class to allow blog.php to only show the first 5 results and then give a 'next' link to view the next set of 5? I'm not worried about displaying the total number of pages.
As the class is very large, it can be viewed at http://pastebin.com/qN5ii2Ta.
You are currently using a method get_posts(). You could redefine this method to accept a starting point to begin gathering posts from. With this starting point defined you can LIMIT how many posts are returned in your SQL query.

SELECT COUNT(*) FROM 'accounts' WHERE role = 'engineer' getting the total number of rows using symfony1.4

Hi i have a problem in getting all the data from the table accounts, I want to get the total number of data's from the table accounts using WHERE. Let's say from the table accounts where role = engineer. all the total number of rows from the field role = engineer will get. Im using symfony framework on this
Here's my controller below
<?php
class spFindRoleDetailAction extends sfAction{
function preExecute(){
}
public function execute($request){
$this->setLayout('spLayout');
$this->setTemplate('sp/roleDetail');
$role = $request->getParameter('ro');
$this->roleDetail = AccountTable::getInstance()->getRoleDetail($role);
$this->roleCount = AccountTable::getInstance()->getCountDetail($role);
}
}
Model query
public function getCountDetail($role){
return $this->createQuery('a')
->where('a.role = ?', $role)
->execute();
}
and my module templates view code here
<div id="search-by-role-detail" data-role="page">
<div role="main">
<ul data-role="listview">
<li>
<a href="<?php echo url_for('#find-sp-role-page'); ?>" class="ui-btn ui-btn-icon-left ui-icon-nav-l center">
<h1>エンジニア<span class="header-result-count">(<?php echo $roleCount->count(); ?>)</span></h1>
</a>
</li>
<?php if(isset($roleDetail)): ?>
<?php foreach($roleDetail as $role): ?>
<li>
<a href="<?php echo $role->getSlug(); ?>"class="ui-btn ui-btn-icon-right ui-icon-list-r" data-transition="slide">
<?php if($role->getLogo() == NULL): ?>
<div class="middle v-image">
<img class="image-user" src="/images/sp/sample.png">
</div>
<?php else: ?>
<div class="middle v-image">
<img class="image-user" alt="<?php echo $role->getName(); ?>" src="http://img.creww.me/uploads/account/logos/<?php echo $role->getLogo(); ?>">
</div>
<?php endif; ?>
<div class="middle v-list">
<h3><?php echo $role->getName(); ?></h3>
<p><?php echo $role->getOverview(); ?></p>
</div>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
The one in the H1 tag there is a count in there, How will i able to get the total number of rows in it. Im using there a <?php echo $roleCount->count(); ?> which is wrong
Anyone can help me out on this??
Any help is muchly appreciated
Thanks!
If count() isn't working then you can try something like this.
public function getCountDetail($role)
{
$q = Doctrine_Query::create()
->from('[some table] a')
->where('a.role = ?', $role)
->select('COUNT(a.id)')
->execute(array(),Doctrine_Core::HYDRATE_SINGLE_SCALAR)
return $q;
}
In this case the function will return a single number representing the number of records found.

Categories