I load wordpress content in an external page. I display de content in 3 columns. I created a counter to do that. This works perfectly, except that I want to make it responsive. This works for thumbnails but not for the columns. How to change the value of 3 columns to 1 in PHP if the screen or browser size is less than 1024px ? Here is my code, I appreciate your help.
<?php
$compteur=0;
echo "<table width='100%'><tr>";
$posts = get_posts('numberposts=100&order=DESC&orderby=post_date');
foreach ($posts as $post)
{
setup_postdata( $post );
echo "<td width='30%'>";
echo "<br />";
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
echo '<img width="100%" src="' . $image_src[0] . '">';
}
echo the_date();
echo "<br /><h1>";
echo the_title();
echo "</h1>";
echo the_excerpt();
echo "<td />";
$compteur++;
if ($compteur==3)
{
echo "<tr/><tr>";
$compteur=0;
}
}
echo "<tr /><table/>" ;
?>
My bad, It's completly working.
Here is the code :
<div id="articles">
<?php
$compteur=0;
echo "<div width='100%'>";
echo '<div class="colonnes">';
$posts = get_posts('numberposts=100&order=DESC&orderby=post_date');
foreach ($posts as $post)
{
setup_postdata( $post );
echo '<div class="col">' . "\n";
echo "<br />";
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
echo '<img width="100%" src="' . $image_src[0] . '">';
}
echo the_date();
echo "<br /><h1>";
echo the_title();
echo "</h1>";
echo the_excerpt();
echo "</div>";
$compteur++;
if ($compteur==3)
{
echo "</div>";
echo '<div class="colonnes">';
$compteur=0;
}
}
echo "</div /></div>" ;
?>
</div>
and the css :
.colonnes{
display:block;
width: 100%;
}
.colonnes:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.col {
border: 1px solid #999999;
float: left;
width: 30%;
}
#media (max-width: 1024px) {
.col {
width: 100%;
}
}
thank you for your help.
Use a media query and style the cells as blocks:
#media (max-width: 1024px) {
td {
display: block;
}
}
In practice, you would probably want to give the table a class and use that class in the rule so you don't affect every table.
Also, as noted in the comment above, using div instead of table cells would be easier to deal with.
Example:
$compteur=0;
echo "<div width='100%'>";
$posts = get_posts('numberposts=100&order=DESC&orderby=post_date');
foreach ($posts as $post) {
setup_postdata( $post );
echo '<div class="col">' . "\n";
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src(get_post_thumbnail_id(),’thumbnail’ );
echo '<img width="100%" src="' . $image_src[0] . '">';
}
echo the_date();
echo "\n<h1>";
echo the_title();
echo "</h1>";
echo the_excerpt();
echo "</div>";
$compteur++;
if ($compteur==3)
{
$compteur=0;
}
}
echo "</div>" ;
Then, have CSS like:
.col {
width: 30%;
float: left;
}
#media (max-width: 1024px) {
.col {
width: 100%;
}
}
ok this one is working. Now I'm trying to display posts in several pages. I change my code and add a Previous/Next navigation. When I click on next, I've got a wrong link, a 404 page of my actual website. So, what is wrong please ?
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
<div id="articles">
<?php
$args = array( 'posts_per_page' => 3 );
$compteur=0;
echo '<div class="tableau">';
echo '<div class="colonnes">';
$posts = get_posts($args,'order=DESC&orderby=post_date');
foreach ($posts as $post)
{
setup_postdata( $post );
echo '<div class="col">' . "\n";
echo '<div class="img-crop">';
if ( has_post_thumbnail() ) {
echo '<a href='.post_permalink($ID).'>';
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
echo '<img src="' . $image_src[0] . '">';
}
echo '</a>';
echo '</div>';
echo '<br/>';
echo '<div class="date">';
echo the_time('j F Y');
echo "</div>";
echo '<div class="title">';
echo the_title();
echo "</div>";
echo '<div class="excerpt">';
echo the_excerpt();
echo "</div>";
echo "</div>";
$compteur++;
if ($compteur==3)
{
echo "</div>";
echo '<div class="colonnes">';
$compteur=0;
}
}
echo "</div /></div>" ;
?>
</div>
Related
I am trying to show a dummy image from - Font awesome- when the picture is not set.
You can see my code it shows the image if there is one but when there isn't one it needs to set the dummy pic from awesomefont. tried pasting this <i class="fas fa-user-circle"></i> in the else but that isn't working. can anyone please help me?
<?php
$onderrwerp = $app->get_onderwerpen();
foreach($onderrwerp as $onderwerp){
echo '<div class="well well-sm">';
if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) {
echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
} else {
}
echo '<b> '.$onderwerp['onderwerpnaam'].'</b>';
echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
echo '</div>';
}
?>
update
this worked for me! I changed it a little with the font-size. The only problem I have is that it isnt aligned great. As you can see on the pic the text next to the picture on the first one is nice but the second and 3e are a little but lower? how to fix that?
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
<?php
$onderrwerp = $app->get_onderwerpen();
foreach($onderrwerp as $onderwerp){
echo '<div class="well well-sm">';
if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) {
echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
} else {
echo '<i style="font-size: 2.5em;" class="fas fa-user-circle"></i>';
}
echo '<b> '.$onderwerp['onderwerpnaam'].'</b>';
echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
echo '</div>';
}
?>
how it looks like now
You may be missing the required js file that's why it's not showing.
Getting Started
Try:
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
<?php
$onderrwerp = $app->get_onderwerpen();
foreach($onderrwerp as $onderwerp){
echo '<div class="well well-sm">';
if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) {
echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
} else {
echo '<i style="font-size: 6em;" class="fas fa-user-circle"></i>';
}
echo '<b> '.$onderwerp['onderwerpnaam'].'</b>';
echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
echo '</div>';
}
?>
You can put the font icon in else part and it should work perfectly.
I think your if statement isn't allowing you to goto else. just update the if condition like this:
$foto = '/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].' 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if(file_exists($foto)) {
echo '<img class="img-circle" src="'.$foto.'" />';
}
else{
echo '<i class="fas fa-user-circle"></i>'
}
For your second problem:
You can write CSS class and use it in the parent, in your case it'll be <div class="well align-children"> :
.align-children{
line-height: 40px; /* depending on the size of your icon */
display: inline-block;
vertical-align: middle;
}
You can use the styling on html document with this:
<style>
.align-children{
line-height: 40px; /* depending on the size of your icon */
display: inline-block;
vertical-align: middle;
}
</style>
I have a php to echo three variable fields :
<li>
<mark> <?php while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {
echo $toprow2['overallRank'] ." ".$toprow2['EmployeeName'] ." ".$toprow2['Total_points_Rewarded']."<br/>";} ?>
</mark>
</li>
Among these three variables of the list,I want to align the first field "overallRank " to left ,"EmployeeName " to centre and "Total_points_Rewarded" to extreme right.
Below is the code I tried for the first field:
<li>
<mark> <?php while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {
echo "<div style = "text-align=left" ."$toprow2['overallRank'] "</div>"." ".$toprow2['EmployeeName'] ." ".$toprow2['Total_points_Rewarded']."<br/>";} ?>
</mark>
</li>
when I use three divs :
echo "<div style ='text-align:left'>" . $toprow2['overallRank'] . "</div><div style ='text-align:left'>" . $toprow2['EmployeeName'] . "</div><div style ='text-align:right'>" . $toprow2['Total_points_Rewarded'] . "</div>";
I am not able to align them :Current scenario :
the first block is the rank,then name and last points - the three fields that I am trying to echo here.
You can wrap your content elements with <div> and use flexbox to align your items, e.g. like this:
li mark {
display: flex;
justify-content: space-between;
}
li mark div {
flex: 0 1 auto;
width: 100px;
height: 100px;
border: 1px solid;
}
<li>
<mark>
<div>some content</div>
<div>some content</div>
<div>some content</div>
</mark>
</li>
Your PHP code would than probably look like this:
<li>
<mark>
<?php while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {
echo "<div>" . $toprow2['overallRank'] . "</div><div>" . $toprow2['EmployeeName'] . "</div><div>" . $toprow2['Total_points_Rewarded'] . "</div>";
} ?>
</mark>
</li>
See MDN for more information or CSS-tricks for a nice guide.
You could still use list you would want to add the style to your style sheet but you will get the idea
Add new classes to style sheet
/* CSS layout */
.rstlist {
}
.ovrank {
list-style:none;
display:inline-block;
width:33%;text-align:center
}
.emname {
list-style:none;
display:inline-block;
width:33%;text-align:center
}
.tpr {
list-style:none;
display:inline-block;
width:33%;text-align:center
}
Then here is the edited script
<?php while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {
echo '<ul class="rstlist">' . PHP_EOL .
'<li class="ovrank">' . $toprow2['overallRank'] . '</li>' . PHP_EOL .
'<li class="emname">' . $toprow2['EmployeeName'] . '</li>' . PHP_EOL .
'<li class="tpr">' . $toprow2['Total_points_Rewarded'] .'</li>' . PHP_EOL .
'<ul>';
}
?>
Now you can view in different browsers and adjust the style of each element to get your desired look based on you actual output...
This is main problem of my system I can display images and their names but it cant't be a horizontal.. Need help please. Cause when I run my codes the images will be vertically but I want to be in a horizontal
Here;s the code:
echo'<?php $src=array("candidates/images/".$rows["image"]); for($i=0;$i<2;$i++){ ?>';
echo '<div clas ="image">';
echo '<img src="candidates/images/'.$rows['image'].'" width="10" height="20px" />'.', '.'<br>'.$rows['lastname'].', '.$rows['firstname'].'<br>'.' = '.$rows['votes'];
// echo '<br>';
}
$sdsd=$dsada
?>
<img src="candidates/images/percent.gif"width='<?php echo(100*round($rows['votes']/($sdsd),2)); ?>'height='10'>
<?php
if ($rows['votes']==0){
echo "<br>";}
else {
// echo(100*round($rows['votes']/($sdsd),2)); /
/*?>%<br>*/
/*<?php
}
*/echo '</div>';
}
}
Here's the CSS:
.image {
position: relative;
width: 150px;
display: inline-bloxk;
}
Remove the <br> ?
echo ''.', '.''.$rows['lastname'].', '.$rows['firstname'].''.' = '.$rows['votes'];
I have this code:
<div style="margin: 0 auto; display:block; width: 916px; overflow: auto;">
<?php echo "";
echo "<i>Owned: $line->phone </i><br><br>";
$query = "SELECT person_id, person.name, person.sex, person.father_id, person.mother_id,PhotosComp.reference as reference
FROM person
WHERE person.id=$currId";
$result = mysql_query($query);
if (mysql_num_rows($result)>0) {
while ($line = mysql_fetch_object($result)) {
echo "<div valign='top'>";
echo "";
echo "<img src=".$line->reference." style=float:left; height='50px' border='1'>";
echo "";
echo "<a href='details.php?id=$line->person_id'>";
echo "<b>";
echo "$line->name</a>";
echo "</b>, <font size='-2'>(";
echo "$line->sex";
echo ", </font><font size='-2'>";
echo "$line->father_id";
echo "<br>";
echo "$line-mother_id";
echo "<br>";
echo "</div>";
}
}echo "";
?>
</div>
The info is shown correctly vertical… but I would like to show lets say 4 results horizontal before it breaks into new line.
So I get this result from the database as it is now:
PICTURE Bob
Mick
Jane
PICTURE Roy
Mack
June
PICTURE Mia
Roy
Jane
PICTURE Lou
Bob
June
PICTURE Bib
Mock
Jine
PICTURE Beb
Muck
Jone
PICTURE Ray
Rob
Mia
And would like it to be shown as this:
PICTURE Bob PICTURE Roy PICTURE Mia PICTURE Lou
Mick Mack Roy Bob
Jane June Jane June
PICTURE Bob PICTURE Roy PICTURE Mia
Mick Mack Roy
Jane June Jane
The results from DB can be 0 to 15–20. I don’t need any upper limit.
Try this , let me know if it works .
if (mysql_num_rows($result)>0)
{
$counter = 0;
while ($line = mysql_fetch_object($result))
{
if($counter %4 != 0)
{
#this will break your div
echo "<div valign='top' style='clear:both;'>";
}
else
{
echo "<div valign='top' style='float:left;display:block;'>";
}
//if ($line->reference = (NULL)) { echo "<img src=".$line->reference." style=float:left; height='50px' border='1'>";
//else echo "<img src='../temp_pic.jpg' style=float:left; height='50px' border='1'>";}
echo "";
echo "<a href='page.php?id=$line->person_id'>";
echo "<b>";
echo "$line->name</a>";
echo "</b><br>";
echo "line->father_id";
echo"<br>";
echo "line->mother_id";
$counter++;
}
echo "</div>";
}
Some scaffolding like this might get you closer to your goal. as #DanFromGermany explained, you need to clear:both underneath your div elements. Try not to put style tags in your markup, unless your page is super optimised or something.
markup
while($result) {
echo "<div class=\"span-3\">";
echo "<div>";
echo "the content father->id line->person_id etc etc";
echo "</div>";
echo "</div>";
}
echo "<div class=\"clear-fix\"></div>";
css
.span-3 {
width:25%;
float:left;
margin:0; padding: 0;
}
.span-3 > div {
margin: 10px; /* or some other value */
}
.clear-fix {
clear:both;
}
Here is a basic bit of code to show pictures in the format you require. You will need to change this to match up with your while loop and also change the clases and css code to match your requirements ie page width etc.
<style>
html {width:100%; background:#ffffff;}
body {max-width: 960px; background:red; margin:0 auto;}
img {max-width: 225px; height: auto;}
.clear {clear:both;}
.no-margin{margin:0 !important;}
.margin-right {margin-right: 20px;}
</style>
<div class="pic-wrap">
<?php
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$pictures[] = 'http://www.canoefoundation.org.uk/cf/assets/Image/Yellow%20Duck.jpg';
$count = count($pictures);
$i=1;
foreach($pictures as $picture)
{
if($i % 4 == 0)
{
echo '<img src="'.$picture.'" class="no-margin four">';
}
elseif($i == $count)
{
echo '<img src="'.$picture.'" class="margin-right">';
echo '<div class="clear"></div>';
}
else
{
echo '<img src="'.$picture.'" class="margin-right">';
}
$i++;
}
?>
</div>
Here is an image of the output
Much like #DanFromGermany mentioned this clears floats and also adds a clear at the end by using a count of the results.
$count = count($pictures);
It then determines the last result form this
elseif($i == $count)
With $i being incremented on each iteration of the loop.
$i++;
Hope this helps.
In my code I use this MySQL query:
$qry="SELECT ServiceIcon FROM Services INNER JOIN UserServices ON UserServices.ServiceID=Services.ServiceID WHERE UserServices.UserID=$_SESSION[SESS_MEMBER_ID]";
$result=mysql_query($qry);
header("Content-type: image/png");
echo mysql_result($result,0);
I want these images to be displayed in a grid of 3 icons wide. Currently they're displayed on top of each other however. So I should echo an like this:
echo '<ul>';
foreach ($images as $image) {
echo '<li><img src="' . $image['src'] . '" id="' . $image['id'] . '" /></li>';
}
echo '</ul>';
What I don't understand is how I should incorporate into my code. Or maybe my code is not even the right way to do it. Please help me out.
EDIT: So this is the complete file in question:
<?php
session_start();
include "connection.php";
// just so we know it is broken
error_reporting(E_ALL);
// some basic sanity checks
$qry="SELECT ServiceIcon FROM Services INNER JOIN UserServices ON UserServices.ServiceID=Services.ServiceID WHERE UserServices.UserID=$_SESSION[SESS_MEMBER_ID]";
$result=mysql_query($qry);
header("Content-type: image/png");
?>
<html>
<head>
<style>
ul.horizontal-display {
margin: 0;
padding: 0;
}
ul.horizontal-display li {
list-style: none;
display: inline-block;
}
</style>
</head>
<body>
<?php
echo '<ul class="horizontal-display">';
foreach ($images as $image) {
echo '<li><img src="' . $result . '" id="' . $image['id'] . '" /></li>';
}
echo '</ul>';?>
</body>
</html>
Hello you need to add CSS. Something like this:
echo '<ul class="horizontal-display">';
foreach ($images as $image) {
echo '<li><img src="' . $image['src'] . '" id="' . $image['id'] . '" /></li>';
}
echo '</ul>';
And in the CSS rules
ul.horizontal-display {
margin: 0;
padding: 0;
}
ul.horizontal-display li {
list-style: none;
display: inline-block;
}