How can fetch data with swap column - php

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;

Related

How do I show just the first image from mysql database in php

I have the following code. How do I show the first image in the database with index of 0 for the large image display at end of the code? Right now it is showing the last image in the database.
<div id="imgWheel" class="treatmentContainer">
<?php
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $src ?>" />
</div>
Your result set is alreadyx ordered by id, so you need only a variable, to be filled once with the first imageurl
<div id="imgWheel" class="treatmentContainer">
<?php
$bigpictureurl = "";
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
if (empty($bigpictureurl)) {
$bigpictureurl = $src ;
}
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $bigpictureurl ?>" />
</div>
You just need to update your SQL query, just add LIMIT 1. This will limit the result just to 1 record and as you have ORDER id ASC, it will show the first record of the given user (as per you it is 0).
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id ASC LIMIT 1;";
A quick and dirty solution is to save your first image in some separate variables, for example like this:
$isFirst = true;
$firstImageSrc = "";
$result = ....;
while (...) {
// set your $product, $room etc here
if ($isFirst) {
$isFirst = false;
$firstImageSrc = $src;
}
}
echo ...
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $firstImageSrc ?>" />
</div>
A much more elegant solution would be to create an array with all your images, so that you can separate your php from your html. I will refactor your code below, and fix your first image problem as well:
<?php
$images = [];
$idx = 0;
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$images[$idx]["product"] = $row["product"];
$images[$idx]["room"] = $row["room"];
$images[$idx]["style"] = $row["style"];
$images[$idx]["tags"] = $row["tags"];
$images[$idx]["src"] = $row["url"];
$images[$idx]["dataid"] = $row["id"];
$images[$idx]["imgClass"] = "";
if (in_array($src, $favourites)) {
$images[$idx]["imgClass"] = " favourite";
}
$idx++;
}
?>
<div id="imgWheel" class="treatmentContainer">
<?php foreach ($images as $image) { ?>
<div class='treatment<?=$image["imgClass"]?>' data-url='<?=$image["src"]?>' data-product='<?=$image["product"]?>' data-room='<?=$image["room"]?>' data-style='<?=$image["style"]?>' data-tags='<?=$image["tags"]?>' data-number='<?=$image["dataid"]?>' id='pic_<?=$image["dataid"]?>' >
<img src='<?=$image["src"]?>' crossorigin='anonymous'/>
</div>
<?php } ?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?=$images[0]["src"]?>" />
</div>
Since you have all of that in your WHILE statement, I assume you want to echo all those records. And then at the end show the 1st pic. So for the "Large Image Display," give this a try:
<div id="display">
$query = "SELECT * FROM images WHERE user = 0;";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC)
$src = $row["url"];
<img id="mainImage" src="<?php echo $src ?>" />
</div>
If you'd like less code, then save the value of $src inside your WHILE loop when user=0 into some other variable like $src2. And then your code simply becomes:
<img id="mainImage" src="<?php echo $src2 ?>" />

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

PHP insert <div> and line shift after every 10th DB element in foreach loop

Overall I would like to echo all elements of the DB table 'products' as buttons on a row, and for each 10th element, a lineshift should be implemented. Each line should be surrounded by a specific ().
I have printed the code I have been working with to do this:
products_backend.php:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
$i++;
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if ($i%10 == 0):
echo "<br>";
endif;
}
}
And then I have the "products.php page", where the extracted data is shown:
<div class="container-fluid">
<section class="wrapper">
<div class="row">
<div class="row-eq-height">
<?php require("products_backend.php"); ?>
</div>
</div>
</section>
</div>
But the output of this is all the products in the DB printed on one line, without any lineshift for every 10th line:
The image shows the output, however due to the window size, only 11 products are shown, but i keeps going outside the window.
To summarize, I want the output to insert a lineshift for every 10th product printed, and that each row should be inside the div, like in the manipulated Picture below: (Notice, that in order to create the Picture, I have just copied the output from the row line in order to create the second row, Thus I do not want the same output in the second row as in the first row)
So what am I doing wrong in my code above?
the class "col-md-1" looks like a floating div
so to break this floating divs, try this :
echo "<br style=\"clear : both;\">";
You can use % moduler to add div for after every 10 record;
Updated code :
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
if(($i%10) == 0){ echo "<div class="clearfix"></div>";}
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
$i++;
}
}
Try this:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 1;
echo "<div>";
foreach($table_row as $data) {
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if(($i%10) == 0){ echo "</div><div>";}
$i++;
}
echo "</div>";
}

PHP mysqli empty array

I have a question about my code. When i try to fill my html with my fetched array i only get blank spaces instead of the actual results. Can anyone tell me why ?
<?php
require_once("php/loader.inc.php");
include("php/header.php");
include("php/userpanel.php");
$gebruiker = $_SESSION['user'];
?>
<div class="large-8 columns">
<div class="row">
<?php
$query = "
SELECT
items.naam,
items.prijs,
items.image_url
FROM inventory
JOIN items
ON items.id = inventory.item_id
WHERE
gebruiker_id = ?
";
$stmt = $db->prepare($query);
$stmt-> bind_param('i', $gebruiker->id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
$user_items[] = $row;
echo '<div class="large-3 small-4 columns">
<img src="'. $user_items['image_url'] .'">
<div class="panel">
<h6>'. $user_items['naam'] .'</h6>
<h6 class="subheader">'. $user_items['prijs'] .'</h6>
</div>
</div>';
}
?>
By the way, when i try print_r($user_items); it gives me the expected result.
Thanks in advance!
I think you want to change this:
$user_items[] = $row;
to this:
$user_items = $row;
Otherwise your indexes are 1 dimension 'deeper'
As an example:
$row = array("image_url" => "xy");
after this line:
$user_items[] = $row;
You can't access it with:
echo $user_items["image_url"]; //Wrong
you would have to do this:
echo $user_items[0]["image_url"]; //works
You don't need this line $user_items[] = $row; you can use $row directly
while ($row = $result->fetch_assoc())
{
echo '<div class="large-3 small-4 columns">
<img src="'. $row['image_url'] .'">
<div class="panel">
<h6>'. $row['naam'] .'</h6>
<h6 class="subheader">'. $row['prijs'] .'</h6>
</div>
</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