I am creating a forum and the code I am putting in this thread is the page that shows all of the forum categories. I have this setup where I have an id, category_section, category_title, and category_description. The way it is structured now is that it creates a new category section border every time I add a new category title in my database. This is what I mean by this:
As an example:
My category section is called Shoes, my category titles are Nike, Adidas, Reebok
Currently, it does:
Shoes
Nike
Shoes
Adidas
Shoes
Reebok
But it should do this:
Shoes
Nike
Adidas
Reebok
How can I get my current code to do this?
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_title DESC");
$numrows = mysqli_num_rows($query);
$categories = "";
if($numrows > 0){
while($forum_row = mysqli_fetch_assoc($query)){
$categoryid = $forum_row['id'];
$category_section = $forum_row['category_section'];
$categoryTitle = $forum_row['category_title'];
$categoryDescription = $forum_row['category_description'];
$categories = "<a href='forum_view_category.php?cid=".$categoryid."'>"
. $categoryTitle . "</a>";
//$categories .= "<a href='forum_view_category.php?cid=".$categoryid
//."' class='cat_links'>" . $categoryTitle . "</a>" " - " . $categoryDescription .;
?>
<div class="category_section">
<?php echo $category_section; ?>
</div>
<div class="category_border">
<div class="discussions_left">
<div class="discussions_category_title">
<?php echo $categories; ?>
</div>
<div class="discussions_category_description">
<?php echo $categoryDescription; ?>
</div>
</div>
<div class="discussions_right">
</div>
</div>
<?php
}
//echo $categories;
} else {
echo "<p>There are no posted categories available yet. </p>";
}
I would change your SQL to order by section first, then the title.
SELECT * FROM forum_categories ORDER BY category_section DESC, category_title DESC
After you've done that, define an if statement to check if the current section is the same as the previous statement.
If so, move one.
If not, echo the new section title.
I've cleaned up your code a bit and switched to the Alternative syntax for control structures for readability and maintainability.
PHPFiddle Demo (faked MySQL)
<?php
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_section DESC, category_title DESC");
$category_section = "";
?>
<?php if(mysqli_num_rows($query) > 0): ?>
<?php while($cat = mysqli_fetch_assoc($query)): ?>
<?php if($cat['category_section'] !== $category_section): ?>
<?php $category_section = $cat['category_section'] ?>
<div class="category_section">
<?= $category_section ?>
</div>
<?php endif; ?>
<div class="category_border">
<div class="discussions_left">
<div class="discussions_category_title">
<a href="forum_view_category.php?cid=<?= $cat['id'] ?>">
<?= $cat['category_title'] ?>
</a>
</div>
<div class="discussions_category_description">
<?= $cat['category_description'] ?>
</div>
</div>
<div class="discussions_right">
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<p>There are no posted categories available yet.</p>
<?php endif; ?>
Related
My while loop not working, I also have a while loop on the same PHP page, but this one doesn't working, I dont understand why. Heres my code:
$posts_query = "SELECT * FROM posts ORDER BY id";
$posts_result = mysqli_query($connection, $posts_query);
<ul class="accordion__wrapper">
<?php while ($post_side = mysqli_fetch_assoc($posts_result)) : ?>
<?php
$category_id = $post_side['category_id'];
$category_query = "SELECT category_name FROM categories WHERE id=$category_id";
$category_result = mysqli_query($connection, $category_query);
$category = mysqli_fetch_assoc($category_result);
?>
<li class="accordion__menu accordion__products-bg">
<button class="accordion__effect accordion__products-btn">
<?= $category['category_name'] ?>
</button>
<div class="accordion__panel accordion__products-list">
<p>
Artikel 1
</p>
<p>
Artikel 2
</p>
<p>
Artikel 3
</p>
</div>
</li>
<?php endwhile ?>
</ul>
Its not showing any error, and the doesn't show either...
Already solved, the problem was happen because I try to the same variable with only one call.. But I think there should be another way that can siplify it
$categories_query = "SELECT * FROM categories ORDER BY id";
$categories_result = mysqli_query($connection, $categories_query);
$side_categories_query = "SELECT * FROM categories ORDER BY id";
$side_categories_result = mysqli_query($connection, $side_categories_query);
<?php
$no = 1;
while ($category = mysqli_fetch_assoc($categories_result)) : ?>
<button class="product__top-btn products-btn-<?= $no; ?>">
<?= $category['category_name'] ?>
</button>
<?php $no++;
endwhile ?>
<?php $no = 1;
while ($side_category = mysqli_fetch_assoc($side_categories_result))
: ?>
<div class="accordion__menu accordion__products-bg">
<button class="accordion__effect accordion__products-btn">
<?= $side_category['category_name'] ?>
</button>
</div>
<?php $no++;
endwhile ?>
I am trying to get my code to return the last 3 posts from wordpress in Magento using the Fishpig extension. This is the code I have so far, but it appears to returning posts more than once. I also need it to just return posts, as it also returns pages at the moment.
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID ORDER BY p.post_date";
$results = $readConnection->fetchAll($query);
?>
<?php
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='/news/wp-content/uploads/'.($results1[0]['meta_value']);
?>
<div class="blog-post-image">
<img src="<?php echo $url; ?>">
</div>
<div class="blog-post-content">
<?php ?>
<h3> <?php echo $row['post_title'];?></h3>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
More Info
</div>
<?php endif; ?>
<?php
if($counter == 4)
{
break;
}
$counter++;
}
?>
To display 3 posts and display the post title, URL, image and post content (or excerpt), you would need the following code:
<?php $posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addPostTypeFilter('post')
->setPageSize(3)
->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
<?php echo $post->getPostContent() ?>
<?php
/**
* You could also use:
* echo $post->getPostExcerpt(20)
* This would display the first 20 words of the post content
* or the manually entered excerpt
**/
?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This has been writte quickly by hand and hasn't been tested but should work fine.
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
Im trying to get two of the latest posts from Wordpress that include featured images. Need to get the post title, content (character limited) and the featured image. I have this so far, all that is missing is the featured image.
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT `id`, `post_title`,`post_name` ,`post_content`, `comment_count` FROM `wp_posts` WHERE `post_type`='post' ORDER BY `comment_count` DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'): ?>
<li class="item">
<?php echo $row['post_title'];?>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
Try this:
I have updated your query and added another query to get post image url :
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID WHERE pm.meta_key = '_thumbnail_id' AND p.post_type='post' ORDER BY p.post_date DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='www.yoursite.com/wp-content/uploads/'.($results1[0]['meta_value']);
echo $url; //YOUR URL just set your site name
//So final url YOUR--> SITENAME/wp-content/uploads/pathfromquery
?>
<li class="item">
<?php echo $row['post_title'];?>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
You shouldn't really be using raw SQL here. As a work around, couldn't you do either of the following:
1) Select more than 2 posts (eg. 5) and loop through them and display the 2 that have featured images
2) Ensure all posts have featured images and then select the first 2 posts
3) Use a collection of posts and then add your custom SQL to that.
This code will get 2 posts and add the _thumbnail_id to the collection. You can add some code to this to check this field exists (use $posts->load(true) to debug the SQL query and $posts->getSelect()->where() to add the custom filter)
<?php $posts = Mage::getResourceModel('wordpress/post_collection') ?>
<?php $posts->addPostTypeFilter('post') ?>
<?php $posts->addIsViewableFilter() ?>
<?php // Limit the collection to 2 posts. Change this number or remove this line completely to include all posts
<?php $posts->setPageSize(2) ?>
<?php // Adds the _thumbnail_id meta field to the collection ?>
<?php // This can be used for checking in the SQL whether a post has a featured image ?>
<?php $posts->addMetaFieldToSelect('_thumbnail_id') ?>
<?php $posts->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2><?php echo $post->getPostTitle() ?></h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
<div class="post-content"><?php echo $post->getPostContent() ?></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I have 2 SQLite- tables, one containing articles and one containing image URL's.
What I'd like to do is to do a foreach() to show the articles in order and show a set number of random images next to the article.
The images should be selected by checking similarity between the Article's and image's categories (column in the table) and then randomized. (The article catergory could be for example 'motorboats' and the img category 'boat').
How do I show the results from two different arrays?
The code for the articles is:
$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="artikelLista">
<?php foreach($res as $article): ?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?>
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
To add the second array, I tried this, which didn't work, it only showed the results from the first array multiple times:
$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $db->prepare('SELECT * FROM Object;');
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="artikelLista">
<?php foreach($res as $article): ?>
<?php foreach($res2 as $object): ?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<?php echo $object['img'];?> <!-- For testing. The images should be filtered and a a few random images would be shown here -->
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
I guess it's not possible to use nested foreach() like this.
How will I manage this?
Also, If anyone knows on top of their head how to match the similarities between the arrays as described above, I'd be greatful. If not, I'll deal with this later.
You can fall a function inside the first foreach a pass category of the article to that function. Now in that functino collect all images in an array related
to the passed category and then return it by randomizing it. Code below ..
<?php foreach($res as $article): ?>
<?php
$img = get_img($article['category']);
?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<img src="<?php echo $img; ?>" /> <!-- The return result will be shown here -->
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
<?php
function get_img($category){
$stmt2 = $db->prepare('SELECT * FROM Object WHERE category = '.$category);
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$rnd = array();
foreach($res2 as $object){
$rnd[] = $object['img'];
}
$n = rand(0,(count($rnd)-1));
return $rnd[$n];
}
?>