How to make my php slider work - php

My php slider is sort of working i have managed to get it to link to my database. However, i need it to loop through all the images within my slider and my code isn't work but i believe i have missed a loop query?!
THIS IS MY CODE WHERE MY SLIDER IS:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<img src="<?php print $row['image']?>"/>
<!-- <img src="images/nivo/arts.png" alt="the grand theatre and nothern ballet">
<img src="images/nivo/slider3.png" alt="leeds night light slider image">
<img src="images/nivo/slider2.png" alt="Leeds Trinity slider image">
<img src="images/nivo/slider4.png" alt="leeds art hotels">
<img src="images/nivo/slider1.png" alt="leeds art slider image"> -->
</div>
</div>
THIS IS MY CODE TO RUN MY SLIDER AT THE MOMENT:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$row = mysqli_fetch_array($result);
?>

you need a loop in your php:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$rows = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rows[] = array(
'image' => $row['image'];
)
}
?>
And in your HTML:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php
foreach($rows as $row) { ?>
<img src="<?php print $row['image']?>"/>
<?php
} ?>
</div>
</div>

<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
?>
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
// use a while here
<?php while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ ?>
<img src="<?php print $row['image']?>"/>
<?php } ?>
</div>
</div>

you can use foreach to loop through your data, like this
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php foreach ($row as $key => $item): ?>
<img src="<?php echo $item['image']?>"/>
<?php endforeach; ?>
</div>
</div>
hope it help..

Related

Fetch an image from Mysql in html with PHP

This is my code and I wanted to display an image from DB. I followed some videos and recommendations here on Stack Overflow, but still does not work.
Other values like description, title etc works, so the problem is not in the connection with the DB.
Thank you.
<?php
$sql= "SELECT * FROM guides WHERE id='$id'";
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
if ($num > 0) {
$row = mysqli_fetch_assoc($result)
?>
<h1 class="title"><?=$row['title']?></h1>
<?php echo' <img class="header" style="height:400px" src="data:image/jpeg;base64,'.base64_encode( stripslashes($row['photo']) ).'" style="width:100px;"></img>' ?>
<div class="row">
<div class="leftcolumn">
<h2><?=$row['date']?></h2>
<div class="card" >
<div class="clearfix">
<p class="text" ><?=$row['description']?></p>
</div>
</div>
</div>
</div>
<?php
}
?>
I think you should try and specify the folder containing the given image also. E.g src = “containing_folder/$row[‘photo’]” and see if that works. You can still add your functions to the $row[‘photo’], this was just an example.
mysqli_fetch_assoc($result) this returns an array and you need to loop through the array:
The modified code given below----
$sql= "SELECT * FROM guides WHERE id='$id'";
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
if ($num > 0) {
while($row = mysqli_fetch_assoc($result)){
?>
<h1 class="title"><?=$row['title']?></h1>
<?php echo' <img class="header" style="height:400px" src="data:image/jpeg;base64,'.base64_encode( stripslashes($row['photo']) ).'" style="width:100px;"></img>' ?>
<div class="row">
<div class="leftcolumn">
<h2><?=$row['date']?></h2>
<div class="card" >
<div class="clearfix">
<p class="text" ><?=$row['description']?></p>
</div>
</div>
</div>
</div>
<?php
}
}
?>

How i can display blob elements is same row with php?

I want to display images in same row,but i can display only under each other. I tried in css display: inline-block; but dont working. Any idea what i need to do? Thanks
The code for display images:
require_once 'dbConfig.php';
$result = $db->query("SELECT image FROM images ORDER BY uploaded DESC");
?>
<?php if($result->num_rows > 0){ ?>
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-3" >
<a class="lightbox">
<?php while($row = $result->fetch_assoc()){
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'" width="250", " height="250">';}?>
<?php }else{ ?>
<p class="status error">Image(s) not found...</p>
<?php } ?>
</a>
</div>
</div>
It's because you large col is only 3.
Inspect element result
if you are using these images for lightbox thumbnail I would suggest d-flex rather than column system....
Try this code it will work .. now it will loop your columns as well
require_once 'dbConfig.php';
$result = $db->query("SELECT image FROM images ORDER BY uploaded DESC");
?>
<?php if($result->num_rows > 0){ ?>
<div class="row">
<?php while($row = $result->fetch_assoc()){
<div class="col-sm-6 col-md-4 col-lg-3" >
<a class="lightbox">
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'" width="250", " height="250">';}?>
</a>
</div>
<?php }else{ ?>
<p class="status error">Image(s) not found...</p>
<?php } ?>
</div>

