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;
}
?>
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>';
}
Please check out php code given below.
<?php
$con = mysqli_connect('localhost', 'root', '', 'unityaccess');
if (mysqli_connect_errno()) {
echo "connection failed";
exit();
}
$query = "SELECT * FROM players ORDER BY id DESC";
$row_result = mysqli_query($con, $query);
while($record = mysqli_fetch_assoc($row_result)){
$card = '<div class="card">';
$card .= '<div class="card-body">';
$card .= '<div class="comment-header">';
$card .= '<h6 class="card-subtitle mb-2 comment-name">'.$record['name'].' </h6>';
$card .= '</div>';
$card .= '</div>';
$card .= '</div>';
}
?>
I want to loop all name values inside html cards. according to loop there have 7 all name values. when I echo them it will loop correctly. but when I concatenate & echo $card like follows.
<div class="row">
<div class="col-md-12 comment-section">
<h4>Comments</h4>
<?php
echo $card;
?>
</div>
</div>
Then I can only show first value of name attribute. Why I can't retrieve all the name values with card. I think code looks not bad anyway.
change
$card = '<div class="card">';
with
$card .= '<div class="card">';
*It is a good practice to initialize your variables, if you don't the PHP engine will do a type cast depending on variable usage.
You are overwriting the $card variable each time round the loop, so all you are left with after the loop is the last card.
$row_result = mysqli_query($con, $query);
$card = ''; // init the card variable, so you can use `.=` from here on
while($record = mysqli_fetch_assoc($row_result)){
//$card = '<div class="card">'; this was the offending line
$card .= '<div class="card">';
$card .= ' <div class="card-body">';
$card .= ' <div class="comment-header">';
$card .= ' <h6 class="card-subtitle mb-2 comment-name">'.$record['name'].' </h6>';
$card .= ' </div>';
$card .= ' </div>';
$card .= '</div>';
}
I am looking a way to show my mysql results to a 5 div with same numbers of rows.
Here what i did:
$query_1 = $mysqli->query("SELECT * FROM tables ORDER BY id ASC");
$all_cnt = array();
$i = 0;
while($rows = mysqli_fetch_assoc($query_1)) {
$all_cnt[$i] = $rows["name"];
$i++;
}
$all_cnt = array_chunk($all_cnt, 5);
$output = "";
foreach($all_cnt as $cnt) {
$output .= '
<div class="col-md-2 ">
<div class="row content">
<ul class="list-unstyled">
';
foreach ($cnt as $t) {
$output .= '<li>'.$t.'</li>';
}
$output .= '
</ul>
</div>
</div>
';
}
But i get not ordered results and not same number on each grid !
Any other solution please ?
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.
Currently I am using a file with css and all.I have a problem as i am using php output between many html tags. The menu consist of the hard coded pages which is included with the php require_once. However, i also want to add in some pages from my database.
The Problem: The menu only shows 1 page name from my database when i have 2. It is displaying the 2nd page's name i have made. However clicking on it, it calls up the content from the 1st page.
The page name from my database is added using php output after the <ul id="menu-toc">
The page's content from my database is added at the bottom of the "The Page" section.
update: I've changed the placing of the while loop before the to after the
Pattern of css
<div class="bb-item" id="abscess">
<div class="content">
<div class="scroller">
Content
</div>
</div>
</div>
The Page
function find_condition() {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE visible = 1 ";
$query .= "AND subject_id = 2 ";
$query .= "ORDER BY position ASC";
$page_set = mysqli_query($connection, $query);
confirm_query($page_set);
return $page_set;
}
$two = find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
}
echo $output;
?>
</ul>
</div>
<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
<?php require_once("abscess.php"); ?>
<?php require_once("apthousulcer.php"); ?>
<?php require_once("bad breath.php"); ?>
<?php
$two= find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
$content = $page['content'];
$output .= "<div class=\"bb-item\" id=\"";
$output .= $pagearray;
$output .= "\">";
$output .= "<div class=\"content\">";
$output .= "<div class=\"scroller\">";
$output .= "<h2>";
$output .= $pagearray;
$output .= "</h2>";
$output .= $content;
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
}
echo $output;
?>
You're starting a new <ul> each time through the while loop. You should just do that once, before the loop. You're also reinitializing $output each time through the loop, but not printing it in the loop. So you're just printing the last assignment of output after the loop is done.
$two = find_condition();
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
echo $output;
}
?>