Needed to know more about 2d arrays in PHP - php

(I want it like this) For Example. I dont know how to create 2d arrays where i can add section title and show several packages after that i need to show logo design heading with logo design packages and etc..
<section id="packages" data-aos-delay="0" data-aos-duration="0" data-aos="fade" class="page-section">
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<h1 class="display-4" data-aos="zoom-out-up">Packages</h1>
</div>
<div class="container">
<div class="row justify-content-center mb-3">
<?php foreach ($temp_packages as $package) : ?>
<?php
$title = $package['title'];
$package_price = get_post_meta($package['id'], '_package_price', true);
$package_subtitle = get_post_meta($package['id'], '_package_subtitle', true);
$package_attributes = get_post_meta($package['id'], "_package_attributes", true);
?>
<div class="col-lg-4 col-md-4 col-sm-12">
<div class="card mb-4 box-shadow">
<div class="card-header">
<h2 class="my-3">$ <?php echo $package_price; ?></h2>
<h4 class="my-1"><?php echo $title; ?></h4>
<h6 class="mb-2"><?php echo $package_subtitle; ?></h6>
</div>
<div class="card-body">
<ul class="list-unstyled">
<?php if (gettype($package_attributes) == 'array') : ?>
<?php foreach ($package_attributes as $attribute) : ?>
<li>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
Enquire
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif; ?>

Creating 2D arrays in PHP is actually very easy:
<?php
$myArray = [
[
"title" => "foo",
"desc" => "bar"
],
[
"title" => "foo2",
"desc" => "bar2"
],
[
"title" => "foo3",
"desc" => "bar3"
],
[
"title" => "foo4",
"desc" => "bar4"
]
];
echo $myArray[0]["title"]; // foo
foreach($myArray as $value) {
echo $value["title"];
}
// foo foo2 foo3 foo4
Is that what you were looking for?

I just created an Array PHP: Arrays with all the content you need. Here is my array with one example entry:
$temp_packages = Array(
[
'id' => 12345,
'title' => 'Some Title',
'subtitle' => 'Some Subtitle',
'price' => 129.99,
'attributes' => [
'An Attribute',
'Some more Stuff',
'foo',
'bar'
]
],
);
The content of $temp_packages will probaly be stored in a database and be fetched from there.
After that, you can use your code to generate your section. I changed a bit, so it works for me:
<section id="packages" data-aos-delay="0" data-aos-duration="0" data-aos="fade" class="page-section">
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<h1 class="display-4" data-aos="zoom-out-up">Packages</h1>
</div>
<div class="container">
<div class="row justify-content-center mb-3">
<?php foreach ($temp_packages as $package) : ?>
<div class="col-lg-4 col-md-4 col-sm-12">
<div class="card mb-4 box-shadow">
<div class="card-header">
<h2 class="my-3">$ <?php echo $package['price']; ?></h2>
<h4 class="my-1"><?php echo $package['title']; ?></h4>
<h6 class="mb-2"><?php echo $package['subtitle']; ?></h6>
</div>
<div class="card-body">
<ul class="list-unstyled">
<?php if (gettype($package['attributes']) == 'array') : ?>
<?php foreach ($package['attributes'] as $attribute) : ?>
<li>
<?php echo $attribute; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
Enquire
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</section>

Related

php have code skip button click to next step and go straight to the next step automatically

