How do I get multiple images from a single string - php

I've looked around and haven't found a answer that did it for me. I don't want all the images from an HTML page. I just want all the images from a single string.
On a page, I'm using two different strings. I want all the images that are within the second image. I want to loop them into a carousel.
I've looked around a bit and this is what I got:
function GetImgString($plaatje){
preg_match_all('/<img[^>]+>/i',$plaatje, $result);
$house = $result[0];
$i = 1;
$output = '<div class="carousel-inner">';
foreach ( $house as $houses ) {
if ($i == 1) {
$output.= '<div class="item active">';
}else{
$output.= '<div class="item">';
}
$output.=
$house.'
<div class="container">
<div class="carousel-caption">
</div>
</div>
</div>
';
$i++;
}
$output .= '</div>';
return $house;}
This is the output:
Array
(
[0] => &ltimg src="images/Huizen/huis-3.jpg" alt="" />
[1] => &ltimg src="images/Huizen/huis-4.jpg" alt="" />
)
How do I solve this?

I assume, when you do the regexp, in your results, there are the 2 images.
You are start to building your output into a variable, calles $output, but you return with $house, what contain the 2 result from preg_match.
So you can try with this:
function GetImgString($plaatje) {
preg_match_all('/<img[^>]+>/i', $plaatje, $result);
$houses = $result[0];
$houses = str_ireplace('<', '&lt', $house);
$houses = str_ireplace('>', '&gt', $house);
$i = 1;
$output = '<div class="carousel-inner">';
foreach ($houses as $house) {
$class = ' class="item"';
if ($i === 1) {
$class = ' class="item active"';
}
$output.= '<div '.$class.'>';
$output.= $house . '
<div class="container">
<div class="carousel-caption"></div>
</div>
</div>' . "\n";
$i++;
}
$output .= '</div>';
return $output;
}

Related

Dynamically building bootstrap row, 3 per row

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.

change the class for the first row extend from mysql with PHP

I have a banner that used from bootstrap and, the first slider on this banner should have class='item active' the rest of the sliders should have class='item' I am getting my sliders from my database
so far that what I try to do.
<?php
$getBanner = $db->prepare("SELECT * FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
while ($b = $results->fetch_array()) {
$bannerImages = array($b['image']);
foreach ($bannerImages as $image) {
if ($image[0]) {
echo '<div class="item active">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
}
?>
still not working as expected
var_dump
array (size=1)
0 => string '06.jpg' (length=6)
array (size=1)
0 => string '03.jpg' (length=6)
I see that the var_dump is 0 for all items what did I do wrong here?
This is a fix to the old code:
<?php
$getBanner = $db->prepare("SELECT image FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
$is_first = true;
while ($b = $results->fetch_array()) {
if ($is_first) {
echo '<div class="item active">
<img src="../images/en_banner/' . $b[0] . '" alt="Koueider">
</div>';
$is_first = false;
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $b[0] . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
?>
Change the code like this:
<?php
$getBanner = $db->prepare("SELECT * FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
$rows = $results->fetch_all(MYSQLI_ASSOC);
$ind = 0;
foreach ($rows as $image) {
if ($ind == 0) {
echo '<div class="item active">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
$ind++;
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
?>

function and output foreach result with html

My problem is for output $items on div.link,all test is not good because the first test display result on body and second test dispay result in top body not in my specific div
First bad test:
function list($id) {
$query = sprintf("SELECT * FROM `social` WHERE `userid` = '%s'", $id);
$result = $this->db->query($query);
$table = array();
if($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$table[] = $row['username'];
}
echo '<div class="sidebar friends">';
echo '<div class="content">';
echo '<div class="head">Group</div>';
echo '<div class="inner">';
foreach($table as $items){
echo '<div class="link">';
echo $items;
echo'</div>';
}
echo'</div>';
echo'</div>';
echo'</div>';
} else {
return false;
}
}
better result is like this, but output is not in my div link because before i closing variable output:
function list($id) {
$query = sprintf("SELECT * FROM `social` WHERE `userid` = '%s'", $id);
$result = $this->db->query($query);
$table = array();
if($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$table[] = $row['username'];
}
$output = '<div class="sidebar friends">
<div class="content">
<div class="head">Group</div>
<div class="inner">
<div class="link">'; //i closing var but foreach result not displayed in div link
//how i can output result here in div link?
foreach($table as $items){
echo $items; //items need to be display...
}
$output .= '</div>
</div>
</div>
</div>
</div>';
return $output;
} else {
return false;
}
}
how i can using foreach for output result in my div.link?
here is how i need to display:
<div class="sidebar-container widget-welcome">
<div class="sidebar-content">
<div class="sidebar-header">Group</div>
<div class="sidebar-inner">
<div class="sidebar-link">
//foreach output items result here
</div>
</div>
</div>
</div>
</div>
It wont show up because you are ECHO'ing $item. The fix is to append the $item with the $output string you are returning from the function.
Please change your loop like below:
foreach($table as $items){
$output .= $items; //items need to be display...
}
Change your block of code as below ( place your link div inside loop and append closing div properly)
$output = '<div class="sidebar friends">
<div class="content">
<div class="head">Group</div>
<div class="inner">';
foreach($table as $items){
$output .= '<div class="link">'.$items.'</div>';
}
$output .='</div></div></div>';
ON a different note, I'd recommend simply moving the html to a different file and then including it, Passing $table as a parameter and looping there. increases readability quite a bit.

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 in foreach loop

I have a problem with embeded html in foreach loop:
HTML:
<div class="head">
<div class="wins">
<div class="images">
<img src="images/1.jpg" alt="">
<img src="images/2.jpg" alt="">
<img src="images/3.jpg" alt="">
</div>
<div class="Bar">
<span>1</span>Cat1
<span>2</span>Cat2
<span>3</span>Cat3
</div>
</div>
I want to print html with this function :
function Bar($array){
$box .= '<div class="images">';
foreach($array as $key => $value){
$box .= ' <img src="'.$value['image'].'" alt="">';
}
$box .= '</div>';
return $box;
}
I want to break the loop after <div class="images"> and continue loop after the <div class="Bar"> . But Im confusing about this issue. Please show me the right way.
Thanks in advance
Two options:
Run through the loop twice.
or
Store each section in a separate variable and merge them later on, i.e.
function Bar($array){
foreach($array as $key => $value){
$images .= ' <img src="'.$value['image'].'" alt="">';
$bar .= ' <span>....';
}
$box .= '<div class="images">';
$box .= $images;
$box .= '</div>';
$box .= '<div class="Bar">';
$box .= $bar;
$box .= '</div>';
return $box;
}
function Bar($array){
$divImage=$divBar=array();
foreach($array as $key => $value){
$divImage[]= " <a href='#' ><img src='{$value['image']}' alt=''></a>";
$divBar[]= " <a href='#' rel='$key' ><span>$key</span>Cat$key</a>";
}
$divImage="<div class='images'>".implode("\r\n",$divImage)."</div>";
$divBar="<div class='bar'>".implode("\r\n",$divBar)."</div>";
$box="<div class='wins'>
$divImage
$divbar
</div>";
return $box;
}
maybe something like that?
$divimg = '<div class="images">';
$divbar = '<div class="Bar">';
foreach(...) {
$divimg .= ' ... ';
$divbar .= ' ... ';
}
$divimg .= '</div>';
$divbar .= '</div>';
$divimg .= $divbar;
return $divimg;

Categories