How can this be packed into a foreach loop - php

The query and the fetching are working properly, but it's too repetitive, I tried (applying some tutorials on for loops) but can't seem to figure out.
<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulos[0]['id']; ?>&gen=<?= $articulos[0]['genero']; ?>&id=<?= $articulos[0]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[0]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[0]['id']; ?>&gen=<?= $articulos[0]['genero']; ?>&id=<?= $articulos[0]['id'] ?>">
<?= $articulos[0]['titulo']; ?>
</a>
</a>
</div>
</div>
<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[1]['id']; ?>&gen=<?= $articulos[1]['genero']; ?>&id=<?= $articulos[1]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[1]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[1]['id']; ?>&gen=<?= $articulos[1]['genero']; ?>&id=<?= $articulos[1]['id'] ?>">
<?= $articulos[1]['titulo']; ?>
</a>
</a>
</div>
</div>
<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[2]['id']; ?>&gen=<?= $articulos[2]['genero']; ?>&id=<?= $articulos[2]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[2]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[2]['id']; ?>&gen=<?= $articulos[2]['genero']; ?>&id=<?= $articulos[2]['id'] ?>">
<?= $articulos[2]['titulo']; ?>
</a>
</a>
</div>
</div>
<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[3]['id']; ?>&gen=<?= $articulos[3]['genero']; ?>&id=<?= $articulos[3]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[3]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[3]['id']; ?>&gen=<?= $articulos[3]['genero']; ?>&id=<?= $articulos[3]['id'] ?>">
<?= $articulos[3]['titulo']; ?>
</a>
</a>
</div>
</div>
It's working OK but I want to make it lightweight.
Thank You for any suggestions.