I am editing a template that currently has two steps. I want to make it automatically go to step two upon loading, or skip step 1 altogether. I have tried using the headerfunction but that didnt seem to get me what I needed.
At the bottom of the page on step 1 there is a "create new product button" that is the link I am trying to skip to without having to click the button.
Here is the code:
<div class="col-md-12 add-product-outer-wrapper">
<div class="select-product-cat-wrapper">
<?php $is_new_listing = isset($_REQUEST['new_listing']) ? true : false;
$is_cats_hier = isset($_REQUEST['cats_hier']) ? true : false;
if( ( $is_new_listing && $is_cats_hier ) || !get_wcmp_vendor_settings('is_singleproductmultiseller', 'general') == 'Enable' ) {
?>
<!-- New product list categories hierarchically -->
<div class="select-cat-step-wrapper">
<div class="cat-step1" >
<div class="panel panel-default pannel-outer-heading mt-0">
<div class="panel-heading d-flex">
<h1><span class="primary-color"><span><?php _e( 'Step 1 of', 'dc-woocommerce-multi-vendor' );?></span> <?php _e( '2:', 'dc-woocommerce-multi-vendor' );?></span> <?php _e('Select a product category', 'dc-woocommerce-multi-vendor'); ?></h1>
<h3><?php _e('Once a category is assigned to a product, it cannot be altered.', 'dc-woocommerce-multi-vendor'); ?></h3>
</div>
<div class="panel-body panel-content-padding form-horizontal breadcrumb-panel">
<?php echo $url; ?>
</div>
</div>
<div class="panel panel-default pannel-outer-heading wcmp-categories-level-panel has-scroller">
<div class="cat-column-scroller cat-left-scroller"><i class="wcmp-font ico-left-arrow-icon"></i></div>
<div class="form-horizontal cat-list-holder">
<div class="wcmp-product-categories-wrap cat-column-wrapper">
<div class="wcmp-product-cat-level 1-level-cat cat-column" data-level="1" data-mcs-theme="dark">
<ul class="wcmp-product-categories 1-level" data-cat-level="1">
<?php echo wcmp_list_categories( apply_filters( 'wcmp_vendor_product_classify_1_level_categories', array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'html_list' => true,
'cat_link' => 'javascript:void(0)',
) ) ); ?>
</ul>
</div>
</div>
</div>
<div class="cat-column-scroller cat-right-scroller"><i class="wcmp-font ico-right-arrow-icon"></i></div>
</div>
</div>
</div>
<?php }else{ ?>
<!-- List a product by name or gtin -->
<div class="cat-intro">
<div class="panel panel-default pannel-outer-heading mt-0">
<div class="panel-body panel-content-padding form-horizontal text-center">
<img src="<?php echo $WCMp->plugin_url.'assets/images/add-product-graphic.png'; ?>" alt="">
<h1 class="heading-underline"><?php _e('List a New Product', 'dc-woocommerce-multi-vendor'); ?></h1>
<div class="serach-product-cat-wrapper">
<h2><?php// _e('Search from our existing Product Catalog', 'dc-woocommerce-multi-vendor'); ?></h2>
<?php
if (get_option('permalink_structure')) {
$category_url = '?new_listing=1&cats_hier=1';
} else {
$category_url = wcmp_get_vendor_dashboard_endpoint_url( get_wcmp_vendor_settings( 'wcmp_add_product_endpoint', 'vendor', 'general', 'add-product' ) ) . '&new_listing=1&cats_hier=1';
}
$url = ( get_wcmp_vendor_settings('is_disable_marketplace_plisting', 'general') == 'Enable' ) ? esc_url(wcmp_get_vendor_dashboard_endpoint_url(get_wcmp_vendor_settings('wcmp_edit_product_endpoint', 'vendor', 'general', 'edit-product'))) : $category_url; ?>
<p><?php //_e('Not in the catalog?', 'dc-woocommerce-multi-vendor'); ?> <?php _e('Create a new product', 'dc-woocommerce-multi-vendor'); ?> <i class="wcmp-font ico-right-arrow-icon"></i></p>
</div>
</div>
</div>
<!-- End List a product by name or gtin -->
<?php } ?>
<div class="clearfix"></div>
</div>
</div>

How to use pjax with pagination in yii2

