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.
Related
I have an array of objects in which I want to iterate them in a foreach loop. However I want to only allow for 3 of them to be in a row and then after the 3rd, a new row to start, and so on.
function displayIncomeDetails($deets) {
$result = NULL;
if($deets != NULL) {
foreach($deets as $deet) {
$result .= '
<div class="row mt-4">
<div class="col-sm-3">
'.$deet.'
</div>
</div>
';
}
} else {
$result = '
<div class="row">
<p style="margin: 0 auto;" class="mt-4 text-danger">No Details Found</p>
</div>
';
}
return $result;
}
So instead of 1 column in the row I would like 3
array_chunk is very usable here, e.g.:
foreach (array_chunk($deets, 3) as $perRow) {
$result .= '<div class="row mt-4">';
foreach ($perRow as $deet) {
$result .= '<div class="col-sm-3">' . $deet . '</div>';
}
$result .= '</div>';
}
I'm trying to run a foreach loop inside $output = ''; and later echo $output;.
I can print any other variable like this '.$row["name"].' inside $output = ''; but can do a foreach loop.
if(isset($_POST["id"]))
{
$output = '';
$query = mysqli_query($databaseLink, "SELECT * FROM test WHERE test_id = '".$_POST["id"]."'");
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center" > Practice Vertical </h4>
'$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value) {
<br>
print $value;
<br> }'
';
}
echo $output;
}
The thing is I cant forech loop inside $output .= ' '; , it does not work.
Okay here is the thing, this code here
$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value) {
<br>
print $value;
<br>
}
runs just fine if I run it outside $output .= ' ';.
Any php code that goes inside $output .= ' ' should be wrapped inside another '. .' or ' ' else it becomes a simple text. so if i want to print a variable i have to do it like this '.$count.'. But I cant use a loop.
You didn't close your <div>s properly, nor did you assign the output to $output properly. I fixed the positions of the single apostrophes ' so they make sense.
Here is the corrected code:
if(isset($_POST["test_id"]))
{
$output = '';
$query = mysqli_query($databaseLink, "SELECT * FROM test WHERE test_id = '".$_POST["test_id"]."'");
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center"> Practice Vertical </h4>';
$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value)
{
$output .= '<br>'.$value.'<br>';
}
$output .= '</div>
</div>';
}
echo $output;
}
A simple approach:
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center" > Practice Vertical </h4>';
// Add every id to your output explicictly
$list_id = explode(",", $row["pet_id"]);
foreach($list_id as $value) {
$output .= '<br>' . $value . '<br>';
}
$output .= '</div>';
}
Try the following code:
<?php
if(isset($_POST["id"]))
{
$output = '';
$query = mysqli_query($databaseLink, "SELECT * FROM test WHERE test_id = '".$_POST["id"]."'");
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center" > Practice Vertical </h4>';
$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value) {
$output = $output."<br>".$value."<br>";
}
$output .= '</div>';
}
echo $output;
}
?>
I have a nested div in which I want to fetch data from my database. I have explained what I want below:
<div class="row-fluid">
<div class="span6">
**First Entry Here**
</div>
<div class="span6">
**Second Entry Here**
</div>
</div>
<div class="row-fluid">
<div class="span6">
**Third Entry Here**
</div>
<div class="span6">
**FourthEntry Here**
</div>
</div>
and so on...
I am literally confused right now and the solution is not clicking in my mind. I have this code so far:
$sql = "SELECT * FROM `users`";
$result = mysqli_query($conn, $sql);
$ic = 0;
while ($row = mysqli_fetch_assoc($result)) {
if(($ic%2) == 0) {
$div1 = '<div class="row-fluid">';
$div2 = '</div>';
}else{
$div1 = '';
$div2 = '';
}
?>
<?=$div1;?>
<div class="span6">
<?=$row['userid'];?>
</div>
<?=$div2;?>
<?php
$ic = $ic + 1;
}
?>
I have tried two while loops but it was outputting around 5000 lines of code.
$ic = 0;
$temp = "";
while ($row = mysqli_fetch_assoc($result)) {
$temp .= "<div class='span6'>".$row['user_id']."</div>";
if ( $ic % 2 != 0 ){
echo "<div class='row-fluid'>".$temp."<div>";
$temp = "";
}
$ic++;
}
// in case the number of records are odd::
if ($temp != "" )
echo "<div class='row-fluid'>".$temp."<div>";
#Amr Magdy logic is correct, however the solution didn't work out for me. I have now managed to fix it and if anyone else has the same situation may refer to the code below:
<?php
$sql = "SELECT * FROM `users`";
$result = mysqli_query($conn, $sql);
$ic = 0;
$temp = "";
while ($row = mysqli_fetch_assoc($result)) {
$temp = '<div class="span6">'.$row['userid'].'</div>';
if(($ic%2) == 0) {
echo '<div class="row-fluid">'.$temp;
$temp = "";
}else{
echo $temp;
}
$ic = $ic + 1;
}
echo '</div>';
?>
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] => <img src="images/Huizen/huis-3.jpg" alt="" />
[1] => <img 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('<', '<', $house);
$houses = str_ireplace('>', '>', $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;
}
Before i was displaying zones records static although i have records in database table
HTML
<div class="pub_fot_sec_menu pub_fot_list_fst">
<h2>Kosi</h2>
<h2>Mechi</h2>
<h2>Sagarmatha</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_sec">
<h2>Bagmati</h2>
<h2>Janakpur</h2>
<h2>Narayani</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_thrd">
<h2>Dhawalagiri</h2>
<h2>Gandaki</h2>
<h2>Lumbini</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_frth">
<h2>Bheri</h2>
<h2>Karnali</h2>
<h2>Rapti</h2>
</div>
Now i want to fetch zone records using while or whatever method that work for me!
<?php
$sql="select * from tb_zone";
$res =mysql_query($sql);
while($data =mysql_fetch_array($res))
{
// want do display zone record in the above html output format
}
?>
Any help would be appreciated!
Try This
<?php
$class = array ('pub_fot_sec_menu pub_fot_list_fst','pub_fot_sec_menu pub_fot_list_sec','pub_fot_sec_menu pub_fot_list_thrd','pub_fot_sec_menu pub_fot_list_frth');
$sql="select * from tb_zone";
$res =mysql_query($sql);
$j=0;
$i=0;
while($data =mysql_fetch_array($res))
{
if($i==0)
{ echo '<div class="'.$class[$j].'">'; }
echo '<h2>'.$data["zone"].'</h2>';
if($i%2==0 && i > 0)
{ echo '</div>'; $j++;$i=0; }
else{ $i++; }
}
?>
just use this type
while($data =mysql_fetch_array($res))
{
echo
'
<div class="pub_fot_sec_menu pub_fot_list_fst">
<h2>'.$data['zone'].'</h2>
';
}