php two arrays into string - php

With php print_r I got final result as :
Array ( [0] => 31 [1] => 21 ) Array ( [0] => 33 [1] => 27 )
And I want result to be like 31,21,33,27.
I am stuck up. Please guide me through.
Thanks,
Vikram
**Please bare with lengthy stuff. **
I have 2 main categories and need to display 3 fourth level children on home page. Categories are like :
Category > Clothing.
Category > Clothing > Mens.
Category >> Clothing > Mens > Top.
Category > Clothing > Mens > Top > Jeans.
CODES
<div class="container products" data-aos="fade-down">
<div class="page-titles" data-aos="fade-up">
Products
<hr />
</div>
<!--page-titles-->
<?php
$homepage_products=get_post_meta($post->ID, 'homepage_products', TRUE);
$homepage_parent=explode(',', $homepage_products);
global $parent_one;
global $x;
$x=1;
foreach($homepage_parent as $parent_one)
{
$parent_one_title = get_the_title( $parent_one );
$content_post = get_post($parent_one);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
?>
<div class="row clothing">
<div id="<?php echo $x++; ?>" class="col-xs-12 col-sm-12 col-md-4 col-lg-4
<?php if($x % 2)
{
echo 'col-md-push-8 col-lg-push-8';
}
?>
">
<?php echo get_the_post_thumbnail($parent_one, 'medium_large', array('class' => 'img-'.$x)); ?>
</div>
<!--col-4 img-->
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8
<?php if($x % 2)
{
echo 'col-md-pull-4 col-lg-pull-4';
}
?>
">
<h2><?php echo $parent_one_title; ?></h2>
<?php echo $content; ?>
<!-- child pages of <?php echo strtolower($parent_title); ?> starts-->
<div class="row">
<?php
$parent_two=get_pages( array('parent'=>$parent_one,'child_of'=>$parent_one) );
foreach($parent_two as $parent_two_ids)
{
$parent_two_id=$parent_two_ids->ID;
$parent_two_id.=",";
$parent_three=get_pages( array('parent'=>$parent_two_id) );
$str_id = array();
foreach($parent_three as $parent_three_ids)
{
$parent_three_id=$parent_three_ids->ID;
$parent_three_id.=',';
$parent_four=get_pages( array('parent'=>$parent_three_id) );
$four_ids=array();
foreach($parent_four as $parent_four_ids)
{
$four_id=$parent_four_ids->ID;
$four_ids[]=$four_id;
}//parent_four_ids
// HERE IS MY ISSUE //
echo implode(',', array_merge($four_ids) );
// HERE IS MY ISSUE //
//$ids=implode(',',$four_ids);
//$idx=$ids;
//$idv=preg_replace('#\s+#',',',trim($idx));
//echo $idv;
//$str_id[]=$idx;
//$str_idx=implode(',',$str_id);
//echo $str_idx;
$content=apply_filters('the_content', $parent_four_ids->post_content);
$c_length = 80;
if (strlen($content) > $c_length)
{
$content = wordwrap($content, 80);
$i = strpos($content, "\n");
if ($i) {
$content = substr($content, 0, $i);
}
}
?>
<!-- <div class="col-xs-12 col-sm-12 col-md-3 col-lg-3 product">
< ?php echo get_the_post_thumbnail($parent_four_ids, 'medium_large'); ?>
<a href="< ?php echo get_permalink($parent_four_ids);?>">
<h4>< ?php echo $parent_four_ids->post_title; ?></h4>
</a>
< ?php echo $content; ?>
</div><!--col 3 #< ?php echo $parent_four_ids->ID; ?>-->
<?php
}//parent_three_ids
} // parent_two_ids
?>
</div>
<!-- row category - slider- child pages of <?php echo strtolower($parent_one_title); ?> ends-->
</div>
<!-- col-6 content - child pages-->
</div>
<!--row <?php echo strtolower($parent_title); ?>-->
<? } //home_parent ?>
</div>
<!--container products-->