I want to use pjax with pagination in yii2.
When user clicks on next page, i want to reload only one div with dynamic contents.
I have put pjax::begin() and pjax::end() before and end of my div.
This is my controller code :
public function actionRestaurantDetails(){
$this->layout = "detail";
if(isset($_REQUEST['rid']) && !empty($_REQUEST['rid'])){
$rid = $_REQUEST['rid'];
$snRestaurantsDetail = Restaurants::find()->where(['id'=>$_REQUEST['rid']])->one();
$snRestaurantMenuCategoryArr = MenuCategories::find()->where(["status"=>"1"])->all();
$snRestaurantMenusArr = RestaurantMenu::find()->where(['restaurant_id'=>$_REQUEST['rid'],'status'=>"1"])->all();
$query = RestaurantsGallery::find()->where(['restaurant_id'=>$_REQUEST['rid'],'status'=>"1"]);
$snRestaurantgallerysArr = RestaurantsGallery::find()->where(['restaurant_id'=>$_REQUEST['rid'],'status'=>"1"])->all();
$pagination = new Pagination(['totalCount' => $query->count(), 'pageSize'=>3]);
$models = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
$snRestaurantLayoutsArr = $snRestaurantsDetail->getRestaurantLayouts();
$snRestaurantTablesArr = $snRestaurantsDetail->getRestaurantTables();
$snRestaurantWorkingHoursArr = $snRestaurantsDetail->getRestaurantWorkingHours();
$snRestaurantMealTimesArr = $snRestaurantsDetail->getRestaurentMealTimes();
//p($snRestaurantsDetail);
}
return $this->render('restaurant_detail', [
'snRestaurantsDetail' => $snRestaurantsDetail,
'snRestaurantMenuCategoryArr' => $snRestaurantMenuCategoryArr,
'snRestaurantMenusArr' => $snRestaurantMenusArr,
'snRestaurantMealTimesArr' => $snRestaurantMealTimesArr,
'snRestaurantWorkingHoursArr' => $snRestaurantWorkingHoursArr,
'snRestaurantgallerysArr' => $snRestaurantgallerysArr,
'models' => $models,
'pagination' => $pagination,
]);
}
And this is my view :
<?php
use common\models\RestaurantMenu;
use yii\widgets\Pjax;
?>
<div class="container">
<div class="row align-items-center site-vh-100">
<div class="col-md-12">
<?php
$url = Yii::getAlias('#web')."/img/chiefs-rs-text.png";
?>
<!-- <a class="brand" href="#"><img src="<?php echo $url; ?>" width="35%" height="35%"></a> -->
<h1 class="site-heading site-animate mb-3"><?= !empty($snRestaurantsDetail) ? $snRestaurantsDetail->name : "-" ?></h1>
<h2 class="h5 site-subheading mb-5 site-animate">Please book our restaurant now</h2>
<?php if(Yii::$app->user->isGuest){ ?>
<p>For Booking Restaurant :Register Now</p>
<?php }else{ ?>
<p>Book Now</p>
<?php } ?>
</div>
</div>
</section>
<section class="site-section section_details" id="section-about">
<div class="container">
<div class="row">
<div class="col-md-5 site-animate mb-5">
<h4 class="site-sub-title">Our Story</h4>
<h2 class="site-primary-title display-4">Welcome</h2>
<p><?= !empty($snRestaurantsDetail->description) ? $snRestaurantsDetail->description : "No contents found." ?></p>
<!-- <p class="mb-4">A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.</p> -->
</div>
<div class="col-md-1"></div>
<div class="col-md-6 site-animate img" data-animate-effect="fadeInRight">
<img src="<?= ($snRestaurantsDetail->photo) ? Yii::getAlias('#web')."../../../uploads/".$snRestaurantsDetail->photo : "No image uploaded"?>" alt="chiefsRS" class="img-fluid" style="width:100%;height: 60vh">
</div>
</div>
</div>
</section>
<section class="site-section section_details" id="section-menu">
<div class="container">
<div class="row">
<div class="col-md-12 text-center mb-5 site-animate">
<h2 class="display-4">Delicious Menu</h2>
<div class="row justify-content-center">
<div class="col-md-7">
<p class="lead">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
</div>
</div>
</div>
<div class="col-md-12 text-center">
<ul class="nav site-tab-nav nav-pills mb-5" id="pills-tab" role="tablist">
<?php
if(!empty($snRestaurantMenuCategoryArr)){
foreach ($snRestaurantMenuCategoryArr as $key => $category) { ?>
<li class="nav-item site-animate">
<a class="nav-link <?= (strtolower($category->name) == "breakfast") ? 'active' : '' ?>" id="pills-<?= strtolower($category->name)?>-tab" data-toggle="pill" href="#pills-<?= strtolower($category->name)?>" role="tab" aria-controls="pills-<?= strtolower($category->name);?>" aria-selected="true"><?= $category->name;?></a>
</li>
<?php }
}
?>
</ul>
<div class="tab-content text-left">
<?php
if(!empty($snRestaurantMenuCategoryArr)){
foreach ($snRestaurantMenuCategoryArr as $key => $category) { ?>
<div class="tab-pane fade <?= (strtolower($category->name) == "breakfast") ? 'show active' : '' ?>" id="pills-<?= strtolower($category->name)?>" role="tabpanel" aria-labelledby="pills-<?= strtolower($category->name)?>-tab">
<div class="row">
<?php
$breakfastMenu = RestaurantMenu::find()->where(['restaurant_id'=>$_REQUEST['rid'],'menu_category_id'=>$category->id,'status'=>"1"])->all();
// p($breakfastMenu);
if(!empty($breakfastMenu)){
foreach ($breakfastMenu as $key_menu => $menu) { ?>
<div class="col-md-6 site-animate">
<div class="media menu-item">
<img class="mr-3" src="<?php echo Yii::getAlias('#web')."/../../uploads/".$menu->photo; ?>" class="img-fluid" alt="chiefsRS">
<div class="media-body">
<h5 class="mt-0"><?= !empty($menu->name) ? $menu->name : ""?></h5>
<p><?= !empty($menu->description) ? $menu->description : ""?></p>
<h6 class="text-primary menu-price"><?= !empty($menu->price) ? "$".$menu->price : ""?></h6>
</div>
</div>
</div>
<?php
}
}
?>
</div>
</div>
<?php }
}
?>
</div>
</div>
</div>
</div>
</section>
<!-- END section -->
<?php
Pjax::begin(['id' => 'gallery_r','timeout'=>100000]); ?>
<section class="site-section section_details" id="section-gallery">
<div class="container">
<div class="row site-custom-gutters">
<div class="col-md-12 text-center mb-5 site-animate">
<h2 class="display-4">Gallery</h2>
<div class="row justify-content-center">
<div class="col-md-7">
<p class="lead">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
</div>
</div>
</div>
<div id="categories"></div>
<?php if (!empty($models)) {
foreach ($models as $key => $image) {
?>
<div class="col-md-4 site-animate">
<a href="<?php echo Yii::getAlias('#web') . "../../../uploads/" . $image->image_name; ?>" class="site-thumbnail image-popup">
<img src="<?php echo Yii::getAlias('#web') . "../../../uploads/" . $image->image_name; ?>" alt="chiefsRS" class="img-fluid" style="width:80%;height:50vh;" title="<?= $image->image_title; ?>">
</a>
</div>
<?php
}
}
?>
</div>
</div>
</section>
<?php echo \yii\widgets\LinkPager::widget([
'pagination' => $pagination,
]);
Pjax::end();?>
<!-- END section -->
<section class="site-section section_details bg-light" id="section-contact">
<div class="container">
<div class="row">
<div class="col-md-12 text-center mb-5 site-animate">
<h2 class="display-4">Get In Touch</h2>
<div class="row justify-content-center">
<div class="col-md-7">
<p class="lead">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
<p class="text-black">
<?php
if(!empty($snRestaurantsDetail)){ ?>
Address: <br><?= $snRestaurantsDetail->address; ?><br>
Phone: <br><?= $snRestaurantsDetail->contact_no; ?><br> <br>
Email: <br> <?= $snRestaurantsDetail->email; ?>
<?php } ?>
</p>
</div>
</div>
</div>
</div>
</div>
</section>
<div id="map"></div>
<?php// p($snRestaurantsDetail); ?>
<input type="hidden" name="lat" id="lat" value="<?= $snRestaurantsDetail->lattitude; ?>">
<input type="hidden" name="long" id="long" value="<?= $snRestaurantsDetail->longitude; ?>">
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBvpANF446OIBFdLaqozAf-lheEZ__oVVg&libraries=geometry"></script>
<script type="text/javascript">
$.pjax.reload({container: '#gallery_r'});
</script>
<?php Pjax::begin(['id'=>'gallery_r']);
if(!empty($models)){
foreach ($models as $key => $image) { ?>
<div class="col-md-4 site-animate">
<a href="<?php echo Yii::getAlias('#web')."../../../uploads/".$image->image_name;?>" class="site-thumbnail image-popup">
<img src="<?php echo Yii::getAlias('#web')."../../../uploads/".$image->image_name;?>" alt="chiefsRS" class="img-fluid" style="width:80%;height:50vh;" title="<?= $image->image_title;?>">
</a>
</div>
<?php }
Pjax::end();
echo \yii\widgets\LinkPager::widget([
'pagination' => $pagination,
]);
}?>
And this is my js code :
$.pjax.reload({container: '#gallery_r'});
And I call this jquery to add class after pjax load to display contents :
jQuery(document).on("pjax:success", "#gallery_r", function(event){
contentWayPoint();
}
);
var contentWayPoint = function() {
var i = 0;
$('.site-animate').waypoint( function( direction ) {
if( direction === 'down' && !$(this.element).hasClass('site-animated') ) {
i++;
$(this.element).addClass('item-animate');
setTimeout(function(){
$('body .site-animate.item-animate').each(function(k){
var el = $(this);
setTimeout( function () {
var effect = el.data('animate-effect');
if ( effect === 'fadeIn') {
el.addClass('fadeIn site-animated');
} else if ( effect === 'fadeInLeft') {
el.addClass('fadeInLeft site-animated');
} else if ( effect === 'fadeInRight') {
el.addClass('fadeInRight site-animated');
} else {
el.addClass('fadeInUp site-animated');
}
el.removeClass('item-animate');
}, k * 50, 'easeInOutExpo' );
});
}, 100);
}
} , { offset: '95%' } );
};
And on one button click I am calling this URL like this :
<p class="mb-0">Read More</p>
This URL calls as ajax call and loads only pjax container div, When i reload this page manually then it will display the whole page.I don't know why this happened ?
You should move the LinkPager inside the Pjax scope.
<?php
Pjax::begin(['id' => 'gallery_r']);
if (!empty($models)) {
foreach ($models as $key => $image) {
?>
<div class="col-md-4 site-animate">
<a href="<?php echo Yii::getAlias('#web') . "../../../uploads/" . $image->image_name; ?>" class="site-thumbnail image-popup">
<img src="<?php echo Yii::getAlias('#web') . "../../../uploads/" . $image->image_name; ?>" alt="chiefsRS" class="img-fluid" style="width:80%;height:50vh;" title="<?= $image->image_title; ?>">
</a>
</div>
<?php
}
echo \yii\widgets\LinkPager::widget([
'pagination' => $pagination,
]);
Pjax::end();
}
?>

