Dynamically building bootstrap row, 3 per row - php

I am not sure the best way to go about this, on a site i'm building (using bootstrap) i want to have 3 cards per row using the grid system like:
HTML:
<div class="container">
<div class="row">
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
</div>
</div>
Which is no problem normally, but i'm building it (or trying to) dynamically:
PHP:
<main>
<br /><br /><br />
<?php
$pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);
$mixed = shuffle($pages);
$count = 0;
echo '<div class="container"><div class="row">';
foreach($pages as $page)
{
$count++;
if (strpos($page, '.php') !== false && $page != 'index.php') {
$html = file_get_contents($page);
$code = explode("|", extractXvideos($html));
?>
<div class="col-md-4">
<div class="card" style="width: 18rem;">
<img src="<?= $code[3]; ?>" class="card-img-top" alt="<?= $code[0]; ?>">
<div class="card-body">
<p class="card-text"><?= substr($code[0], 0, 25); ?> ...</p>
</div>
</div>
</div>
<?php
if ($count == 18) {
// The reason only 15 is showing is because we ignore ".", ".." & "index.php".
break;
}
}
}
echo '</div></div>';
?>
</main>
For this project i'm scanning .php pages on the server, then trying to lay them out 3 per row, so after every row of 3 i need to start a new row echo '<div class="container"><div class="row">'; from what i can see, i do not know the best way to go about this, any help would be appreciated.

main
<?php
$pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);
$mixed = shuffle($pages);
$count = 0;
$output = '';
foreach($pages as $page) {
$count++;
if (strpos($page, '.php') !== false && $page != 'index.php') {
$html = file_get_contents($page);
$code = explode("|", extractXvideos($html));
$output .= '<div class="container">';
$output .= '<div class="row">';
$output .= '<div class="col">Column</div>';
$output .= '<div class="col">Column</div>';
$output .= '<div class="col">Column</div>';
$output .= '</div>
$output .= '</div>';
}
}
echo $output;
?>
main
try this template and apply your conditions.

Related

WP-JSON - Multi-endpoint - Re-occuring Foreach Error

