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
Related
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="" />';
I have this Related Posts section in my portfolio page, and I'm trying to generate the categories for each post with "the_category" function. In this case, the post on the left has categories "Photo, Test" and the rest has the category "Blog".
The function seems to be working properly, but as you can see in the attached image, the categories are showing above the title.
I want the categories to be generated within the "listing_meta" block. (Next to the comments)
<div class="blog_content">
<div class="listing_meta">
<span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>
<span class="blog_post_author">'. __('by', 'gt3_builder') .' '.get_the_author_meta('display_name').'</span>
<span class="comments">'. get_comments_number(get_the_ID()) .'</span>
<span>'. the_category(', ') .'</span>
</div>
<div class="blog_post_title"><h2>' . get_the_title() . '</h2><div class="blog_post_format_label"></div></div>
</div>
Is there any solution for this?
Thank you.
-------------------------Edited-------------------------------
I tried the function "get_the_category", but now it returns all the categories as ARRAY as shown in the image.
<div class="blog_content">
<div class="listing_meta">
<span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>
<span class="blog_post_author">'. __('by', 'gt3_builder') .' '.get_the_author_meta('display_name').'</span>
<span class="comments">'. get_comments_number(get_the_ID()) .'</span>
<span>'. get_the_category() .'</span>
</div>
<div class="blog_post_title"><h2>' . get_the_title() . '</h2><div class="blog_post_format_label"></div></div>
</div>
get_the_category()
https://developer.wordpress.org/reference/functions/get_the_category/
That is the function you want. If you use
the_category()
It will immediately echo that, which will then echo out before the rest of your content.
---------------EDIT--------------
If more than one category is returned, you can convert the array to a comma delimited list of categories with foreach:
//get all the categories
$categories = get_the_category();
//begin to assemble our string/html we want return/output
$returnedString = '<div class="blog_content">
<div class="listing_meta">
<span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>
<span class="blog_post_author">'. __('by', 'gt3_builder') .' '.get_the_author_meta('display_name').'</span>
<span class="comments">'. get_comments_number(get_the_ID()) .'</span>
<span>';
//loop over the array of category objects
foreach($categories as $category){
//append each category name to the returned string
$returnedString .= $category->name . ',';
}
//add the remaining HTML
$returnedString .= '</span>
</div>
<div class="blog_post_title"><h2>' . get_the_title() . '</h2><div class="blog_post_format_label"></div></div>
</div>';
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>
I have this variable in php with html, but it doesn't work. Can you guys find any mistake...I tried and didn't find anything.
The code is:
$html .= '<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="box-produtos">
<a href="' . $url_imagem . '" class="magnifier" rel="shadowbox">
<img src="' . $url_imagem . '" class="img-responsive img-produtos center-block" onerror="imgError(this);">
</a>
<div class="facebook-btn" onclick="window.open("http://www.facebook.com/sharer/sharer.php?u=' . $base_url . '"/facebookPublish.php?idproduto="' . $idproduto . '", "sharer", "toolbar=0,status=0,width=548,height=325");"><i class="fa fa-facebook fc-facebook"></i>' . $lang["FACEBOOK_SHARE"] . '</div>
<div class="clearfix"></div>
<h5 class="prod-size">' . $produto . '<br>' . $refproduto . '</h5>
<div class="clearfix"></div>
<h5 class="text-center font-color">' . $lang['DEFAULT_PRICE'] . ' ' . $preco . ' €</h5>
' . $lang['GERAL_COMPRAR'] . '';
You can use Heredocs to mix PHP with HTML in pretty way.
$html .= <<< HTML
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="box-produtos">
<a href="{$url_imagem}" class="magnifier" rel="shadowbox">
<img src="{$url_imagem}" class="img-responsive img-produtos center-block" onerror="imgError(this);">
</a>
<div class="facebook-btn" onclick="window.open("http://www.facebook.com/sharer/sharer.php?u={$base_url}/facebookPublish.php?idproduto={$idproduto}", "sharer", "toolbar=0,status=0,width=548,height=325");">
<i class="fa fa-facebook fc-facebook"></i>{$lang["FACEBOOK_SHARE"]}
</div>
<div class="clearfix"></div>
<a href="{$base_url}/{$langi}/produtosDesc/{$idproduto}">
<h5 class="prod-size">{$produto}<br/>{$refproduto}</h5>
</a>
<div class="clearfix"></div>
<h5 class="text-center font-color">
{$lang['DEFAULT_PRICE']} {$preco} €
</h5>
<a href="{$base_url}/{$langi}/produtosDesc/{$idproduto}" class="btn btn-skin center-block btn-prod">
{$lang['GERAL_COMPRAR']}
</a>
HTML;// no indentation, must be single in line, so remove this comment
Also you were messing with facebook link in onclick attribute.
If you want to avoid the mistakes of quotes you can try something like this :
$var="variables";
$html =<<<HTML
<h1>All your html code</h1>
<p>with all your $var or {$var}</p>
HTML;
echo $html;
it's safer.
Use <?php ?> whenever specifying the php variable.
You cannot spread a string over multiple lines. (Justinas sais you can. See comment below)
I think this question is related to your one:
Best Practices: working with long, multiline strings in PHP?
Edit 1:
I think an other solution could also be that you have your HTML in a separate file an then read its content to your variable.
$html .= file_get_contents("your-file.html");
Edit 2: Removed wrong statement. As mentioned in Justinas comment.
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 ";