Show Data database only with specific ID

I have this code to show all data in my database:
<?php foreach($alldatas as $data): ?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
That code works perfect to show all my data with different data_name and data_version. But what I want is to show the data only with specific ID.
So I want to make it work with bootstrap navtabs:
<div class="col-md-12">
<ul class="nav nav-pills nav-tabs nav-justified" role="tablist">
<li role="presentation" class="active">Pro</li>
<li role="presentation">Middle</li>
<li role="presentation">Low</li>
</ul>
</div>
<?php foreach($alldatas as $data): ?>
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="pro">
<?php if($data['id'] == "2" | $data['id'] == "3" | $data['id'] == "4"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<div role="tabpanel" class="tab-pane fade" id="middle">
<?php if($data['id'] == "5" | $data['id'] == "6" | $data['id'] == "7"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php endforeach; ?>
But it just only show data with ID 1,2,3 when I try to click other tabs. How to fix this?
You are having foreach loop on "tab-content" which is wrong, actually, it will create so many "tab-content"s and that's not required. You MUST loop for content not "tab-content".
So, first fix would be:
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="pro">
<?php foreach($alldatas as $data): ?>
<?php if($data['id'] == "2" | $data['id'] == "3" | $data['id'] == "4"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
</div>
<div role="tabpanel" class="tab-pane fade" id="middle">
<?php foreach($alldatas as $data): ?>
<?php if($data['id'] == "5" | $data['id'] == "6" | $data['id'] == "7"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
</div>
</div>
It will resolve your issue, however, if it still persists, you can implement a jquery function for custom toggling of "active" class. (Still, first correct the position of "foreach" as shown above).

Wordpress, blog-page dont show blogs

I merge wordpress with bootstrap and i want get something like this:
1|
| 2
3|
I check if there is post (there is 3 at that moment). Next is loop and showing the blogs... But its show empty boxes...
What i done wrong ?
Or maybe there is better way to do this?
<div class="wrapper">
<?php
$rest_query = new WP_Query(array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => array('post'),
'post_status' => 'publish'
));
if($rest_query->have_posts()):
?>
<?php while($rest_query->have_posts()): $rest_query->the_post(); ?>
<?php
if ($rest_query->current_post == 0)
{
echo '<div class="row">
<div class="col-md-6">
<div class="single first-post">
<div class="thumb"><?php the_post_thumbnail(); ?></div>
<div class="content">
<h1><?php the_title(); ?></h1>
<div class="data">
<p class="date"><?php echo get_the_date();s ?></p>
<p class="social">0 shares / 0 comments</p>
</div>
</div>
</div>
</div>
<div class="middleLine"></div>
<div class="col-md-6"></div>
</div>';
}
elseif ($rest_query->current_post == 1)
{ echo '<div class="row">
<div class="col-md-6"></div>
<div class="middleLine"></div>
<div class="col-md-6">
<div class="single secound-post">
<div class="thumb"><?php the_post_thumbnail(); ?></div>
<div class="content">
<h1><?php the_title(); ?></h1>
<div class="data">
<p class="date"><?php echo get_the_date();s ?></p>
<p class="social">0 shares / 0 comments</p>
</div>
</div>
</div>
</div>
</div>'; }
?>
<?php endwhile; ?>
<?php endif; ?>
</div>
I think you code has some error while showing in the page. While you use ECHO you should not use PHP tag inside it which is why it is not working in your case.
Revising your code:
<div class="wrapper">
<?php
$rest_query = new WP_Query(array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => array('post'),
'post_status' => 'publish'
));
if($rest_query->have_posts()):
?>
<?php while($rest_query->have_posts()): $rest_query->the_post(); ?>
<?php
if ($rest_query->current_post == 0)
{
echo '<div class="row">
<div class="col-md-6">
<div class="single first-post">
<div class="thumb">'.the_post_thumbnail().'</div>
<div class="content">
<h1>'.the_title().'</h1>
<div class="data">
<p class="date">'.get_the_date().'</p>
<p class="social">0 shares / 0 comments</p>
</div>
</div>
</div>
</div>
<div class="middleLine"></div>
<div class="col-md-6"></div>
</div>';
}
elseif ($rest_query->current_post == 1)
{ echo '<div class="row">
<div class="col-md-6"></div>
<div class="middleLine"></div>
<div class="col-md-6">
<div class="single secound-post">
<div class="thumb">'.the_post_thumbnail().'</div>
<div class="content">
<h1>'.the_title().'</h1>
<div class="data">
<p class="date">'.get_the_date().'</p>
<p class="social">0 shares / 0 comments</p>
</div>
</div>
</div>
</div>
</div>'; }
?>
<?php endwhile; ?>
<?php endif; ?>
NOTE: If you want to print any PHP variable then it should be used as i have have shown above code.
THank you

Display foreach for different keys in an array in PHP

I have the following array:
<?php
$sets = array (
array (
'img' => 'file.png',
'heading' => 'Slide Title 1',
'lead' => 'Slide leadription 1',
),
array (
'img' => 'file.png',
'heading' => 'Slide Title 2',
'lead' => 'Slide leadription 2',
),
array (
'img' => 'file.png',
'heading' => 'Slide Title 3',
'lead' => 'Slide leadription 2',
),
array (
'img' => 'file.png',
'heading' => 'Slide Title 3',
'lead' => 'Slide leadription 2',
)
);
?>
Which provides the input for this
<?php
foreach ($sets as $set) {
?>
<!-- START THE FEATURETTES -->
<div class="row featurette">
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
</div>
<?php
}
?>
Now this is working perfectly but I want the md-7 HTML and md-5 HTML to alternate so every other one will now be
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
</div>
So basically alternating between the picture and details left-right
UPDATE
As per Jhansen's suggestion This DOES NOT WORK. It will only take the first set, it won't alternate between the two.
<?php
foreach ($sets as $set) {
?>
<!-- START THE FEATURETTES -->
<?php $count = 1; ?>
<?php if( $count % 2 != 0 ): ?>
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
</div>
<?php else: ?>
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
</div>
<?php endif; ?>`
<?php
}?>
If you're just wanting to alternate, why not encapsulate the html within a modulus statement?
IE,
<?php $count = 1; ?>
<?php if( $count % 2 != 0 ): ?>
... HTML for first arrangement ...
<?php else: ?>
... HTML for second arrangement ...
<?php endif; ?>`
If you want to switch all loops, the proposed jhansen is correct:
<?php
$count=1;
foreach ($sets as $set) { ?>
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<?php if($count % 2 != 0){ ?>
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<?php }else{ ?>
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<?php } ?>
</div>
<?php $count++; } ?>
If you want to switch only the first should do something like this:
<?php
foreach ($sets as $k => $set) { ?>
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<?php if($k==0){ ?>
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<?php }else{ ?>
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<?php } ?>
</div>
<?php } ?>

Categories