Display MySQL DB results in two different slides with RoyalSlider - php

I am using the RoyalSlider plugin (dimsemenov.com/plugins/royal-slider) to display the first 6 items from a MySQL database in the slider using PHP. I am using the LIMIT function to restrict each slide to 3 items so that the first slide displays items 1-3 from the DB and the second slide shows items 4-6. This works but it uses two separate SELECT / LIMIT queries which adds to page load time. Is it possible to merge the two separate LIMIT queries into one query?
The code I have so far is:
HTML / PHP
<div class="royalSlider rsDefault">
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 0,3");
while($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id='.$property['property_id'].'">
<img class="img-main" src="/files/'.$property['property_id'].'-1.jpg" title="" alt=""/>
<h2>'.$property['property_name'].'</h2>
<h3>'.$property['property_location'].'</h3>
</a>
</li>';
}
?>
</ul></div>
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 3,3");
while($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id='.$property['property_id'].'">
<img class="img-main" src="/files/'.$property['property_id'].'-1.jpg" title="" alt=""/>
<h2>'.$property['property_name'].'</h2>
<h3>'.$property['property_location'].'</h3>
</a>
</li>';
}
?>
</ul></div></div>

Please, try this code:
<div class="royalSlider rsDefault">
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 0,6");
$i=1;
while ($i<=3 && $property = mysql_fetch_array($qry)) {
$i++;
echo '<li><a href="/property.php?id=' . $property['property_id'] . '">
<img class="img-main" src="/files/' . $property['property_id'] . '-1.jpg" title="" alt=""/>
<h2>' . $property['property_name'] . '</h2>
<h3>' . $property['property_location'] . '</h3>
</a>
</li>';
}
?>
</ul>
</div>
<div class="rsContent">
<ul>
<?php
while ($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id=' . $property['property_id'] . '">
<img class="img-main" src="/files/' . $property['property_id'] . '-1.jpg" title="" alt=""/>
<h2>' . $property['property_name'] . '</h2>
<h3>' . $property['property_location'] . '</h3>
</a>
</li>';
}
?>
</ul>
</div>
</div>

Related

Image doesn't display using PHP

