I have 2 tables in database first one is $slide_images and second is $why_us and I am using owl carousel for images.
The $slide_images contains 10 rows.
The $why_us contains 3 rows.
I need each image take one row of $why_us that's mean the first 3 images will have a text from $why_us table.
I tried many ways to do that but it's not given me what I want and I can't edit tables to do that and the id column is not nested to join them with SQL.
Can i solve them with nested foreach?
<div class="home_area">
<!-- start Carousel -->
<div class="owl-carousel block_1">
<?php
if(is_array($slide_image)){
foreach($slide_image as $src){
?>
<div class="overlay-text">
<div class="carousel_item" style="background-image:url('<?php echo base_url(); echo $src->url; ?>');"></div>
<div class="text-layer">
<!--============ start we us ================ -->
<?php if($why_us != ''){ ?>
<div class="we">
<div class="container">
<div class="row">
<?php foreach($why_us as $we){ ?>
<div class="box">
<h6><?php echo $we->name; ?></h6>
<div class="text"><?php echo $we->desc; ?></div>
</div>
<?php }?>
</div>
</div>
</div>
<?php } ?>
<!--============= end we us ========== -->
</div>
</div>
<?php } }?>
</div>
<!-- end Carousel -->
</div>
I hope I described my question well.
You should not use nested loops. That creates a cross product between the arrays.
Instead, you should just have one loop that uses the corresponding elements of both arrays. Since one of the arrays is smaller, you should check that the index exists.
foreach ($slide_image as $index => $src) {
// code that uses `$src` here
if (isset($why_us[$index])) {
$we = $why_us[$index];
// code that uses $we here
}
}
Related
I want to group the data as 3 or 2 in the foreach loop while listing the data with PHP. Is it possible to do this?
There are 30 tables in total.
I want to group by 3 by 3 and I want to give "first" class to the first data per group.
Like the picture I want to list.
Sample:
<div class="container">
<?php foreach ($query as $row): ?>
<div class="row">
<div class="post-1 first"></div>
<div class="post-2"></div>
<div class="post-3"></div>
</div>
<?php endforeach; ?>
</div>
First, you pass the data you want to group into array_chunk() and determine the limit, and then we say $key==0 in the first element of each group.
<div class="container">
<?php foreach (array_chunk($query, 3) as $chunk): ?>
<div class="row">
<?php foreach ($chunk as $key => $row): ?>
<div class="post <?php if ($key == 0): ?>first<?php endif; ?>"></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
I have setup a small webstore and I want to show this <div class ="data-plan"></div> only for one single product. My database table from where I want to match this id is called sb_files and it has these fields id table_id path type slide_link, so I am trying to get my code to go trough that table, search up the id ( Its 13040100 ) and if it matches then to show the div, else it shows an empty div. I am using the Yii2 framework, which is php based. So far I have tried this
<?php if($product->$this->id('13040100')): ?>
<ul>
<?php
$index = 1;
foreach($product->() as $prd):
if(strpos($prd->getPath(), '13040100') == true) {
?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php
$index++;
}
endforeach;
?>
</ul>
<?php else: ?>
<div class="wrap">
</div>
<?php endif; ?>
I'm not that familiar with Yii2 framework myself, but are you sure that $product->$this->id('13040100') is correct? It looks weird to me, shouldn't it be something like $product->id('13040100')?
The solution was way easier than I tought, just had to think simple
<?php if ($product->id == '13001100'): ?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>"
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php else: ?>
<div class="wrap">
</div>
<?php endif;
This question already has answers here:
How can I echo two items per one time by using PHP Foreach?
(2 answers)
How to display two table columns per row in php loop
(4 answers)
Closed 3 years ago.
I have a testimonial carousel. The carousel loop through two item each time. Now I want to get two items each time in a foreach loop. How can I get it?
Code:
<?php foreach ($kiyra_section_meta['testimonials-group'] as $single_testimonial): ?>
<div>
<div class="row">
<div class="col-md-6">
<div class="single-review">
<div class="media">
<div class="media-body">
<h3 class="mt-0"><?php echo esc_html($single_testimonial['client-name']); ?></h3>
<h5><?php echo esc_html($single_testimonial['client-position']); ?></h5>
<i class="fas fa-quote-right fa-5x"></i>
</div>
</div>
<?php echo esc_html($single_testimonial['client-testimonial']); ?>
</div> <!-- /.single-review -->
</div>
</div>
</div>
<?php endforeach; ?>
You can use array_chunk to split the array to group the two items, and then use foreach again:
foreach (array_chunk($input_array, 2) as $group) {
// Start Group
foreach ($group as $item) {
// Item
}
// End group
}
Update with HTML
<div class="owl-carousel">
<?php foreach (array_chunk($input_array, 2) as $group) : ?>
<div class="owl-item">
<?php foreach ($group as $item) : ?>
<div class="item">
<!-- Code of item -->
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
I have a simple MySQL table that stores four fields - CATEGORY, TITLE, DESCRIPTION, IMAGE as well as a unique ID for each row.
I use ORDER BY CATEGORY to display all of them on the page through one query.
SELECT
RESOLUTIONS.CATEGORY,
RESOLUTIONS.ID,
RESOLUTIONS.TITLE,
RESOLUTIONS.DESCRIPTION,
RESOLUTIONS.IMAGE
FROM
RESOLUTIONS
ORDER BY
RESOLUTIONS.CATEGORY
I want to create a jump menu that will jump to the first row of each category on the page. Is this possible using php? I know how to create a jump menu that jumps to an ID anchor of a div, but how can I make a unique identifier (that I can jump to) inside the repeat region?
Here is the repeat code I have now...
<?php
while(!$DETAILS->atEnd()) {
?>
<div class="row g-my-10 g-color-black">
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
In each iteration check if the category is different than the one before and create an anchor.
<?php
// set any initial value that does not match an empty category (if you have them)
$lastCategory = false;
while(!$DETAILS->atEnd()) {
<div class="row g-my-10 g-color-black">
<?php
// this category is different than the last: create anchor
if ($lastCategory !== $DETAILS->getColumnVal("CATEGORY")) {
echo '<a name="category-' . $DETAILS->getColumnVal("CATEGORY") . '"></a>';
// set the compare-value to the current category
$lastCategory = $DETAILS->getColumnVal("CATEGORY");
}
?>
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
Note: You might have to put the anchor within the column or even the <h2>, depending on your grid system (is that bootstrap?). You maybe also want to refactor the generation of the title/description column, so you don't repeat yourself. Something like
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
$cols = '9';
} else {
$cols = '12';
}
?>
<div class="col-md-<?php echo $cols ?>">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
?>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<?php
}
?>
Also note that according to PSR-1 you should use either <?php or the echo shortcut <?= but not just <?
And I know it's a matter of personal choice and also depends on what your file mainly contains (HTML or PHP), but I personally prefer echoing HTML code snippets instead of opening and closing PHP tags, because I find it easier to read (there is also a very marginal performance advantage)
These two lines of code specifically - ($teamproject as $teamproject) and ($cattypes as $teamproject). Basically it's an image card that displays the catergory (cattypes) and the main image and the title of that specific image card.
<div class="container" id="projectcards">
<div class="row">
<?php
if(!empty($error)){
echo $error;
}
if (!empty($teamproject)) {
foreach ($teamproject as $teamproject): ?>
<a href="/project-single/1" class="col-md-6 col-sm-6 col-xs-12 thumbnailx">
<?php foreach ($cattypes as $teamproject): ?>
<h4 class="post-content"><?= h($teamproject->cattype_title)?></h4>
<?php endforeach; ?>
<img src="<?= h($teamproject->mainimg)?>">
<h3><?= h($teamproject->title)?></h3>
</a>
<?php
endforeach;
}
?>
</div>
</div>
You're misunderstanding how foreach works in PHP. The first item should be the name of an array, which it will iterate over, and the second what you want to call each item. This statement:
foreach ($teamproject as $teamproject):
Shouldn't list the same variable twice.
If it's just a single entity, don't iterate over it at all, just directly access its values.
If it's an array containing a list of teamproject entities, then just give the item a unique name for the foreach loop:
foreach ($teamproject as $project)
The second nested foreach- if this is supposed to be a list of headers for each specific team project, then it probably should be a list inside the single $project entity? As you have it written, it's just iterating over $cattypes array, and on each iteration, printing the same $project title over and over again.
If cattypes is supposed to be a list on each $project, then its something vaguely along the lines of:
foreach($project->cattypes as $cattype)
Replacing "cattypes" with what ever that list is actually called on your teamproject entity.
Rather than adding to foreach conditions, I had to add an IF conditions to get it working.
<div class="container" id="projectcards">
<div class="row">
<?php
if (!empty($teamproject)) {
foreach ($teamproject as $team): ?>
<a href="/projects-single/<?= h($team->id)?>" class="col-md-6 col-sm-6 col-xs-12 thumbnailx">
<?php foreach ($cattypes as $cattype):
if ($team->cattype_id == $cattype->id ) {
?>
<h4 class="post-content"><?= h($cattype->title)?></h4>
<?php
} endforeach; ?>
<img src="<?= h($team->mainimg)?>">
<h3><?= h($team->title)?></h3>
</a>
<?php
endforeach;
}
?>
</div>