While loop use variable 2 times - php

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

Related

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 and bootstrap

I have problem to display posts properly.
Im displaying content dinamicaly with a function.
Function in php is
function displayAllPosts () {
global $connection;
$query = "SELECT * FROM blog_post ";
$run_query = mysqli_query($connection,$query);
while ($row = mysqli_fetch_array ($run_query)){
$post_title = $row ['post_title'];
$post_desc = $row ['post_desc'];
$post_img = $row ['post_img'];
echo "
<div class='col-md-3 col-sm-6'>
<div class='feature-center text-center animate-box' data-animate-effect='fadeIn'>
<img class='image-responsive' width='100%' src='$post_img' / ><hr>
<h2>{$post_title}</h2><hr>
<p>{$post_desc} </p>
<a href='#'><button type='button' class='btn btn-info' style='width:100%'>Categoty Title</button></a>
</div>
</div>
";
}
}
and like that page looks like this:
page
on smaler devices it will display 2 columns in a row. I was wandering is it posible to insert
<div class='clearfix'></div>
after 2 displayed columns?
i found solution :
$count=0;
while ($row = mysqli_fetch_array ($run_query)){
$post_title = $row ['post_title'];
$post_desc = $row ['post_desc'];
$post_img = $row ['post_img'];
echo "
<div class='col-md-3 col-sm-6'>
<div class='feature-center text-center animate-box' data-animate-effect='fadeIn'>
<img class='image-responsive' width='100%' src='$post_img' / ><hr>
<h2>{$post_title}</h2><hr>
<p>{$post_desc} </p>
<a href='#'><button type='button' class='btn btn-info' style='width:100%'>Categoty Title</button></a>
</div>
</div>
";
$count++;
switch ($count){
case ($count % 4 == 0) ;
echo "<div class='clearfix visible-lg-block visible-md-block'></div>";
break ;
case ($count % 2 == 0 && $count % 4 !==0 ) ;
echo "<div class='clearfix visible-sm-block'></div>";
break ;
}
}

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

Having Trouble Outputting Data correctly From SQL Table

I am trying to outputting data from a SQL table
Table cols are:
sheduleID, userID, empID, timeSlot, WeekSlot, daySlot
Connecting to DB
$schedQ = "SELECT * FROM seo_schedule WHERE empID=1 AND weekSlot=1";
$Em1Wk1Res = mysql_query($schedQ) or die(mysql_error());
Displaying Data
echo "<div class='week1'>";
while ($Em1WkRow = mysql_fetch_array($Em1Wk1Res)) {
$clientQ = "SELECT * FROM clients WHERE userID=".$Em1WkRow["userID"]."";
$clientRes = mysql_query($clientQ) or die(mysql_error());
$clientRow = mysql_fetch_array($clientRes);
echo "<div class='day".$Em1WkRow["daySlot"]."'>";
if ($Em1WkRow["timeSlot"] == "am") {
echo "<span class='".$Em1WkRow["timeSlot"]."'>";
echo $clientRow["company"];
echo "</span>";
}
else if ($Em1WkRow["timeSlot"] == "pm") {
echo "<span class='".$Em1WkRow["timeSlot"]."'>";
echo $clientRow["company"];
echo "</span>";
}
echo "</div>";
}
echo "</div>";
Current Output
<div class="week1">
<div class="day1">
<span class="am">Company 1</span>
</div>
<div class="day1">
<span class="pm">Company 1</span>
</div>
<div class="day2">
<span class="am">Company 2</span>
</div>
<div class="day2">
<span class="pm">Company 2</span>
</div>
...etc fir rest of days in week 1
</div>
What I want to be displayed is:
<div class="week1">
<div class="day1">
<span class="am">Company 1</span>
<span class="pm">Company 1</span>
</div>
<div class="day2">
<span class="am">Company 2</span>
<span class="pm">Company 2</span>
</div>
...etc fir rest of days in week 1
</div>
How do I go about doing this....?
You need a nested while to go through the days of the week rendering <span />s inside the week container. I'm not a php dev so can't help you with the implementation, sorry.
The question is do these events depend on a specific time or are you just trying to throw them in 1 company event per day AM and PM? In which case, you can just do:
$arr = array(0 => "am", 1 => "pm");
echo "<div class='week1'>";
while($Em1WkRow = mysql_fetch_array($Em1Wk1Res))
{
$clientQ = "SELECT * FROM clients WHERE userID='" . $Em1WkRow["userID"] . "'";
$clientRes = mysql_query($clientQ) or die(mysql_error());
$i = 0;
$out .= "<div class='day" . $Em1WkRow["daySlot"] . "'>"; // start day
while($clientRow = mysql_fetch_array($clientRes) && $i < 2)
{
// add current day's event
$out .= "<span class='" . $arr[$i++] . "'>";
$out .= $clientRow["company"];
$out .= "</span";
}
$out .= "</div>" // end day
}
echo $out; // display all week's info
echo "</div>"; // end week
This is what I ended up doing...
function emp_schedule($empID) {
$weeks = array(1, 2, 3, 4);
foreach ($weeks as $i => $week) {
$schedQ = "SELECT * FROM seo_schedule WHERE empID=$empID AND weekSlot=$week";
$Em1WkRes = mysql_query($schedQ) or die(mysql_error());
echo "<div class='week'>";
echo "<div class='head'><span class='ts'>Wk ".$week."</span> <span>Monday</span> <span>Tuesday</span> <span>Wednesday</span> <span>Thursday</span> <span>Friday</span></div>";
echo "<div class='ts'><span><strong>AM</strong></span><span><strong>PM</strong></span></div>";
while ($Em1WkRow = mysql_fetch_array($Em1WkRes)) {
$clientQ = "SELECT * FROM clients WHERE userID='".$Em1WkRow["userID"]."'";
$clientRes = mysql_query($clientQ) or die(mysql_error());
while($clientRow = mysql_fetch_array($clientRes)) {
$schd = "<div class='".$Em1WkRow["timeSlot"]."'>";
$schd .= "<span class='client'>".$clientRow["company"]."</span>";
$schd .= "</div>";
$days = array(1, 2, 3, 4, 5);
foreach ($days as $i => $day) {
if ($Em1WkRow["daySlot"] == $day && $Em1WkRow["timeSlot"] == "am" ) {
echo "<div class='day ".$Em1WkRow["daySlot"]."'>";
echo $schd;
}
if ($Em1WkRow["daySlot"] == $day && $Em1WkRow["timeSlot"] == "pm" ) {
echo $schd;
echo "</div>";
}
}
}
}
echo "</div>";
}
}
Mess I Know but it works.

Categories