Displaying data from while loop into html code

I need tips or direction on how can I display data from mysql using echo. But I want to display it in html code. I want to display $row["title"] of first title in mysql instead title1 and $row["content"] of first content in mysql instead content1 and do that for all 3 divs. php code works fine I just can't figure out how to make that possible.
<div class="carousel-inner" style="background-size:cover;">
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title1</h2>
<p>content1</p>
</div>
</div>
<div class="item">
<img src="img/road2.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title2</h2>
<p>content2</p>
</div>
</div>
<div class="item">
<img src="img/road3.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title3</h2>
<p>content3</p>
</div>
</div>-->
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
} else {
echo "0 results";
}
?>
You're almost there. Just move the html into the echo of the while loop.
echo '<div class="carousel-inner" style="background-size:cover;">';
$counter = 1;
while($row = mysqli_fetch_assoc($query)) {
echo '
<div class="item ' . ($counter == 1 ? 'active' : '') . '">
<img src="img/road{$counter}.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>' . $row["title"] . '</h2>
<p>' . $row["content"] . '</p>
</div>
</div>';
$counter++;
}
echo '</div>';
The only issue is the image, realistically you'd save the image in the database with the title and content then use the same method but for this simple case lets just use a counter
please note that I change your entire code a little bit to make the desired results...
<div class="carousel-inner" style="background-size:cover;">
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) { ?>
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2><?php echo $row["title"]; ?></h2>
<p><?php echo $row["content"]; ?></p>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
?>
Also note that I'm repeating just the first image... You need an extra on planning to determine how to handle images in code and then update this one.

div inside while loop in php

The search results should display like this
But my results are stacking on top of each.
Here is my code :
<div class="container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thesis") or die(mysql_error());
$search = trim( $_POST['SearchKeywords']);
$query = " SELECT * FROM new_data WHERE Product_Title or Product_link LIKE'%$search%' ";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);
$count == 0;
if ($count == 0) {
echo "Sorry, Nothing Found !!";
}else{
while ($row = mysql_fetch_array($sql))
{
$img = $row ['Product_Img'];
$link = $row ['Product_link'];
$title = $row ['Product_Title'];
$price = $row ['Product_Price'];
?>
<div class="card">
<div class="front alert alert-success">
<?php echo "$title";
echo "<img src='$img' width='80px' height='100px'>";
echo "$price"; ?>
</div>
</div>
<?php
};
};
?>
</div> <!-- Container -->
Those div blocks are inside a container.
I added a bootstrap class in order for better a design.
You can use thumbnails with custom content
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="..." alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
I used a counter inside while loop.
Which will check, when there are already 4 blocks/ products in a single row then it will create a new row
<?php
if($productCount == 0)
{
echo "<div class='row'>"; }
}
$productCount++;
if($productCount==4)
{
echo "</div>" ;
$productCount = 0;
}
?>

Trouble with html link tag and image

Got some trouble when i tried to use an url to image.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$query = mysql_query($sql);
if ($query > 0){
?>
<div class="container">
<div class="description-plate">
<?php
while
($row = mysql_fetch_array($query)){
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<!--div class="caption-btm">
<p style="margin-left:6px; margin-top:175px;">Start Airing From:</p>
<h5 style="margin-left:10px;"><?php echo $start; ?></h5>
<p style="margin-left:6px;">Airing Schedule:</p>
<h5 style="margin-left:10px;"><?php echo $schedule; ?></h5>
</div-->
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
So when i tried to call the image using php, the tag only appear on the last image. What i'm trying to do is having the tag on every images. Would appreciate any help, thanks :)
Right now you are going through the loop (not sure why you are using while) and each time creating
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
What you want to do is build up an html string on each pass appending the next image tag, something more like
...
$myimages = '';
while // skipped details
$myimages .= ' <div class="thumbnail-fluid">
<a href=". $row['image'] . '>
<div id="og-plate">
<div><img src="admin/' . $row['image'] . '></div>'
. '</div>
</a>
</div>';
}
Its appear last image because ORDER BY id and the condition status = 'On Going' can return one image. Your html structure should be like this.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$result = mysql_query($sql);
$n = mysql_num_rows($result);
if ($n > 0) {
?>
<div class="container">
<div class="description-plate">
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
</div>
</a>
</div>
<?php } ?>
</div>
</div>
<?php } ?>

Categories