PHP Counting With Related Articles - php

my goal is to display related articles below a post (check if available, then loop through the ones that are there). It works fine if I have 3 or 6 setup but if I have only 5, it displays automatically the original post as the sixth related article.
echo "<div class='related-posts'>";
$related_articles = get_field('related_articles', false, false);
$id = $related_articles[0];
if($id) {
echo "<h5 class='related-posts_h'>Ähnliche Artikel, die du lesen musst</h5>";
echo "<ul class='related-posts_list clearfix'>";
for ($i=1; $i <= 6; $i++) {
$id = $related_articles[$i-1];
if ( has_post_thumbnail($id) ) {
echo "<li class='related-posts_item'>";
echo "<figure class='featured-thumbnail thumbnail large'>";
echo "<div class='hider-page'></div>";
echo "<a href='".get_permalink($id)." title='".get_the_title($id)."'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
echo "<span class='zoom-icon'></span>";
echo "</a>";
echo "</figure>";
} else {
"<figure class='thumbnail featured-thumbnail'>";
echo "<div class='hider-page'></div>";
echo "<a href='".get_permalink($id)."' title='".get_the_title($id)."'>";
echo "<img src='".get_template_directory_uri()."/images/empty_thumb.gif' alt='".get_the_title($id)."' />";
echo "</a>";
echo "</figure>";
}
echo "<h6><a href='".get_permalink($id)."'>";
echo get_the_title($id);
echo "</a>";
echo "</h6>";
echo "</li>";
}
echo "</ul>";
echo "</div><!-- .related-posts -->";

Try this.. check after the for loop whether the related Post ID is not equals to the original post ID then only display the related Posts
that is
$id = $related_articles[$i-1];
if(get_the_ID() != $id){
//then do the stuff
}
I Hope you are in the details or single.php page so you will get the ID of original post by get_the_ID()
echo "<div class='related-posts'>";
$related_articles = get_field('related_articles', false, false);
$id = $related_articles[0];
if($id) {
echo "<h5 class='related-posts_h'>Ähnliche Artikel, die du lesen musst</h5>";
echo "<ul class='related-posts_list clearfix'>";
for ($i=1; $i <= 6; $i++) {
$id = $related_articles[$i-1];
if(get_the_ID() != $id){
if ( has_post_thumbnail($id) ) {
echo "<li class='related-posts_item'>";
echo "<figure class='featured-thumbnail thumbnail large'>";
echo "<div class='hider-page'></div>";
echo "<a href='".get_permalink($id)." title='".get_the_title($id)."'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
echo "<span class='zoom-icon'></span>";
echo "</a>";
echo "</figure>";
} else {
"<figure class='thumbnail featured-thumbnail'>";
echo "<div class='hider-page'></div>";
echo "<a href='".get_permalink($id)."' title='".get_the_title($id)."'>";
echo "<img src='".get_template_directory_uri()."/images/empty_thumb.gif' alt='".get_the_title($id)."' />";
echo "</a>";
echo "</figure>";
}
echo "<h6><a href='".get_permalink($id)."'>";
echo get_the_title($id);
echo "</a>";
echo "</h6>";
echo "</li>";
}
}
echo "</ul>";
echo "</div><!-- .related-posts -->";

Related

Why I can't echo div?

I have code for list values from database but when I add them to echo "<div>" it stops working
My code:
$sql = "SELECT name, size, type, username FROM Files";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<div class='filediv'>";
echo "<a href='uploads/$row['name']' download>$row['name']</a>";
echo "<p>Size: $row['size']KB</p>";
echo "<p>Type: $row['type']</p>";
echo "<p>Creator: $row['username']</p>";
echo "</div>";
echo "<hr>";
}
}
you have a syntax error
echo "<div class='filediv'>";
echo "<a href='uploads/".$row['name']."' download>".$row['name']."</a>";
echo "<p>Size:". $row['size']."KB</p>";
echo "<p>Type: ".$row['type']."</p>";
echo "<p>Creator: ".$row['username']."</p>";
echo "</div>";
echo "<hr>";
You can use {} for print php variables in html.
echo "<div class='filediv'>";
echo "<a href='uploads/{$row['name']}' download>{$row['name']}</a>";
echo "<p>Size: {$row['size']}KB</p>";
echo "<p>Type: {$row['type']}</p>";
echo "<p>Creator: {$row['username']}</p>";
echo "</div>";
echo "<hr>";