I'm trying to display the contents from database. Following query is working fine, but the problem is image does not show anymore.
<?php
session_start();
// Include config file
require_once "../auth/dbconnection.php";
$output='';
$sql='select `image`,`title`,`sub_title`,`username`,`blog_id`,`body`,`published` from `blog` WHERE user_id=? ';
$stmt=$conn->prepare( $sql );
$stmt->bind_param( 's',$_SESSION['user_id'] );
$res=$stmt->execute();
if( $res ){
$stmt->store_result();
$stmt->bind_result($image,$title,$sub_title, $username, $blog_id,$body,$published);
while( $stmt->fetch() ){
$filepath="../assets/img/blog_images/";
$title= substr($title,0,30);
$body= substr($body,0,500);
$date= date('dS F Y', strtotime($published));
$output .= '
<div class="col-sm-6 col-md-6 col-lg-4">
<div class="blog grid-blog">
<div class="blog-image">
<a href="#">';
$output .= ' <img style="height:190px; width:330px;" class="img-fluid" src="data:image/png;base64, %s" alt="" />'; base64_encode(file_get_contents($filepath.$image) ) ;
$output .= ' </a>
</div>
<div class="blog-content">
<h3 class="blog-title"> '.$title.' </h3>
<p> <code> '.$body.' </code> </p> <br>
<i class="fa fa-long-arrow-right"></i> Read More
<div class="blog-info clearfix">
<div class="post-left">
<ul>
<li><i class="fa fa-calendar"></i> <span>'.$date.'</span></li>
</ul>
</div>
<div class="post-right"><i class="fa fa-heart-o"></i>21 <i class="fa fa-eye"></i>8 <i class="fa fa-comment-o"></i>17</div>
</div>
</div>
</div>
</div>';
}
echo $output;
}else{
echo 'No any post found';
}
?>
Can anyone guide me how can fix the issue, i would like to appreciate if someone guide me regarding this. Thank You.
I assume that you omit sprintf function.
This should work.
sprintf('<img src="data:image/png;base64, %s" alt="" />', base64_encode(file_get_contents($filepath.$image));
You could put the image binary in separate variable to be easy to use
$binary = "data:image/png;base64," . base64_encode(file_get_contents($filepath . $image));
and use it like this
$output .= '<img style="height:190px; width:330px;" class="img-fluid" src="' . $binary . '" alt="" />';

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.

Loop within a Loop PHP

Ok, this is my first time using Stack so I apologize in advance for anything I do incorrectly.
The Situation:
I have a class project to rewrite HTML code in PHP. Here is a snippet of the HTML code.
<div class="col-small-6 col-med-6 col-lg-4 albumContainer">
<img src="_images/elephant_king_cover_240x240.png" alt="">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">Elephant King</a></h2>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>Elephant King</li>
<li>Joy & Sorrow</li>
<li>Traverse</li>
<li>Tres Capos</li>
<li>Timepiece</li>
<li>Adventures in Sawyerland</li>
<li>Be Still</li>
<li>Overtime</li>
<li>Bongolo</li>
<li>Coronation</li>
<li>Anchor</li>
</ol>
<h3>available at:</h3>
iTunes
Amazon
United Interests
</div>
</div>
</div>
</div>
</div><!-- end albumContainer -->
The structure repeats with different content.
The Problem:
When I run my SQl/PHP I am getting all of the information I am asking for, but it is not rendering properly. I get all of my information over and over again for as many different song titles there are (the ordered list).
I want everything to run one time for each section and for the song titles to be the content for the accordion. Here is the SQL/PHP code I have been messing around with.
<?php
require('mysqli_connect_remote.php');
$q = "SELECT Album_Art, Concat(Title, ' ', Release_Date) AS Title, destination, direction, Songs.Name FROM Albums Inner JOIN Songs ON Albums.Album_id=Songs.Album_id ";
$result = mysqli_query($dbcon, $q);
if ($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body">';
echo '<ol> <li>' . $row['Name'] . '</li> </ol> </div></div></div></div></div>';
}
// <h3>available at:</h3>
// <a href=' . ['Location'] . '>iTunes</a>
// <a href=' . ['Location2'] . '>Amazon</a>
// <a href=' . ['Location3'] . '>United Interests</a>
// ';}
mysqli_free_result ($result);
} else {
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Any insights on how to make this work for me would be most appreciated.
First, if you're doing loops in loops, or if-statements in loops, or loops in if-statements, indent better to where its clear what's in what. Sounds trivial, but it makes all the difference in the world.
And I would suggest losing the Egyptian brackets. You know, the ones that look like someone walking like an Egyptian. That is, start putting opening brackets on a new line so the opening and closing brackets line up which makes it easy to see when a bracket is missing.
I have re-indented your code without changing anything:
if ($result)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body">';
echo '<ol> <li>' . $row['Name'] . '</li> </ol> </div></div></div></div></div>';
}
/*<h3>available at:</h3>
<a href=' . ['Location'] . '>iTunes</a>
<a href=' . ['Location2'] . '>Amazon</a>
<a href=' . ['Location3'] . '>United Interests</a>
';}*/
mysqli_free_result ($result);
}
else
{
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Now you can actually see what's going on. Initially when I re-indented the first time I missed one of your closing brackets that was hiding at the end of a long long line and thought you were missing the closing bracket of the loop.
And it seems the problem is actually with your SQL query. Throwing a distinct in there might solve it:
$q = "SELECT DISTINCT Album_Art, Concat(Title, ' ', Release_Date) AS Title, destination, direction, Songs.Name FROM Albums Inner JOIN Songs ON Albums.Album_id=Songs.Album_id ";

Combine site content with external site rss

I'm currently running a classified ads site and planning to display my own content combined with and external site rss.
So here is what i got right now after the db query for the jobs ads,
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo '<div class="media margin-none">
<a class="pull-left bg-inverse innerAll text-center" href="#"><img src="'.$foto.'" share_alt="" width="100" height="100"></a>
<div class="media-body innerAll">
<h4 class="media-heading innerT">
'. $remuneracion .' ' . substr(ucfirst(strtolower($row['title'])), 0, 53) . ' <small class="pull-right label label-default"><i class="fa fa-fw fa-calendar-o"></i> ' . $row['date_created'] . '</small></h4>
<p>' . substr(ucfirst(strtolower($row['description'])), 0, 80) . ' ...</p>';
echo '</div>
</div>
<div class="col-separator-h"></div>';
}
echo pagination($statement,$per_page,$page, $url_filtros, $filtros);
?>
it is the while loop that i use to display ads from my database, what could be the best way to display (in this same loop?) other site's rss feed?
Thanks

HOWTO: get data from MYSQL database and then use/show this data in a php page in a while/foreach?

I figured out how to get the data, how to use it in a while loop, but I am stuck on the last part: a loop in a loop.
I have the following code:
<div id="tab1" class="tab_content">
<ul class="columns">
<?php
$result = mysql_query("SELECT * FROM TabContent WHERE TabID = 1");
while($row = mysql_fetch_array($result))
{
echo '<li><img src="images/layouts/'. $row['LayoutName'] .'.png" alt="" />
<div class="info">
<h2>'. $row['LayoutName'] .'</h2>
<p>'. $row['LayoutTiles'] . 'Tiles, '. $row['LayoutLayers'] . 'Layers</p>
</div>
</li>';
}
?>
</ul>
<div class="clear"></div>
<div class="bottom">
<div class="clearfix">
<div class="lfloat"></div>
<div class="rfloat"><a class="hide" href="#" onclick="return false">Cancel</a></div>
</div>
</div>
</div>
<div id="tab2" class="tab_content">
etc etc same as above
The problem I have is with the div with id="tab1" and the next one with id="tab2" etc. I can get an array of tabs using:
<?php
$result = mysql_query("SELECT * FROM Tabs");
while($row = mysql_fetch_array($result))
{
echo 'tab'. $row['TabID'] .;
}
?>
Giving me: Tab1 Tab2 Tab3 etc. Now the hard part, how do I use these so I can make a loop using this to replace my html div ids?
Here's a couple of tutorials that might help you in your struggle:
http://www.freewebmasterhelp.com/tutorials/phpmysql
http://www.homeandlearn.co.uk/php/php.html

Categories