Just as iainn said above:
php > echo implode(',', array_merge([31, 21], [33, 27]));
31,21,33,27
If you wanted a single array and not a string, remove the implode() call.

Related

PHP - Adding 2 div to a foreach loop every 4 times with ACF Repeater

I have an ACF Repeater field i'd like to output as an accordion grid, like so:
<div class="intro row">
<div class="item item-1">name 1</div>
<div class="item item-2">name 2</div>
<div class="item item-3">name 3</div>
<div class="item item-4">name 4</div>
</div>
<div class="expanded row">
<div class="expand" id="item-1">expanded info 1</div>
<div class="expand" id="item-2">expanded info 2</div>
<div class="expand" id="item-3">expanded info 3</div>
<div class="expand" id="item-4">expanded info 4</div>
</div>
<div class="intro row">
<div class="item item-5">name 5</div>
<div class="item item-6">name 6</div>
<div class="item item-7">name 7</div>
<div class="item item-8">name 8</div>
</div>
<div class="expanded row">
<div class="expand" id="item-5">expanded info 5</div>
<div class="expand" id="item-6">expanded info 6</div>
<div class="expand" id="item-7">expanded info 7</div>
<div class="expand" id="item-8">expanded info 8</div>
</div>
I can group the initial row fine, it's just the second "expanded" row i'm having trouble with. How can I repeat and group the second row of 4 correctly in the same loop? My current PHP:
<?php // check if the repeater field has rows of data
if( have_rows('features') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
while ( have_rows('features') ) : the_row();
$name = get_sub_field('feature_name');
$expandedInfo = get_sub_field('feature_info');
if ($count % 4 == 0) {
$group++;
?>
<div class="intro row">
<?php
}
?>
<div class="item item-<?php echo $count; ?>">
<?php echo $name ?>
</div><!-- item-->
<?php
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<?php
}
$count++;
endwhile;
else :
// no rows found
endif;
?>
The second 'expanded' row can be done so that you store each count (item-1,item-2) in an array or just traverse through all the count when you close the intro row.
<?php
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<div class="expanded row">
<?php
$start = $count-3;
// if $count is 4, $start will be 1, and the $i will go to 4
// if $count is 8, $start will be 5
for($i=$start;$i<=$count;$i++){
echo '<div class="expand" id="item-' . $i . '"></div>';
} ?>
</div>
<?php
}
This is just an example. I would suggest you to store each $count in an array and then use the count($array) to get the number of them. After you have traversed the array, reset it.
The Array Approach
<?php // check if the repeater field has rows of data
if( have_rows('features') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
// Content Array
$content_array = array();
while ( have_rows('features') ) : the_row();
$name = get_sub_field('feature_name');
$expandedInfo = get_sub_field('feature_info');
// Adding the Expanded Info
$content_array[ 'item-' . $count ] = $expandedInfo;
if ($count % 4 == 0) {
$group++;
?>
<div class="intro row">
<?php
}
?>
<div class="item item-<?php echo $count; ?>">
<?php echo $name ?>
</div><!-- item-->
<?php
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<div class="expanded row">
<?php
foreach( $content_array as $item_id => $expanded_info ) {
echo '<div class="expanded" id="' . $item_id . '">';
echo $expanded_info;
echo '</div>';
} ?>
</div>
<?php
// Resetting the Array
$content_array = array();
}
$count++;
endwhile;
else :
// no rows found
endif;
?>
okey, lets see ,
using variables to store your templates may helps a lot in this context ,
as follow :
$intro = '';
$expanded = '';
while (have_rows('features')) :
the_row();
if ($count % 4 == 0) {
$group++;
$intro .= '<div class="intro row">';
$expanded .= '<div class="expanded row">';
}
$intro .= '<div class="item item-' . $count . '"></div><!-- item-->';
$expanded .= '<div class="expand" id="item-' . $count . '"></div>';
if ($count % 4 == 3) {
$intro = '</div><!-- intro-->';
$expanded = '</div><!-- intro-->';
}
$count++;
endwhile;
I've made a quick example to explain to show you how using variables may fix your issue : https://3v4l.org/cKPP4
Can't test it right now, but I think something like that should work.
<?php // check if the repeater field has rows of data
if( have_rows('features') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
$array = array();
while ( have_rows('features') ) : the_row();
$name = get_sub_field('feature_name');
$expandedInfo = get_sub_field('feature_info');
if ($count % 4 == 0) {
$group++;
?>
<div class="intro row">
<?php
}
?>
<div class="item item-<?php echo $count; ?>">
<?php echo $name ?>
</div><!-- item-->
<?php
array_push($array, $expandedInfo);
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<div class="expanded row">
<?php
for ($i=0; $i < count($array); $i++) {
echo '<div class="expand" id="item-'.$i + 1.'">'.$array[$i].'</div>';
}
echo '</div>';
}
$count++;
endwhile;
else :
// no rows found
endif;
?>