How to print username for each alphabet letter

I have script where I try to print names following its alphabetical letter:
$alphabets = 'abcdefghijklmnopqrstuvwxyz';
$usersResult = getUsersEmail();
echo "<div class='m0'>";
echo "<div id=woohoo class=hey_im_here>";
echo '<input class="search" placeholder="Enter a name">';
echo "<br><br>";
$c=26; //or count(alphabets)
$count = count($usersResult);
for ($ii=0;$ii<$c;$ii++){
echo "<div id='memberList$ii' style='display:inline-block;vertical-align:top'>";
echo "<fieldset class=fieldname style='display:inline'>";
echo "<legend class=name_legend>" . $alphabets[$ii] . "</legend>";
echo "<ul class='list'>";
while ($row_usrname = mysqli_fetch_array($usersResult)) {
if (strtolower(substr($row_usrname['username'],0,1)) == $alphabets[$ii]) {
echo '<li><label for="'.$row_usrname['username'].'">'
. '<input type="checkbox" id="'.$row_usrname['username'].'" class="empCheckBoxClass" name="employment[]" value="'.$row_usrname['staff_id'].'" />'.$row_usrname['username'].'</li> </label>';
}
}
echo "</ul>";
echo "</fieldset>";
echo "<br>";
echo "</div>";
}
echo "</div>";
echo "</div>";
I am able to print names only for A:
How can I print it for every letter?

Using while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) to display images from database but only 1 image is shown

I try to fetch all image from database but only one image is shown.
this is my code:
<?php
$query = "SELECT id, name, image, price FROM products ORDER BY name";
$stmt = $con-> prepare ( $query );
$stmt->execute();
$num = $stmt->rowCount();
if($num>0){
//some html code
this is my while loop:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
echo "<div class='inner'>";
//creating new table row per record
echo "<ul>";
echo "<li>";
echo "<a class='thumb' href='images/{$image}'>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<span class='hoodnamecarousel'>{$name}</span>";
echo"</a>";
echo "</li>";
echo "</ul>";
echo "</div>";
echo "</div>";
This is second part of displaying image after click on any image:
echo "<div id='thumbs2' style='display:none;'>";
echo "<div class='inner'>";
echo "<ul>";
echo "<li>";
echo "<a class='thumb' href='images/{$image}'></a>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<span class='hoodnamecarousel'>{$name}</span>";
echo "<br />";
echo "<span class='price'>{$price}</span>";
echo "<br />";
echo "<a href='add_to_cart.php?id={$id}&name={$name}' class='button btn btn-primary'>";
echo "افزودن به سبد <span class='glyphicon glyphicon-shopping-cart'></span>";
echo "</a>";
echo "</li>";
echo"</ul>";
echo "</div>";
echo "<div id='closeBtn'>بستن</div>";
}
echo "</div>";
echo "</div>";
}
else {
echo "محصولات در حال بروزرسانی می باشند.";
}
?>
this code only display 1 image and doesn't show other images from database
please help me!

How to get the highest value from a database row?

