php MySQL loop within nested divs - php

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

Related

Split mysql results to 5 bootstrap grid

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 ?

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;

While loop use variable 2 times

Hello guys I got a for loop cicle that prints me divs and information from SQL, I print the slider fields with the settings on the mysql like, Slider number, Field postion and so on, the problem is I have a Modal Bootstrap to be printed aswell but I cant print it inside the currently loop .
My question is, is there anyway to store a variable from a for cicle so it can be reutilized?
There is the code
$ID=$row['ID'];
$sql = "SELECT NUM_Slides as valmax FROM slider_settings,Paginas, slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID=$ID";
$sqlconnect =$connect->query($sql);
$sqlresult =$sqlconnect->fetch_assoc();
for ($k = 1 ; $k <= $sqlresult['valmax']; $k++){
echo "<div class='slider1'>";
$sql1 = "SELECT P$k as campos, tituloP$k as titulo FROM slider_settings, Paginas,slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID = $ID";
$sqlconnect1 =$connect->query($sql1);
$sqlresult1 =$sqlconnect1->fetch_assoc();
echo "<div class='titulo'>
<h2>$sqlresult1[titulo]</h2>
</div>";
for ($l = 1 ; $l <= $sqlresult1[campos]; $l++){
$campo = "SELECT Butao,Titulo,Texto FROM slider_config, Paginas, slider_settings where slider_config.ID = $ID and Paginas.ID = $ID and slider_settings.ID =$ID and P_NUM = $k and Campo = $l";
$sqlconnect2 = $connect->query($campo);
$sqlresult2 = $sqlconnect2->fetch_assoc();
echo "<div class='part' id='part".$l."'>
<div id='imagem' class='button' data-toggle='modal' data-target='#myModal".$l."'>
<img src='data:image/png;base64," . base64_encode($sqlresult2['Butao']) . "'/>
</div>
<div id='titulo'>
<h4>$sqlresult2[Titulo]</h4>
</div>
<div id='texto'>
$sqlresult2[Texto]
</div>
</div>";
}
echo "</div>";
}
and There is the code that cant printed inside of the div or modal wont display
$modal = "SELECT Titulo_modal , Imagem_modal , Texto_modal FROM modal_settings , Paginas where modal_settings.ID = $ID and Paginas.ID= $ID and P_NUM_modal = $k and Campo_modal = $l";
$sqlconnect33 =$connect->query($modal);
$sqlresult33 =$sqlconnect33->fetch_assoc();
for ($n = 1 ; $n <= $sqlresult1[campos]; $n++){
echo "<div class='modal fade' id='myModal".$n."'>
<div class='modal-dialog modal-lg'>
<div class='modal-content'>
<div class='modal-header'>
<h4 class ='titulopopup'>$sqlresult33[Titulo_modal]</h4>
<button type='button' class='close' data-dismiss='modal'>
<span aria-hidden='true'>×</span></button>
</div>
<div class='modal-body'>
<div class='imagem'>
<img src='data:image/png;base64," . base64_encode($sqlresult33['Imagem_modal']) . "'/>
</div>
<div class='texto'>
$sqlresult33[Texto_modal]
</div>
</div>
</div>
</div>
</div>";
}
NOTE : the $ID is comming from another file :) and its work fine the first half of the code
maybe create array like that
$array = [];
for ($n = 1 ; $n <= $sqlresult1[campos]; $n++) {
$array[$k] = $i;
}
this way u can reuse your var stored in array in other for loop like
foreach ($array as $k => $i) {
// and u get all your var :)
}
with your code u can do somethings like
$ID=$row['ID'];
// here
$array = [];
//
$sql = "SELECT NUM_Slides as valmax FROM slider_settings,Paginas, slider_config where slider_settings.ID = $ID and Paginas.ID= $ID
and slider_config.ID=$ID";
$sqlconnect =$connect->query($sql);
$sqlresult =$sqlconnect->fetch_assoc();
for ($k = 1 ; $k <= $sqlresult['valmax']; $k++){
echo "<div class='slider1'>";
$sql1 = "SELECT P$k as campos, tituloP$k as titulo FROM slider_settings, Paginas,slider_config where slider_settings.ID = $ID and Paginas.ID= $ID and slider_config.ID = $ID";
$sqlconnect1 =$connect->query($sql1);
$sqlresult1 =$sqlconnect1->fetch_assoc();
echo "<div class='titulo'>
<h2>$sqlresult1[titulo]</h2>
</div>";
for ($l = 1 ; $l <= $sqlresult1[campos]; $l++){
// here
$array[$k] = $l;
$campo = "SELECT Butao,Titulo,Texto FROM slider_config, Paginas, slider_settings where slider_config.ID = $ID and Paginas.ID = $ID and slider_settings.ID =$ID and P_NUM = $k and Campo = $l";
$sqlconnect2 = $connect->query($campo);
$sqlresult2 = $sqlconnect2->fetch_assoc();
echo "
<div class='part' id='part".$l."'>
<div id='imagem' class='button' data-toggle='modal' data-target='#myModal".$l."'>
<img src='data:image/png;base64," . base64_encode($sqlresult2['Butao']) . "'/>
</div>
<div id='titulo'>
<h4>$sqlresult2[Titulo]</h4>
</div>
<div id='texto'>
$sqlresult2[Texto]
</div>
</div>";
}
echo "</div>";
}