Bootstrap Carousel Thumbnail Silider with PHP codeigniter

I am trying to get a bootstrap carousel to perform like this example below but with some php code.
http://www.bootply.com/81478
But with php code where it has row and columns. I have 5 images that I have displayed but each one for some reason comes in as a new slide.
It should show about 4 - 5 images on one row. But with my php code does not seem to work properly only shows one image each slide.
My Question is: In my view is there any way to be able to make each slide to display about 4 images.
My View Is
<div id="carousel-thumbnail<?php echo $module;?>" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php foreach ($banners as $i => $banner) { ?>
<li data-target="#carousel-thumbnail<?php echo $module; ?>" data-slide-to="<?php echo $i; ?>"<?php echo !$i ? ' class="active"' : ''; ?>></li>
<?php } ?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php foreach ($banners as $i => $banner) { ?>
<div class="item<?php echo !$i ? ' active' : ''; ?>">
<div class="row">
<div class="col-lg-3">
<img src="<?php echo $banner['src']; ?>" class="img-thumbnail">
</div>
</div>
</div>
<?php } ?>
</div>
<!-- Controls -->
<?php if (count($banners) > 1) { ?>
<a class="carousel-control left" href="#carousel-thumbnail<?php echo $module; ?>" data-slide="prev">‹</a>
<a class="carousel-control right" href="#carousel-thumbnail<?php echo $module; ?>" data-slide="next">›</a>
<?php } ?>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#carousel<?php echo $module;?>').carousel({
interval: 10000
});
});
</script>
My Controller
<?php
class Carousel extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function index($info) {
static $module = 0;
$data['banners'] = array();
$banners = $this->get_banner($info[0]['module_display_id']);
foreach ($banners as $banner) {
if (is_file(FCPATH . 'images/' . $banner['banner_image'])) {
$data['banners'][] = array(
'src' => base_url() . 'images/' . $banner['banner_image']
);
} else {
$data['banners'][] = array(
'src' => $banner['banner_image']
);
}
}
$data['module'] = $module++;
$this->load->view('theme/default/template/module/carousel', $data);
}
public function get_banner($banner_id) {
$this->db->select('*');
$this->db->from('banner b', 'LEFT');
$this->db->join('banner_image bi', 'bi.banner_id = b.banner_id', 'LEFT');
$this->db->where('b.banner_id', $banner_id);
$this->db->where('b.banner_status', '1');
$this->db->order_by('banner_sort_order', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
}
PHP code for groups of four images
Indicators
Each item is a group of four images. The indicator indicates the group, rather than the image. Therefore, the number of indicators is four times less than the number of images.
<!-- Indicators -->
<ol class="carousel-indicators">
<?php for ($i = 0; $i < ceil(count($banners) / 4); $i++) { ?>
<li data-target="#carousel-thumbnail<?php echo $module; ?>" data-slide-to="<?php echo $i; ?>"<?php echo !$i ? ' class="active"' : ''; ?>></li>
<?php } ?>
</ol>
Items
You need to wrap in the row a series of four slide, rather than one by one:
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php foreach ($banners as $i => $banner) { ?>
<?php if (($i > 0) && ($i % 4 == 0)) { ?>
</div>
</div>
<div class="item">
<div class="row">
<?php } ?>
<div class="col-lg-3">
<img src="<?php echo $banner['src']; ?>" class="img-thumbnail">
</div>
<?php } ?>
</div>
</div>
</div>

How can I display a two column array in PHP/HTML?

I have an array of icons. Right now, we only display them in buckets of 4. So if you have 7 icons, the 4th icon on the first slide will repeat as the 8th on the second slide. That's because the 3rd index of the array is stored in that same spot. To fix this, I want to loop through the array instead of explicitly iterating icon by icon.
<?php if (!empty($icons)) { // if we have icons attributes
// NOTE: we've changed it to show as many sets as we can
// (sets of 4)
$number_of_sets = ceil(count($icons) / 4);
$k=0; // for inner iteration
for ($j=0;$j < $number_of_sets; $j++) {
$slide_total ++;
?>
<div class="cf slide icon-slide">
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon1 = $icons[$k];
$k++;
}
?>
<div class="col left">
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon1['thumb']?>" alt="<?=htmlentities($icon1['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon1['post_title']?></h4>
<p><?=$icon1['post_content']?></p>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon2 = $icons[$k];
$k++;
}
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon2['thumb']?>" alt="<?=htmlentities($icon2['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon2['post_title']?></h4>
<p><?=$icon2['post_content']?></p>
</div>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon3 = $icons[$k];
$k++;
}
?>
<div class="colR right">
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon3['thumb']?>" alt="<?=htmlentities($icon3['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon3['post_title']?></h4>
<p><?=$icon3['post_content']?></p>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon4 = $icons[$k];
$k++;
}
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon4['thumb']?>" alt="<?=htmlentities($icon4['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon4['post_title']?></h4>
<p><?=$icon4['post_content']?></p>
</div>
</div>
</div>
</div> <!-- // end icon slide -->
<?php
} // end for $j (number of sets of 4 icons
?>
My proposed solution:
<?php if (!empty($icons)) {
$num_cols = 2;
$i = 0;
$slide_items = 4;
?>
<div class="cf slide icon-slide">
<?php foreach ( $icons as $icon ) {
echo $i++%$num_cols==0 ? '</div><div class="col" style="width: 250px;">' : '';
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon['thumb']?>" alt="<?=htmlentities($icon['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon['post_title']?></h4>
<p><?=$icon['post_content']?></p>
</div>
</div>
<?php } } // end if we have icons attributes ?>
I'm having trouble figuring out how to make another slide after I hit 4 icons. Adding the following line before the end of the foreach loop hasn't worked.
echo $i++%$slide_items==0 ? '</div><div class="cf slide icon-slide">' : '';
Here's some logic for the loop:
$i = count( $icons );
if ( $i % 4 != 0 )
$i = $i + ( $i % 4 );
for( $n = 0; $n < $i; $n++ )
{
if ( $n % 4 == 0 )
{
// code to open a row here
}
// code to open a column here
// echo your icon here, use isset() or !empty().
// code to close a column here
if ( $n > 0 && $n % 3 == 0 )
{
// code to close a row here
}
}
The IFs at the top is for keeping the column number consistent. Otherwise there'll be some weirdness at the end of the loop.

Add new row after 4th album PHP

so I am trying to add a new row after every 4th gallery and continue on until I am out of galleries to add. So if there is 17 galleries there will be 4 rows of 4 galleries and 1 row of the remaining gallery. here is an example of how it looks: http://www.csulb.edu/centers/latinohealth/media/galleries/
here is my code:
<?php $this->start_element('nextgen_gallery.gallery_container', 'container', $displayed_gallery); ?>
<div class="row-fluid secondone">
<div class="ngg-albumoverview span12">
<div class="row-fluid">
<?php $count = 0;?>
<?php foreach ($galleries as $gallery) {
$count++;
?>
<div class="ngg-album span3">
<div class="ngg-albumtitle">
<?php echo_safe_html($gallery->title); ?>
</div>
<div class="ngg-albumcontent">
<div class="ngg-thumbnail">
<a class="gallery_link" href="<?php echo nextgen_esc_url($gallery->pagelink); ?>"><img class="Thumb" alt="<?php echo esc_attr($gallery->title); ?>" src="<?php echo nextgen_esc_url($gallery->previewurl); ?>"/></a>
</div>
<div class="ngg-description">
<p><?php echo_safe_html($gallery->galdesc); ?></p>
<?php if (isset($gallery->counter) && $gallery->counter > 0) { ?>
<p><strong><?php echo $gallery->counter; ?></strong> <?php _e('Photos', 'nggallery'); ?></p>
<?php } ?>
</div>
</div>
</div>
<?php if ($count % 4 == 0 ) ?>
</div>
<div class="row-fluid">
<?php } ?>
</div>
</div>
</div>
<?php $this->end_element(); ?>
Found the problem:
the row:
<?php if ($count % 4 == 0 ) ?>
should be:
<?php if ($count % 4 == 0 ) { ?>
You need to do what you want with css styles, not with the php.
Create a container block with fixed width that can contain exacly 4 galleries and use the float property on the boxes of the galleries.

Setting an interval on do / while from 5 to 5

I've made a repeat region with dreamweaver and the result is this:
<div>
<?php do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
No problem here, it works fine.
The thing is, I'm trying to use only 5 slides for each page, something like this:
<div>
<div class="page">
<?php do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php
$i = 0;
$r = $i % 5;
$i++;
if ( $r == 0 ) {
?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<? } ?>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
It's obviously not working, but hopefully you'll see what I'm trying to reach.
Thanks in advance!
UPDATE:
The result i get is:
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
I would like it to be:
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
You're setting $i to 0 on every iteration. Place the $i = 0 before the do-while loop.
You should probably also place $i++ before $r = $i % 5; to get the result you require.
EDIT: You're also missing a closing brace before your while statement (typo?)
Hence, the complete code:
<div>
<div class="page">
<?php
$i = 0;
do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php
$i++;
$r = $i % 5;
if ( $r == 0 ) {
?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<?php
}
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
the piece you gave is not a valid php code.
the line here:
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
opens a php tag without closing it before the next one you open.. so a first step would be to change that line to
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
also you missed closing a block, here:
<? } ?>
you should close both the if and the do/while block (so another })
another problem is the fact that on every step you change $i to 0, so every time $r will be 0 also
so in conclusion: take your problems step by step as you have more than one
<div>
<div class="page">
<?php for ($i = 0; $row_Recordset1 = mysql_fetch_assoc($Recordset1); $i++) { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php if ( $i % 5 == 0 && $i ) { ?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<?php } ?>
<?php } ?>
</div>
</div>
Something like this would loop through 5 times adding a new <div>content</div> to the variable $html. You can then echo the variable in your html body.
$html = "";
for ($i=0;$i<5;$i++) {
$html .= "<div class=\"page\">". $content[$i] . "</div>\n";
}
Additions:
<?PHP
while ($row_Recordset = mysql_fetch_assoc($Recordset1)) {
$html .= "<div class='page'>\n";
for ($i=0;$i<5;$i++) {
$html .= "<div class='slide'>". $row_Recordset[$i]['column'] . "</div>\n";
}
$html .= "</div>\n";
}
?>
This code will loop through the recordset populating $html on each iteration. Try echo $html and you will have an output structured as you want.

Categories