I have a problem. I have a one page website and if there is posted a message a posts shows up and below the post there needs to be an image, but on the last on the index page I don't want an image to show up. So it's like if the id from the database is the highest that post doesn't get a image below it.
Here is the code:
<?php
include 'functions/image/functions.php';
$countQuery = $db->prepare("SELECT paginaNummer AS max FROM pages ;");
$countRow = $countQuery->fetch();
$maxId = $countRow['max'];
$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$num_cols = 1;
$i = 0;
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
while($row = $query->fetch()) {
if(isset($row)) {
echo "<section data-stellar-background-ratio='0.5'>";
echo "<div class='panelContainer'>";
echo "<div class='panel2'>";
echo "<div class='symbol3'></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "<section class='wrapper'>";
echo "<div class='box'>";
echo "<div class='content'>";
echo $i++ % $num_cols == 0 ? '' : '';
echo "<div id='message'><h2> ", $row['berichtNaam'], "</h2>";
echo $row['paginaContent'], "<br />";
if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
echo " <a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
} elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
} else {
}
echo "</div>";
echo "</div>";
echo "</section>";
echo max($row);
if (count($row['paginaNummer']) == max($row)){
} else {
echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
}
echo "</section>";
}
}
echo "</div></div>";
?>
I won't get any further with this part I hope you can help me with this problem
It's hard to understand what you need. But try this query:
$countQuery = $db->prepare("SELECT paginaNummer AS max
FROM pages ORDER BY paginaNummer DESC LIMIT 1");
Or may be this:
$countQuery = $db->prepare("SELECT paginaNummer AS max
FROM pages ORDER BY id DESC LIMIT 1");
Update: this code changes instead of top:
<?php
include 'functions/image/functions.php';
$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$maxrow = $query->rowCount();
$num_cols = 1;
$i = 0;
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
$n = 0;
while($row = $query->fetch()) {
if(isset($row)) {
$n++;
echo "<section data-stellar-background-ratio='0.5'>";
echo "<div class='panelContainer'>";
echo "<div class='panel2'>";
echo "<div class='symbol3'></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "<section class='wrapper'>";
echo "<div class='box'>";
echo "<div class='content'>";
echo $i++ % $num_cols == 0 ? '' : '';
echo "<div id='message'><h2> ", $row['berichtNaam'], "</h2>";
echo $row['paginaContent'], "<br />";
if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
echo " <a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
} elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
} else {
}
echo "</div>";
echo "</div>";
echo "</section>";
echo max($row);
if ($n == $maxrow){
} else {
echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
}
echo "</section>";
}
}
echo "</div></div>";
?>

Display Thumbnails on Wordpress with get_post_thumbnail

I'm struggling to display a few post thumbnails on a Wordpress page. I'm using advanced custom fields where I select those featured articles. The featured articles have a featured image. I can display the title and link to the articles correctly, but I'm struggling with the featured images.
<?php
if (is_page('reviews')) {
$featured_articles = get_field('featured_articles');
$id = $featured_articles[0];
echo "<div class='row blog-first'><div class='small-12 columns'><div class='row blog-teaser'>";
echo "<div class='small-8 columns'>";
if ( has_post_thumbnail($id) ) {
echo "<a href='".get_permalink($id)."' class='thumb'>";
echo get_the_post_thumbnail( $id, 'teaser-thumbnail' );
echo "</a>";
}
echo "</div>";
echo "<div class='small-4 columns col-text'>";
echo "<div class='d'></div>";
echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
echo get_field('text_excerpt',$id);
echo "</div>";
echo "</div></div></div>";
echo "<div class='row'>";
echo "<div class='small-12 medium-8 columns'>";
echo "<h2 class='h2 hc f2'> Our best reviews</h2>";
for ($i=1; $i < 4; $i++) {
$id = $featured_articles[$i];
echo "<div><div class='row blog-teaser'>";
echo "<div class='small-6 columns'>";
if ( has_post_thumbnail($id) ) {
echo "<a href='".get_permalink($id)."' class='image'>";
echo get_the_post_thumbnail( $id, 'teaser-thumbnail' );
$image_large = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'teaser-thumbnail');
echo "</a>";
}
echo "</div>";
echo "<div class='small-6 columns col-text'>";
echo "<div class='d'></div>";
echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
echo "</div>";
echo "</div></div>";
}
echo "</div>";
Try this:
$featured_articles=get_field('featured_articles');
foreach($featured_articles as $single_feature){
$img_arr=wp_get_attachment_image_src($single_feature,'medium'); //thumbnail,full,medium
if(!empty($img_arr)){
echo '<img src="'.$img_arr[0].'" alt="image">';
}else{
echo '<img src="" alt="NO image">';
}
}

Categories