Delete first post and show second in PHP

I have a question in PHP.
I am creating a website with posts but I can't make the PHP to show just the second post (without show the first).
My code is like this:
<?php
$result = mysqli_query($dbc, "SELECT * FROM projects");
$x = 1;
while($row = mysqli_fetch_array($result)){
$nome = $row['name'];
$conteudo = $row['description'];
$imagem = $row['image'];
$imagem2 = $row['image2'];
?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<div class="content justify" id="projects-<?php echo $x; ?>" >
<?php echo $conteudo; ?>
<?php if(!empty($imagem2)) { ?>
<img class="hide-for-small" src="images/contebt/project/<?php echo $image2; ?>">
<?php }; ?>
</div>
<?php $count2++; } ?>
<?php $x++;}; ?>
With this code I can show just the first post, but I want to show just the second. Can anybody help me, please? Thanks!
This should work with this code :
PHP
<?php
$result = mysqli_query($dbc, "SELECT * FROM projects");
$x = 0;
while($row = mysqli_fetch_array($result)) {
$nome = $row['name'];
$conteudo = $row['description'];
$imagem = $row['image'];
$imagem2 = $row['image2'];
if (!$x) {
$x = 1;
continue;
}
?>
<div class="content justify" id="projects-<?php echo $x; ?>" >
<?php echo $conteudo; ?>
<?php if(!empty($imagem2)) { ?>
<img class="hide-for-small" src="images/contebt/project/<?php echo $image2; ?>">
<?php } ?>
</div>
<?php
$x++;
}
?>
SQL
But you should directly espace the first row directly in mysql usign either limit :
$result = mysqli_query($dbc, "SELECT * FROM projects LIMIT 1, 1");
or where statement :
$result = mysqli_query($dbc, "SELECT * FROM projects where id > 1");

How to get individuals all record in php from mysqli

How can i get all records which are stored in the mysqli database against certain email id.
Like I want to get all the records of question field which are stored against id#check.com, to get that what i have done so far is mentioned below. but my for loop codes prints only www.google.com. how can i get other two field?
<?php
$email = $_SESSION["email"];
$prevq = "Select * FROM `askadoc` WHERE `email` = '$email'";
$result = mysqli_query($conn, $prevq);
$prevq = "";
$location = "";
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$prevq = $row["question"];
$date = $row["date"];
}
}
?>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
for($i=0; $i<=count($email); $i++){
echo $date;
echo "<br>";
echo $prevq;
echo "<br>";
}
?>
</div>
</div>
Try this it will work:
<?php
$email = $_SESSION["email"];
$prevq = "Select * FROM `askadoc` WHERE `email` = '$email'";
$result = mysqli_query($conn, $prevq);
$prevq = "";
$location = "";
if (mysqli_num_rows($result) > 0):
?>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
while ($row = mysqli_fetch_assoc($result)) :
$prevq = $row["question"];
$date = $row["date"];
echo $date;
echo "<br>";
echo $prevq;
echo "<br>";
endwhile;
?>
</div>
</div>
<?php
endif;
?>
Just write your while loop inside your div
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $prevq = $row["question"];
echo $date = $row["date"];
}
}
?>
</div>
</div>

Categories