It's kind of easy really:
<?php foreach ($articulos as $articulo) { ?>
<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulo['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<?= $articulo['titulo']; ?>
</a>
</a>
</div>
</div>
<?php } ?>

Thanks to Farkie the rest was easy, To limit the amount of results this is how the whole thing ended up.
<?php
$i="1";
foreach ($articulos as $articulo) { ?>
<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulo['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<?= $articulo['titulo']; ?>
</a>
</a>
</div>
</div>
<?php
if ($i++ == 4) break;
} ?>
Thank You so much.

Related

PDF files are not shown in website but working fine in localhost

I want to read my "pdf files" in PHPMYSQL tables. So I found the script from some forums and it's work fine in "localhost using XAMPP".
But When I deploy to website, it's not showing pdf files and all information after that line are missing.
I dont know what is the problem. Kindly someone can help me please...
<div class="page-title">
<div class="container">
<div class="row">
<div class="col-md-12">
<ol class="breadcrumb">
<li>Home</li>
<li>Journals</li>
<li><?php echo $title; ?></a></li>
</ol>
</div>
</div>
</div>
</div>
<section class="block-wrapper">
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-12">
<div class="single-post">
<div class="post-title-area">
<a class="post-cat" href="<?php echo $post_cat_link; ?>"><?php echo $category; ?></a>
<h2 class="post-title">
<?php echo $title; ?>
</h2>
<div class="post-meta">
<span class="post-date"><i class="feather icon-clock"></i> <?php echo $post_date; ?></span>
<span class="post-comment"><i class="feather icon-eye"></i>
<span><?php echo $post_views; ?></span></span>
</div>
<div class="div1">
<?php echo "View PDF File for Journal Details" ?>
<?php
mysql_connect('localhost','adminjournal','adminjournalikat');
mysql_select_db('journal');
// mysql_connect('localhost','root','');
// mysql_select_db('journal');
$sql="SELECT pdf from tbl_blog_posts";
$query=mysqli_query($conn,$sql);
while ($info=mysqli_fetch_array($query)) {
?>
<embed type="application/pdf" src="pdf/<?php echo $info['pdf'] ; ?>" width="900" height="500">
<?php
}
?>
</div>
</div>
<div class="post-content-area">
<div class="entry-content">
<?php
if ($yt_vid == "") {
?>
<div class="post-media post-featured-image">
<img src="images/blog/<?php echo $media; ?>" class="img-fluid single_blog" alt="">
</div>
<?php
}else{
?>
<div class="post-media post-video">
<div class="embed-responsive">
<iframe src="https://www.youtube.com/embed/<?php echo $yt_vid; ?>"></iframe>
</div>
</div>
<?php
}
?>
<?php echo $cont?>
<br></br>
</div>
<div class="tags-area clearfix">
<div class="post-tags">
<span>Tags:</span>
<?php
$post_tags = explode(",",$tags);
foreach ($post_tags as $tag) {
if (WBCleanURL == "true") {
$st1 = preg_replace("/[^a-zA-Z]/", " ", $tag);
$st2 = preg_replace('/\s+/', ' ', $st1);
$tag_title = strtolower(str_replace(' ', '-', $st2));
$tag_link = "tags/$tag_title";
}else{
$tag_link = "pages/tags?key=$tag_title";
}
?><?php echo $tag; ?><?php
}
?>
</div>
</div>
<div class="share-items clearfix">
<ul class="post-social-icons unstyled">
<li class="facebook">
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $sharelink; ?>">
<i class="fa fa-facebook"></i> <span class="ts-social-title">Facebook</span></a>
</li>
<li class="twitter">
<a href="https://twitter.com/intent/tweet?url=<?php echo $sharelink; ?>">
<i class="fa fa-twitter"></i> <span class="ts-social-title">Twitter</span></a>
</li>
<li class="gplus">
<a href="mailto:info#example.com?&subject=&cc=&bcc=&body=<?php echo $sharelink; ?>">
<i class="fa fa-envelope"></i> <span class="ts-social-title">Email</span></a>
</li>
</ul>
</div>
</div>
</div>
thanks for your clue that this issue because of overlapping. Finally I found out the script problem... below is the right one...
Thanks a lot and Wish You All The Best
<?php echo $cont?>
<br></br>
<?php echo "PDF File Details for Journal" ?>
<br><h4><?php echo $title;?> </h4>

How to redirect post

I pull a post from a website then i display it on my website. I want when a user clicks on it i want her to be redirected to the source url but when i share it i want the url to have my url like www.mywebsite.com/post url. Am trying this code below
<div class="post-item mb-3 shadow-sm bg-white">
<div class="d-flex justify-content-between flex-wrap p-1">
<div class="post-feat-image pr-2 d-flex align-items-center"><a href="<?= e_attr(post_url($t['loop'])); ?>" class="post-img-link" <?= post_attrs($t['loop']); ?>>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFElEQVQYV2N8+vTpfwYGBgZGGAMAUNMHXwvOkQUAAAAASUVORK5CYII=" data-src="<?= e_attr(feat_img_url($t['loop.post_featured_image'])); ?>" alt="<?= e_attr($t['loop.post_title']); ?>" class="post-feat-img img-zoom"></a>
</div>
<div class="post-info px-2 py-1"><a href="<?= e_attr(post_url($t['loop'])); ?>" <?= post_attrs($t['loop']); ?>>
<h3 class="post-title" title="<?= e_attr($t['loop.post_title']); ?>">
<?= e(limit_string($t['loop.post_title'], 70), false); ?>
</h3></a>
<div class="post-feed-logo mb-1">
<img src="<?= feed_logo_url($t['loop.feed_logo_url']); ?>" class="feed-logo-img">
</div>
<div class="post-time text-muted py-1">
<?= svg_icon('time', 'text-success'); ?>
<?= time_ago($t['loop.post_pubdate'], _T); ?>
<?= svg_icon('eye-outline', 'text-primary'); ?>
<?= localize_numbers($t['loop.post_hits'], _T); ?>
<a href="https://facebook.com/sharer/sharer.php?u=<?= e_attr(post_url($t['loop'])); ?>" class="btn btn-facebook rounded-0 btn-share" data-toggle="tooltip" title="<?= e_attr(__('Share on Facebook', _T)); ?>" target="_blank" rel="noopener">
<?= svg_icon('facebook'); ?>
</a>
<a href="http://twitter.com/share?text=<?= e_attr($t['post.post_title']); ?>&url=<?= e_attr(post_url($t['loop'])); ?>" class="btn btn-twitter rounded-0 btn-share" data-toggle="tooltip" title="<?= e_attr(__('Share on Twitter', _T)); ?>" target="_blank" rel="noopener">
<?= svg_icon('twitter'); ?>
</a>
</div>
</div>
</div>
</div>
just enclose the whole code in <a></a> tag and in CSS just remove all the text decoration.

solving repetitive html in word press template

I would like some help In creating a loop for my WP template.
I have the same div block repeated a few times, the class numbers are different and that's about it.
based of the sample blocks bellow can someone give me a example of how i could wrap this in a loop and change the class numbers ?
code block
If you see bellow some of the class names just differ with the number
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_1') ?>"
alt="<?php the_field('section_3_image_1_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_1'); ?>"
title="<?php the_field('title_1'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_2') ?>"
alt="<?php the_field('section_3_image_2_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_two', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_2'); ?>"
title="<?php the_field('title_2'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_3') ?>"
alt="<?php the_field('section_3_image_3_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_3', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_3'); ?>"
title="<?php the_field('title_3'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<?pho $i=1; if( have_rows('parent_field') ):
while ( have_rows('parent_field') ) : the_row(); ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_' . $i) ?>"
alt="<?php echo get_field('section_3_image_' . $i . '_alt') ?>">
<h3 style="min-height: 150px"><?php echo get_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php echo get_field('link_' . $i); ?>"
title="<?php echo get_field('title_' . $i); ?>"
class="btn btn-lg btn-secondary">
<?php echo get_field('link_text'); ?>
</a>
</p>
</div>
<?php $i++; endwhile; endif; ?>
if you are working with while loop this will work fine.
This ACF field data and your while will work fine as this.
You can do with this code:
<?pho for($i = 0; $i < YOUR_MAX_VALUE; $i++): ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_' . ($i+1)) ?>"
alt="<?php the_field('section_3_image_' . ($i+1) . '_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_' . ($i+1)); ?>"
title="<?php the_field('title_' . ($i+1)); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<?php endfor; ?>
Remember to set YOUR_MAX_VALUE to the value you need.
if you are working with for loop this will work fine.
<?pho for($i = 0; $i < YOUR_VALUE.length; $i++): ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php
the_field('section_3_image_' . ($i+1)) ?>"
alt="<?php the_field('section_3_image_' . ($i+1) . '_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_' . ($i+1)); ?>"
title="<?php the_field('title_' . ($i+1)); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link'); ?>
</a>
</p>
</div>

Hi, i want to limit flexi slider images in loop, by fetching from database, my project is in codeignitor

<div class="content-bottom-in">
<ul id="flexiselDemo1">
<?php $tg = $this->db->get_where("mobile_accessories",array("acs_category"=>"Tempered_glass","product_status"=>1))->result();
foreach($tg as $g){
?>
<li>
<div class="w3l-specilamk">
<div class="speioffer-agile">
<a href="#">
<img src="<?php echo ($g->accessories_image!='')?base_url().$g->accessories_image:base_url()."img/doc/no_image.jpg";?>" width="160px;" height="160px;" alt="">
</a>
</div>
<div class="product-name-w3l">
<h4 style="text-align:center">
<?php echo $g->accessories_name ?>
</h4>
<div class="w3l-pricehkj" style="padding-left:50px">
<span class="item_price"><i class="fa fa-inr" aria-hidden="true"></i> <?php echo $g->accessories_discount_price ?></span><br>
<del><i class="fa fa-inr" aria-hidden="true"></i><?php echo $g->accessories_actual_price ?></del>
</div>
<div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out">
<form action="#" method="post">
<fieldset>
<input name="submit" value="Add to cart" class="button"/>
</fieldset>
</form>
</div>
</div>
</div>
</li>
<?php } ?>
</ul>
</div>

Dynamic carousel banner based on paged viewed

I trying to produce a dynamic carousel banner to update the image, headers, text, links & alts based on the content page being viewed.
The site is being coded in .php & using the bootstrap CSS framework. I've broken the site into three main parts... Header, "Content" and Footer.
I'm calling the function img-banner as part of the header.php but wish to use variables stored in the "content" pages which will determine the images etc being viewed.
Calling the function:
<div class="img-banner">
<?php include ("functions/img-banner.php"); ?>
<!-- closing img-banner -->
</div>
The function:
<?php
function img_banner()
{
?>
<div class="carousel-inner">
<div class="item active"> <img src="<?php echo $img1;?>" alt="<?php echo $alt1;?>">
<div class="container">
<div class="carousel-caption">
<h1><?php echo $head1;?></h1>
<p><?php echo $text1;?></p>
<p><a class="btn btn-lg btn-primary" href="<?php echo $link1;?>" role="button">Learn More<i class="fa fa-chevron-details"></i></a></p>
</div>
</div>
</div>
<div class="item"> <img src="<?php echo $img2;?>" alt="<?php echo $alt2;?>">
<div class="container">
<div class="carousel-caption">
<h1><?php echo $head2;?></h1>
<p><?php echo $text2;?></p>
<p><a class="btn btn-lg btn-primary" href="<?php echo $link2;?>" role="button">Learn More<i class="fa fa-chevron-right"></i></a></p>
</div>
</div>
</div>
<div class="item"> <img src="<?php echo $img3;?>" alt="<?php echo $alt3;?>">
<div class="container">
<div class="carousel-caption">
<h1><?php echo $head3;?></h1>
<p><?php echo $text3;?></p>
<p><a class="btn btn-lg btn-primary" href="<?php echo $link3;?>" role="button">Learn More<i class="fa fa-chevron-right"></i></a></p>
</div>
</div>
</div>
</div>
<?php
}
?>
I'm a little stumped and have tried (obviously unsuccessfully lol) global and SESSION variables but cant get them to work correctly.
I'd appreciate confirmation on the best method to implement the above so I can read up and figure this out. My main goal is to have a cms style admin panel to update the images.
Thanks!
First of all, the function does not have access to the $img1-$img3 variables as you're not passing them to the function.
Secondly, you really do not require a function to do this simple task.
Last but not least, you are not even calling the function, only including it's file.
Here's how you should do this:
<div class="img-banner">
<?php include ("img-banner.php"); ?>
<!-- closing img-banner -->
</div>
And the img-banner.php:
<div class="carousel-inner">
<div class="item active"> <img src="<?php echo $img1;?>" alt="<?php echo $alt1;?>">
<div class="container">
<div class="carousel-caption">
<h1><?php echo $head1;?></h1>
<p><?php echo $text1;?></p>
<p><a class="btn btn-lg btn-primary" href="<?php echo $link1;?>" role="button">Learn More<i class="fa fa-chevron-details"></i></a></p>
</div>
</div>
</div>
<div class="item"> <img src="<?php echo $img2;?>" alt="<?php echo $alt2;?>">
<div class="container">
<div class="carousel-caption">
<h1><?php echo $head2;?></h1>
<p><?php echo $text2;?></p>
<p><a class="btn btn-lg btn-primary" href="<?php echo $link2;?>" role="button">Learn More<i class="fa fa-chevron-right"></i></a></p>
</div>
</div>
</div>
<div class="item"> <img src="<?php echo $img3;?>" alt="<?php echo $alt3;?>">
<div class="container">
<div class="carousel-caption">
<h1><?php echo $head3;?></h1>
<p><?php echo $text3;?></p>
<p><a class="btn btn-lg btn-primary" href="<?php echo $link3;?>" role="button">Learn More<i class="fa fa-chevron-right"></i></a></p>
</div>
</div>
</div>
</div>

Categories