This question already has answers here:
display div inside php using echo
(2 answers)
Closed 4 months ago.
I'm trying to use echo in this code
<?php
require 'connect.php';
$query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
while($fetch = mysqli_fetch_array($query)){
$id= $fetch['id'];
?>
<?php echo "
<li class='col-lg-4 col-md-6 col-dm-12'>
<div class='da-card box-shadow'>
<div class='da-card-photo'>
<img src=' echo $fetch['video_name']' alt=''>
<div class='da-overlay'>
<div class='da-social'>
<h4 class='mb-10 color-white pd-20'></h4>
<ul class='clearfix'>
<li>
<a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
</li>
<li>
<a href='#'><i class='fa fa-link'></i></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
"; ?>
<?php
}
?>
I'm getting this error Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in C:\xampp\htdocs\desk\gallery.php on line 572
Why this error?
.................................................................
You can escape the string and concatenate the variable in the place your hoping to have it like this:
<img src='". $fetch['video_name'] ."' alt=''>
<?php
require 'connect.php';
$query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
while($fetch = mysqli_fetch_array($query)){
$id= $fetch['id'];
?>
<?php echo "
<li class='col-lg-4 col-md-6 col-dm-12'>
<div class='da-card box-shadow'>
<div class='da-card-photo'>
<img src='". $fetch['video_name'] ."' alt=''>
<div class='da-overlay'>
<div class='da-social'>
<h4 class='mb-10 color-white pd-20'></h4>
<ul class='clearfix'>
<li>
<a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
</li>
<li>
<a href='#'><i class='fa fa-link'></i></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
"; ?>
<?php
}
?>
You can't use echo inside an echo or should I say string statement.
What you have to do is variable substitution like:
echo 'This is my variable '.$x.'.';
Or...
echo "This is my variable $x.";
In your code:
<?php
require 'connect.php';
$query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
while($fetch = mysqli_fetch_array($query)){
$id= $fetch['id'];
$video_name = $fetch['video_name'];
?>
<?php echo "
<li class='col-lg-4 col-md-6 col-dm-12'>
<div class='da-card box-shadow'>
<div class='da-card-photo'>
<img src='$video_name' alt=''>
<div class='da-overlay'>
<div class='da-social'>
<h4 class='mb-10 color-white pd-20'></h4>
<ul class='clearfix'>
<li>
<a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
</li>
<li>
<a href='#'><i class='fa fa-link'></i></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
"; ?>
<?php
}
?>
Also you can ditch the <?php ?>:
<?php
require 'connect.php';
$query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
while($fetch = mysqli_fetch_array($query)) {
$id = $fetch['id'];
$video_name = $fetch['video_name'];
echo "
<li class='col-lg-4 col-md-6 col-dm-12'>
<div class='da-card box-shadow'>
<div class='da-card-photo'>
<img src='$video_name' alt=''>
<div class='da-overlay'>
<div class='da-social'>
<h4 class='mb-10 color-white pd-20'></h4>
<ul class='clearfix'>
<li>
<a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
</li>
<li>
<a href='#'><i class='fa fa-link'></i></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
";
}
So the problem is that the cards are really stretching and I have tried everything to fix it and still haven't achieved the fix. Any way to fix this since I'm out of the clues? And what is the problem and where?
My container that holds cards
<div class="container mt-5">
<div class="row align-items-center">
<div class="col-sm-12 mx-auto">
<div class="card-group">
<?php
$companies = new Companies();
$companies->showAllCompanies();
?>
</div>
</div>
</div>
</div>
My PHP showAllCompanies code
public function showAllCompanies(){
$datas = $this->getAllCompanies();
foreach ($datas as $data){
$name = $data['name'];
$orgnr = $data['organization_number'];
$notes = empty($data['notes']) ? "No notes" : $data['notes'];
$id = $data['id'];
echo "
<div class='card me-2 d-block' style='width: 18rem;'>
<div class='card-body'>
<h4 class='card-title'>$name</h4>
<p class='card-text'><i class='bi bi-stickies'></i> $notes</p>
</div>
<ul class='list-group list-group-flush'>
<li class='list-group-item'>Stores Owned <span class='badge text-bg-primary'>XX</span></li>
<li class='list-group-item'>Organization Number <span
class='badge text-bg-primary'>$orgnr</span></li>
</ul>
<div class='card-body'>
<a href='#' class='btn btn-primary'>Edit</a>
<a href='#' class='btn btn-danger'>Delete</a>
</div>
</div>
";
}
return;
}
You're squeezing all your cards onto one row.
If you limit the amount of cards you display to 6 per row you can update your function to this:
public function showAllCompanies(){
$cardsPerRow = 6;
$i = 0;
$datas = $this->getAllCompanies();
foreach ($datas as $data){
$name = $data['name'];
$orgnr = $data['organization_number'];
$notes = empty($data['notes']) ? "No notes" : $data['notes'];
$id = $data['id'];
if($i == 0){
echo '<div class="row">';
}
echo "
<div class='card me-2 d-block' style='width: 18rem;'>
<div class='card-body'>
<h4 class='card-title'>$name</h4>
<p class='card-text'><i class='bi bi-stickies'></i> $notes</p>
</div>
<ul class='list-group list-group-flush'>
<li class='list-group-item'>Stores Owned <span class='badge text-bg-primary'>XX</span></li>
<li class='list-group-item'>Organization Number <span
class='badge text-bg-primary'>$orgnr</span></li>
</ul>
<div class='card-body'>
<a href='#' class='btn btn-primary'>Edit</a>
<a href='#' class='btn btn-danger'>Delete</a>
</div>
</div>
";
if(++$i >= $cardsPerRow){
echo '</div>';
$i = 0;
}
}
return;
}
i am trying to make a code that will work like the one below, but i need it to fetch data only once because my code below is fetching more than once and i am afraid it will slow down the server. is there a better way to do the same with the one below. basically the code below will put the list from database into its own div category.
<div class="popular-video">
<h2>Popular</h2>
<ul>
<?php
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views`,`tags` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($videoname,$username,$videourl,$uploaddate,$duration,$views,$tags);
while ($stmt->fetch()) {
if($username == 'ichigo'){
echo "
<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>
";
}
}
}
?>
</ul>
</div>
<div class="subcription-video">
<h2>Subcription</h2>
<ul>
<?php
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($videoname,$username,$videourl,$uploaddate,$duration,$views);
while ($stmt->fetch()) {
echo "
<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>
";
}
}
?>
</ul>
</div>
<div class="related-video-by-subcription">
<h2>Related-Video</h2>
<ul>
<?php
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($videoname,$username,$videourl,$uploaddate,$duration,$views);
while ($stmt->fetch()) {
echo "
<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>
";
}
}
?>
</ul>
</div>
You can make a function and call that function in your HTML.
function foo() {
global $connection;
$sexy = "sexy";
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views` FROM `videolist` WHERE `tags` = ? ";
$stmt = $connection->prepare($query);
$stmt->bind_param("s",$sexy);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) {
$stmt->bind_result($videoname, $username, $videourl, $uploaddate, $duration, $views);
while ($stmt->fetch()) {
echo "<a href='video.php?watch=$videourl'>
<li>
<div class='leftside'>
<img src='' width='100%' height='100%' style='background-color: blue;' >
</div>
<div class='rightside'>
<h4>$videoname</h4>
<p>$username</p>
<p>$views views</p>
<p>$duration</p>
</div>
</li>
</a>";
}
}
}
Then just call the function where you want it.
<div class="popular-video">
<h2>Popular</h2>
<ul>
<?php foo(); ?>
</ul>
</div>
You should separate your business logic from presentation logic.
And you can call $stmt->fetch_all() and save the result in a variable and then use this variable in your loops.
<div id="thumbnail-slider" >
<div class="inner" style="height:500px;">
<?php
$query = "select * from released_movies";
$queryr = $con->query($query);
while ($row = $queryr->fetch_assoc()) {
?>
<ul>
<li>
<img src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics']; ?>" width="900" height="500" class="thumb">
</li>
</ul>
<?php } ?>
</div> <!-- .inner-->
</div> <!-- .thumbnail-slider-->
The query is retrieving only 1 picture than the complete pictures given in the database. But if I remove the div , the query is executing fine. Thanks in advance.
Try this
<div id="thumbnail-slider">
<div class="inner" style="height:500px;">
<?php
$query="select * from released_movies";
$queryr=$con->query($query);
?>
<ul>
<?php
while($row=$queryr->fetch_assoc())
{
?>
<li>
<a href="single.php">
<img src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics'];?>" width="900" height="500" class="thumb">
</a>
</li>
<?php
}
?>
</ul>
</div> <!-- .inner-->
</div> <!-- .thumbnail-slider
You are starting and ending <ul> in while loop.
Which in incorrect, you should start it before while loop and end if after while loop.
Corrected code:
<?php
$query = "select * from released_movies";
$queryr = $con->query ( $query );
?>
<ul>
<?php
while ( $row = $queryr->fetch_assoc () ) {
?>
<li><a href="single.php">
<img src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics'];?>"
width="900" height="500" class="thumb"> </a></li>
<?php }?>
</ul>
geeks please help,
I have a problem with the usage of array keys with the while loop.
I'm trying to display the twitter bootstrap slider images by looping from the database with this code:
<div id="slider">
<!--Code for make home images slide show-->
<div id="myCarousel" class="carousel slide">
<!-- here comes the engine to run the sliders -->
<?php
$query = 'SELECT * FROM banners WHERE status = 1 ORDER BY id ASC';
?>
<div class="carousel-inner">
<?php
if ($r = mysql_query($query , $conn)) { //run the query
$row = mysql_fetch_array($r);
reset($row);
while (list($key, $value) = each($row)) {
if ($key == 0) { ?>
<div class="item active">
<img src="images/homeimages/<?php echo $value['image'];?>" alt="<?php echo $value['title'];?>">
<!-- <img src="images/homeimages/one.jpg" alt=""> -->
<div class="carousel-caption">
<!-- <h4>First Thumbnail label</h4> -->
<p><i><?php echo $value['title'];?></i></p>
</div>
</div>
<?php }else{ ?>
<div class="item">
<img src="images/homeimages/<?php echo $value['image'];?>" alt="<?php echo $value['title'];?>">
<!-- <img src="images/homeimages/one.jpg" alt=""> -->
<div class="carousel-caption">
<!-- <h4>First Thumbnail label</h4> -->
<p><i><?php echo $value['title'];?></i></p>
</div>
</div>
<?php }
}
}else {
print '<p style="color: red;">Could not retrieve the slider:<br />' . mysql_error($dbc) . '.</p>';
} ?>
</div>
<!-- <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a> -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
</div>
</div><!-- endof slider -->`
.
as`it's well known the first slider image of twitter bootstrap must have the class of active to make sure it loads faster. I need to pick the first slider image from an array and add the class of active into it while the rest of the slider images remains the same without an active class.
can someone please tell me where i'm messing up with my codes?
Try this:
<div class="carousel-inner">
<?php
if ($r = mysql_query($query , $conn)) {
$count = 0;
while ($row = mysql_fetch_array($r){
if ($count == 0){?>
<div class="item active">
<?php}
else { ?>
<div class="item">
<?php}?>
<img src="images/homeimages/<?php echo $row['image'];?>" alt="<?php echo $row['title'];?>">
<div class="carousel-caption">
<p><i><?php echo $row['title'];?></i></p>
</div>
</div>
<?php
}
$count ++;
}
}
else {
print '<p style="color: red;">Could not retrieve the slider:<br />' . mysql_error($dbc) . '.</p>';
}