Split mysql results to 5 bootstrap grid - php

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 ?

Related

Put elements in a new bootstrap row after n number - foreach loop

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>';
}

foreach loop inside $output

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

How can fetch data with swap column

I already get all data but I don't know how to fetch data with switch column for ex 1st data in left img in right , 2 nd data in right img in left( display will be in like my html )
Here is My php
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
while ($row = mysql_fetch_array($result)){
$data = $row["DATA"];
$img = $row["IMG"];
Here is my HTML ( I want data to fetch like this )
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/ >
<div class="col-md-6">
<div style="height:150px;background-color: red"> $data</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: blue">$img</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: blue"> $img</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: red">$data</div>
</div>
Consider the four div as a combination of 2 div. Use a counter with mod operator to match first two div or 2nd two div you are dealing with.
$i = 0 ;
while ($row = mysql_fetch_array($result)){
if( $i++ % 2 == 0 ) {
$col1 = $row["DATA"];
$col2 = $row["IMG"];
$color1 = 'blue' ;
$color2 = 'red' ;
}
else {
$col1 = $row["IMG"];
$col2 = $row["IMG"];
$color1 = 'red' ;
$color2 = 'blue' ;
}
?>
<div class="col-md-6">
<div style="height:150px;background-color: <?php echo $color1;?>"><?php $col1;?></div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: <?php echo $color2;?>"><?php $col2;?></div>
</div>
<?
php
} //end of while
?>
Logic:
It will print two div at a time. when the loop begin $i is zero. 0 %2 will result in 0; so first data will be printed. after that $++ will will make it 1. when the next time loop comes 1%2 is != 0 so else part will be considerd.
Have you reviewed PHP syntax?
Rather than saving your file as .html, save it as .php. You can write HTML exactly the same, but have the added benefit of being able to use PHP as well. Then you can do this:
<?php
// You can start the file in PHP
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
while ($row = mysql_fetch_array($result)){
$data = $row["DATA"];
$img = $row["IMG"];
?>
<!-- Now we are in HTML, but still hop back into PHP to work with variables. -->
<?foreach ($row as mysql_fetch_array( $result ) ):?>
<? $data = $row['DATA'];
$img = $row['IMG'];
?>
<div>
<div> <? echo $data ?> </div>
<div> <? echo $img ?> </div>
</div>
<? endforeach ?>
If I was to write this, this is how I would do it.
Updated code to reflect your OP markup.
<?php
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
// define variable
$html = NULL;
while ($row = mysql_fetch_array($result)) {
$html .= ''
. '<div class="col-md-6">'
. '<div style="height:150px;background-color: red">' . $row['DATA'] . '</div>'
. '</div>'
. '<div class="col-md-6">'
. '<div style="height:150px;background-color: blue">' . $row['IMG'] . '</div>'
. '</div>';
}
echo $html;

php MySQL loop within nested divs

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

Generating a new id or number with while, do or for loop

first of all I apologize for my bad english skill
I have a dynamic slider for my ecommarce. but I have a problem with links titles. I need to generate numbers ord new ids from 1 to 9 and put them as images' numbers or new ids.
Here is my code.
$sqlslide= mysql_query("SELECT * FROM products ORDER BY date_add DESC LIMIT 9");
$count2 = mysql_num_rows($sqlslide);
if($count2 > 0){
while($row = mysql_fetch_array($sqlslid)){
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$source = "products/$id.jpg";
$img = '<img src="'.$source.'" class="photo" alt="temp">';
$slide1 .=' <div class="panel" title="'.$id.'">
<div class="wrapper">
'.$img.'
<div class="photo-meta-data">
'.$name.$id.' <br />
"'.$descriotion.'
</div>
</div>
</div>
';
$img2 = '<img src="'.$source.'" class="nav-thumb" alt="temp-thumb">';
$nav_thumb .='
<div>'.$img2.$id.'</div>
'
;
}
I need to replace '.$id.' in title and the link with new generated numbers or ids in
$slide1 .=' <div class="panel" title="'.$id.'">
'.$img2.'
so question is, how can I generate with while, do or for loop new numbers or ids for them and replace the '.$id.' with it?
thanx
$sqlslide= mysql_query("SELECT * FROM products ORDER BY date_add DESC LIMIT 9");
$count2 = mysql_num_rows($sqlslide);
if($count2 > 0){
$index=0;
while($row = mysql_fetch_array($sqlslid)){
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$source = "products/$id.jpg";
$img = '<img src="'.$source.'" class="photo" alt="temp">';
$slide1 .=' <div class="panel" title="'.$index.'">
<div class="wrapper">
'.$img.'
<div class="photo-meta-data">
'.$name.$id.' <br />
"'.$descriotion.'
</div>
</div>
</div>
';
$img2 = '<img src="'.$source.'" class="nav-thumb" alt="temp-thumb">';
$nav_thumb .='
<div>'.$img2.$id.'</div>
'
...
$index++;
} // end while
}
Edit 1:
The concept of generating incremented numbers:
$index=0;
while($index<9) // incremented numbers till 9
{
echo $index;
$index++;
}
i understand you right?
$no = 1;
while($row = mysql_fetch_array($sqlslid))
{
//do something.
$no++;
if($no > 9)
{
$no = 1;
}
}

Categories