I'm currently trying to build out a multi-endpoint system for WP-JSON between 4 websites, and most of the time it's fine, however sometimes when my page performs the process, it will throw back an error for the Foreach cycle, saying 'Warning: Invalid argument supplied for foreach() in ...'
I've tried alternative methods of merging the arrays before retrieving the body using wp_remote_retrieve_body but there's no such luck. I've looked up various google solutions on multiple-endpoints using php but nothing i've found seems to do an effective job
<?php
function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
/* BASE DETAILS FOR SEARCH*/
$b_url = get_site_url(); // Base URL
$posts_merged;
$args = array(
'sslverify' => false
);
if($_GET['search'] or !empty($_GET['search'])){
$filter_query = '?search='.$_GET['search'];
}else{
$filter_query = '';
}
$post_url_collective = array(
$b_url.'/wp-json/wp/v2/posts'.$filter_query, // Main Site
$b_url.'/db/wp-json/wp/v2/posts'.$filter_query, // DB Site
$b_url.'/dc/wp-json/wp/v2/posts'.$filter_query, // DC Site
$b_url.'/fw/wp-json/wp/v2/posts'.$filter_query // FW Site
);
$post_response = wp_remote_get($post_url_collective[0], $args);
$post_response = wp_remote_retrieve_body($post_response);
$post_response = json_decode($post_response);
$post_response_two = wp_remote_get($post_url_collective[1], $args);
$post_response_two = wp_remote_retrieve_body($post_response_two);
$post_response_two = json_decode($post_response_two);
$post_response_three = wp_remote_get($post_url_collective[2], $args);
$post_response_three = wp_remote_retrieve_body($post_response_three);
$post_response_three = json_decode($post_response_three);
$post_response_four = wp_remote_get($post_url_collective[3], $args);
$post_response_four = wp_remote_retrieve_body($post_response_four);
$post_response_four = json_decode($post_response_four);
$posts_merged = array_merge( $post_response, $post_response_two, $post_response_three, $post_response_four );
?>
<div class="container multi-search-wrap">
<div class="row" style="margin-bottom: 0px">
<div class="col sm12 m12 l12">
<form class="multi-search-form" method="GET">
<div class="row" style="margin-bottom: 0px">
<div class="col sm8 m9 l10">
<input type="text" name="search" value="<?php echo ( isset( $_GET['search'] ) ? $_GET['search'] : '' ); ?>" placeholder="Search for..."/>
</div>
<div class="col sm4 m3 l2">
<input type="submit" value="Search" />
</div>
</div>
</form>
</div>
</div>
<div class="row" style="background: #eeeeee;padding: 10px;font-size: 12px;margin:0px 0px 20px 0px">
<div class="col sm12 m12 l12">
We've found <strong><?php echo count($posts_merged);?></strong> results for <?php echo $_GET['search'];?>...
</div>
</div>
<div class="row">
<?php
// Post Loop
foreach($posts_merged as $get_post) {
$image_url = $b_url.'/wp-json/wp/v2/media/'.$get_post->featured_media;
$image_response = wp_remote_get($image_url, $args);
$image_response = json_decode($image_response['body']);
//echo'<pre>';print_r($image_response);echo'</pre>';
echo '<div class="col sm12 m6 l6 multi-search-post">';
echo '<div class="row">';
echo '<div class="col sm12 m3 l4 multi-search-post">';
if(!empty($image_response->media_details->sizes->thumbnail->source_url)){
echo '<img class="img_'.$get_post->id.'" src="'.$image_response->media_details->sizes->thumbnail->source_url.'" width="auto"><br>';
}else{
echo '<img class="img_'.$get_post->id.'" src="'.get_template_directory_uri().'/images/post-placeholder.jpg" width="auto"><br>';
}
echo '</div>';
echo '<div class="col sm12 m9 l8 multi-search-post">';
echo '<a href="'.$get_post->link.'">';
echo '<h3>'.$get_post->title->rendered.'</h3>';
echo '</a>';
echo '<p>'.limit_text($get_post->excerpt->rendered, 30).'</p>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
I would like to improve the load speed of the queries but the most important part is ridding this occassional Foreach error.
Any help would be greatly appreciated

Simple dom parser results in multiple pages

I have parsed a page to get the titles, and sometimes the parsed titles are more than 11. My designed template contain max 11 titles. My question is how to replicate the template () for the rest of the titles (from 12 -> n). I have to replicate the template somehow for the rest of results, but I don't know exactly how.
Bellow is my template, witch is displaying all the results.
<?php
include('parse/simple_html_dom.php');
$url = 'link-to-url';
$html = file_get_html($url);
$headlines = array();
$i = 0;
?>
<div class="pf w0 h0">
<div class="w0 h0">
<div class="header-pagina">
<svg role="img" class="header37"><use xlink:href="#header-hp"></use></svg>
</div>
<div class="page-wrapper">
<?php
foreach($html->find('.cmsmasters_row[1] .cmsmasters_toggle_title') as $title) {
$i++;
echo '<div class="agenda-curs">';
echo '<div class="agenda-tab"><span class="modul-tab-text color1" contenteditable="true">Modulul '.$i.'</span></div>';
echo '<div class="agenda-text" contenteditable="true">'.$headlines[] = $title->plaintext.'</div >';
echo '</div>';
}
$head = implode("", $headlines);
?>
</div>
<svg role="img" class="footer35"><use xlink:href="#footer35"></use></svg>
</div>
</div>
I don't know what you are using $head = implode("", $headlines); for in your script but you will be able to glean the data that you want from $headlines or headline_batch below.
Separating your processing from your printing will make your code easier to read and maintain. I could write the script to use less loops, but I am favoring readability over efficiency.
<?php
include('parse/simple_html_dom.php');
$url = 'link-to-url';
$html = file_get_html($url);
$headlines = array();
$counter = 0;
foreach($html->find('.cmsmasters_row[1] .cmsmasters_toggle_title') as $title) {
$headlines[++$counter] = $title->plaintext;
}
// no more processing, just printing
foreach (array_chunk($headlines, 10, true) as $headline_batch) {
?>
<div class="pf w0 h0">
<div class="w0 h0">
<div class="header-pagina">
<svg role="img" class="header37"><use xlink:href="#header-hp"></use></svg>
</div>
<?php
foreach ($headline_batch as $counter => $title) {
echo '<div class="agenda-curs">
<div class="agenda-tab"><span class="modul-tab-text color1" contenteditable="true">Modulul ' . $counter . '</span></div>
<div class="agenda-text" contenteditable="true">' . $title . '</div >
</div>';
}
?>
</div>
<svg role="img" class="footer35"><use xlink:href="#footer35"></use></svg>
</div>
</div>
<?php
}
?>
Using array_chunk($headlines, 10, true) will spare you having to use a modulus condition in your loop to check if a new group should be started. The true parameter retains the $counter values after splitting into groups.

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;
?>

Creating row using php loop

I want to create a row if columns greater than 3 using PHP loop because i am using wordpress
My code is here
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
If columns are greater than 3, then it should create a new row like this
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
<div class="row">
<div class="column">column1</div>
</div>
Any help will be highly appreciated.
Thanks in advance
Sure - just use modulus:
<?php
$elements = array('foo', 'bar', 'rab', 'oof');
echo '<div class="row">';
foreach ($elements as $i => $element) {
if ($i > 0 && $i % 3 == 0) {
echo '</div><div class="row">';
}
echo '<div class="column">' . $element . '</div>';
}
echo '</div>';
?>
DEMO
Output:
<div class="row">
<div class="column">foo</div>
<div class="column">bar</div>
<div class="column">rab</div>
</div>
<div class="row">
<div class="column">oof</div>
</div>
You need something like this:
<?
echo '<div class="row">';
for ($i=0; $i<15;$i++){
if ($i%3 == 0 && $i != 0){
echo '</div><div class="row">';
}
echo '<div class="column">column '.($i+1).'</div>';
}
echo '</div>';
?>
WORKING CODE
There is alternative solution with function.
<?php
// Managing the Code -- Functions: Handling a Variable Number of Parameters
// building a row of 10 <td> columns with a variable number of items
function preferencesRow()
{
// initialize $output
$output = '';
// use "func_get_args()" to collect all parameters into an array
$params = func_get_args();
// used to make sure 10 columns are filled
$maxCols = 10;
// loop through the parameters
foreach ($params as $item) {
$output .= "<td width='80px' align='center'>$item</td>\n";
$maxCols--;
}
// fill in the rest of the row with empty columns
for ($x = 0; $x < $maxCols; $x++) {
$output .= "<td width='80px'> </td>\n";
}
return $output;
}
// NOTE: you can use "." or "," with echo
echo '<h1>Preferences</h1><hr />' . PHP_EOL;
echo '<table border=1>' . PHP_EOL;
echo '<tr><th>Tarzan</th>';
echo preferencesRow('Africa', 'jungles', 'tantor', 'mangani', 'cabin in the woods',
'hunting', 'swinging in trees', 'Jane');
echo '</tr>';
echo '<tr><th>Jane</th>';
echo preferencesRow('London', 'parties', 'dancing', 'social events', 'lavish estates');
echo '</tr>';
echo '</table>' . PHP_EOL;

html output formatting php

I am trying to format some html output from my db using php and here's my problem:
How it should be formated:
...
<li>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-1</div>
<div class="some-class-1">ARRAY-ELEMENT-2</div>
<div class="some-class-1">ARRAY-ELEMENT-3</div>
<div class="some-class-2">ARRAY-ELEMENT-4</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-5</div>
<div class="some-class-1">ARRAY-ELEMENT-6</div>
<div class="some-class-1">ARRAY-ELEMENT-7</div>
<div class="some-class-2">ARRAY-ELEMENT-8</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-9</div>
<div class="some-class-1">ARRAY-ELEMENT-10</div>
<div class="some-class-1">ARRAY-ELEMENT-11</div>
<div class="some-class-2">ARRAY-ELEMENT-12</div>
</div>
</li>
<li>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-13</div>
<div class="some-class-1">ARRAY-ELEMENT-14</div>
<div class="some-class-1">ARRAY-ELEMENT-15</div>
<div class="some-class-2">ARRAY-ELEMENT-16</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-17</div>
<div class="some-class-1">ARRAY-ELEMENT-18</div>
<div class="some-class-1">ARRAY-ELEMENT-19</div>
<div class="some-class-2">ARRAY-ELEMENT-20</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-21</div>
<div class="some-class-1">ARRAY-ELEMENT-22</div>
<div class="some-class-1">ARRAY-ELEMENT-23</div>
<div class="some-class-2">ARRAY-ELEMENT-24</div>
</div>
</li>
... etc.
Data is held inside an array gathered from mysql db, so far I got to this:
$num_thumbs = 4; //Number od elemets in a row
$result = myQueryFunction("SELECT * FROM table_name ORDER BY Id ASC");
$num_rows = mysql_num_rows($result);
if (!empty($num_rows)) {
while ($row = mysql_fetch_array($result)) {
$thumbs_array[] = "<a href=\"" . $row[0] . "\"><img src=\"" . $row[1] . "\"";
}
$thumb_p = "<div>\n";
mysql_free_result($result);
$i = 1;
foreach ($thumbs_array as &$thumb_link) {
if ($i == $num_thumbs) {
$i = 1;
$thumb_p .= "<div class=\"some-class-2\">" . $thumb_link . "</div>";
$thumb_p .= "</div>\n<div class=\"row-wrapper\">\n";
} else {
$thumb_p .= "\t\n<div class=\"some-class-1\">" . $thumb_link . "</div>";
$i++;
}
}
$thumb_p .= "\n</div>\n";
}
print($thumb_p);
this covers only this part of code formatting:
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-13</div>
<div class="some-class-1">ARRAY-ELEMENT-14</div>
<div class="some-class-1">ARRAY-ELEMENT-15</div>
<div class="some-class-2">ARRAY-ELEMENT-16</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-17</div>
<div class="some-class-1">ARRAY-ELEMENT-18</div>
<div class="some-class-1">ARRAY-ELEMENT-19</div>
<div class="some-class-2">ARRAY-ELEMENT-20</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-21</div>
<div class="some-class-1">ARRAY-ELEMENT-22</div>
<div class="some-class-1">ARRAY-ELEMENT-23</div>
<div class="some-class-2">ARRAY-ELEMENT-24</div>
</div>
I've been thinking of using some variable $newList = null; that will be updated at each row, so I can check if it has changed the next one.
any ideas? Thanks in advance :)
It seems you're trying to put an li every three div. In this case, you can do this :
$thumb_p = "<li>\n<div class=\"row-wrapper\">\n";
mysql_free_result($result);
$i = 1;
$j = 0;
foreach ($thumbs_array as &$thumb_link) {
if ($i == $num_thumbs) {
$i = 1;
$thumb_p .= "<div class=\"some-class-2\">" . $thumb_link . "</div>";
$thumb_p .= "</div>\n";
if($j == 3)
{
$thumb_p .= "</li>\n<li>";
$j = 0;
}
$thumb_p .= "<div class=\"row-wrapper\">\n";
$j++;
} else {
$thumb_p .= "\t\n<div class=\"some-class-1\">" . $thumb_link . "</div>";
$i++;
}
}
$thumb_p .= "\n</div>\n</li>\n";
I can't actually try it but I think it's okay. A li is added every three row-wrapper you print.

Categories