I want to show 3 images in a row but i cant find the problem in my code.
<div class="pagh">
<?php
$sql = "SELECT * from imagens INNER JOIN users ON imagens.id_user = users.id_user ";
$consulta = mysqli_query($bd, $sql);
$n_linhas = mysqli_num_rows($consulta);
if ($n_linhas!=0) {
for ($i=1;$i<=$n_linhas;$i++){
$dados = mysqli_fetch_array($consulta);
echo '<div class="row">';
echo '<div class="column">
<img src="user_img/' . $dados["filename"] . '" width="150%">';
echo '</div>';
echo '</div>';
}//for
}//if
?>
code img
change your code like this :
echo '<div class="row">;
for($i = 1; $i<$n_linhas;$i++){
echo '<div class="column">
<img src="...">
</div>';
}
echo '</div>';
In your code, at each $i your write a new row
Inspect your code with right click to see what happend
Related
I'm trying to do a featured car section in my used car lot website.
What I want to do is to use a while loop and select only the rows that have a specific column that returns true. For example, in my database, I have a column that is marked true if a checkbox on the submit form is ticked indicating that the car is supposed to be "featured".
In my while loop I only want those cars to appear in this particular section. Using the code below, I can return all vehicles in the inventory but no way to select only the ones with the "featured" column that returns a true value.
<?php
$sql = "SELECT * FROM inventory LIMIT 10";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc())
{
echo '<div class="slide">';
echo '<div class="car-block">';
echo '<div class="img-flex">';
echo '<a href="inventory-listing.php?vin='.$row['vin'].'">';
echo '<span class="align-center">';
echo '<i class="fa fa-3x fa-plus-square-o"></i></span></a>';
echo '<img src="images/inventory/'.$row['vin'].'-main.png" alt="" class="img-responsive">';
echo '</div>';
echo '<div class="car-block-bottom">';
echo '<h6><strong>'.$row['year'].' '.$row['make'].' '.$row['model'].'</strong></h6>';
echo '<h6>'.$row['body'].', '.$row['milage'].' miles</h6>';
echo '<h5>$ '.$row['price'].'</h5>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
What would I be able to add to my sql query to filter it only the rows where the "featured" column returns true?
Alternative to Goleztrols suggestion, you could just insert an IF clause before echo-ing the results.
<?php
$sql = "SELECT * FROM inventory LIMIT 10";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc())
{
if ($row['featured']=="true")
{
echo '<div class="slide">';
echo '<div class="car-block">';
echo '<div class="img-flex">';
echo '<a href="inventory-listing.php?vin='.$row['vin'].'">';
echo '<span class="align-center">';
echo '<i class="fa fa-3x fa-plus-square-o"></i></span></a>';
echo '<img src="images/inventory/'.$row['vin'].'-main.png" alt="" class="img-responsive">';
echo '</div>';
echo '<div class="car-block-bottom">';
echo '<h6><strong>'.$row['year'].' '.$row['make'].' '.$row['model'].'</strong></h6>';
echo '<h6>'.$row['body'].', '.$row['milage'].' miles</h6>';
echo '<h5>$ '.$row['price'].'</h5>';
echo '</div>';
echo '</div>';
echo '</div>';
}
else
{
echo "nothing to be shown";
}
}
I already get all data but I don't know how to fetch data with switch column for ex 1st data in left img in right , 2 nd data in right img in left( display will be in like my html )
Here is My php
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
while ($row = mysql_fetch_array($result)){
$data = $row["DATA"];
$img = $row["IMG"];
Here is my HTML ( I want data to fetch like this )
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/ >
<div class="col-md-6">
<div style="height:150px;background-color: red"> $data</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: blue">$img</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: blue"> $img</div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: red">$data</div>
</div>
Consider the four div as a combination of 2 div. Use a counter with mod operator to match first two div or 2nd two div you are dealing with.
$i = 0 ;
while ($row = mysql_fetch_array($result)){
if( $i++ % 2 == 0 ) {
$col1 = $row["DATA"];
$col2 = $row["IMG"];
$color1 = 'blue' ;
$color2 = 'red' ;
}
else {
$col1 = $row["IMG"];
$col2 = $row["IMG"];
$color1 = 'red' ;
$color2 = 'blue' ;
}
?>
<div class="col-md-6">
<div style="height:150px;background-color: <?php echo $color1;?>"><?php $col1;?></div>
</div>
<div class="col-md-6">
<div style="height:150px;background-color: <?php echo $color2;?>"><?php $col2;?></div>
</div>
<?
php
} //end of while
?>
Logic:
It will print two div at a time. when the loop begin $i is zero. 0 %2 will result in 0; so first data will be printed. after that $++ will will make it 1. when the next time loop comes 1%2 is != 0 so else part will be considerd.
Have you reviewed PHP syntax?
Rather than saving your file as .html, save it as .php. You can write HTML exactly the same, but have the added benefit of being able to use PHP as well. Then you can do this:
<?php
// You can start the file in PHP
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
while ($row = mysql_fetch_array($result)){
$data = $row["DATA"];
$img = $row["IMG"];
?>
<!-- Now we are in HTML, but still hop back into PHP to work with variables. -->
<?foreach ($row as mysql_fetch_array( $result ) ):?>
<? $data = $row['DATA'];
$img = $row['IMG'];
?>
<div>
<div> <? echo $data ?> </div>
<div> <? echo $img ?> </div>
</div>
<? endforeach ?>
If I was to write this, this is how I would do it.
Updated code to reflect your OP markup.
<?php
$SQL = "SELECT * FROM DB_NEWS";
$result = mysql_query($SQL);
// define variable
$html = NULL;
while ($row = mysql_fetch_array($result)) {
$html .= ''
. '<div class="col-md-6">'
. '<div style="height:150px;background-color: red">' . $row['DATA'] . '</div>'
. '</div>'
. '<div class="col-md-6">'
. '<div style="height:150px;background-color: blue">' . $row['IMG'] . '</div>'
. '</div>';
}
echo $html;
My Carousel only displays the first thing from the database but wont display the next one and onward
this is my code and the query
it wont display the 2nd image
but it displays the 1st image
can anybody help thanks
<?php
require_once("db2.php");
$GardenDBB = new GardenDBB();
$result = ($GardenDBB->LoadAllGallery());
$num_rows = mysql_num_rows($result);
$counter = 1;
while($db_field = mysql_fetch_array($result)){
?>
<div class="item<?php if ($counter <= 1){echo " active"; ?>" style="background-image: url(<?php echo './upload2/'.$db_field['image_name']?>")>;
<?php
echo '<div class="container">';
echo '<div class="row slide-margin">';
echo '<div class="col-sm-6">';
echo '<div class="carousel-content">';
echo '<h1 class="animation animated-item-1">'.$db_field['proName'].'</h1>';
echo '<h2 class="animation animated-item-2">'.$db_field['proDescription'].'</h2>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
$counter++;
}
}
?>
public function LoadAllGallery() {
$db_found = $this->connect_Db();
if ($db_found) {
$SQL = "SELECT * FROM tbl_car";
$result = mysql_query($SQL);
return $result;
mysql_free_result($result);
}
else {
$errorMsg = "Database NOT Found, Please connect your administrator ";
}
$this->close_DB();
}
This should work
if ($counter <= 1)
{
// DIV CLASS="ITEM" wrapper starts here
}
//all the rest of the display code
if ($counter <= 1)
{
echo '</div>'; // DIV CLASS="ITEM" end of wrapper
}
$counter++;
Hi I would like to order custom fields in wordpress. I've got it displaying the single project. But can't figure out how I can get it to display in order. I've created a field called project_order which works correctly on another page.
But i'm not used to this sort of php with order by. This is a project designed by someone else so i'm trying to pick up and learn how it was built.
<?php
//associate sector to product projects page
$sector['construction'] = 131;
$sector['timber-frame'] = 235;
$sector['industrial'] = 253;
$sector['agriculture'] = 263;
//link to view all projects for product
echo '<p>View all projects</p>';
?>
</div>
<div class="span5">
<div id="latestproject">
<h2>Latest Project</h2>
<?php
//get latest projects
$rows = get_field('projects', $sector [$post->post_name] , '&order=ASC');
//display latest project
if ($rows) {
echo '<div class="row">';
if ($rows[0]['project_pageturner']) {
echo '<a href="'.$rows[0]['project_pageturner'].'" target="_blank">';
} else if ($rows[0]['project_pdf']) {
echo '<a href="'.wp_get_attachment_url($rows[0]['project_pdf']).'" target="_blank">';
} else {
echo "<a href=\"javascript:alert('No PDF uploaded for this item.')\">";
}
echo '<div class="span2">';
if ($rows[0]['project_thumbnail']) {
echo wp_get_attachment_image($rows[0]['project_thumbnail'], 'full');
} else {
echo '<img src="'.get_bloginfo('template_directory').'/images/defaultproject.jpg" alt="">';
}
echo '</div>';
echo '<div class="span3">';
echo '<p><strong>'.$rows[0]['project_title'].'</strong></p>';
echo '<p>View project »</p>';
echo '</div>';
echo '</a>';
echo '</div><!--row-->';
} else {
echo '<p>No projects to display currently.</p>';
}
?>
I'm guessing it will have something to do with.
$rows = get_field('projects', $sector [$post->post_name]);
Try below code, may be this resolve your problem.
<?php
wp_reset_query();
//associate sector to product projects page
$sector['construction'] = 131;
$sector['timber-frame'] = 235;
$sector['industrial'] = 253;
$sector['agriculture'] = 263;
//link to view all projects for product
echo '<p>View all projects</p>';
?>
</div>
<div class="span5">
<div id="latestproject">
<h2>Latest Project</h2>
<?php
//get latest projects
$rows = get_field('projects', $sector [$post->post_name] , '&order=ASC');
//display latest project
if ($rows) {
echo '<div class="row">';
if ($rows[0]['project_pageturner']) {
echo '<a href="'.$rows[0]['project_pageturner'].'" target="_blank">';
} else if ($rows[0]['project_pdf']) {
echo '<a href="'.wp_get_attachment_url($rows[0]['project_pdf']).'" target="_blank">';
} else {
echo "<a href=\"javascript:alert('No PDF uploaded for this item.')\">";
}
echo '<div class="span2">';
if ($rows[0]['project_thumbnail']) {
echo wp_get_attachment_image($rows[0]['project_thumbnail'], 'full');
} else {
echo '<img src="'.get_bloginfo('template_directory').'/images/defaultproject.jpg" alt="">';
}
echo '</div>';
echo '<div class="span3">';
echo '<p><strong>'.$rows[0]['project_title'].'</strong></p>';
echo '<p>View project »</p>';
echo '</div>';
echo '</a>';
echo '</div><!--row-->';
} else {
echo '<p>No projects to display currently.</p>';
}
wp_reset_query();
?>
Thanks.
I need to do the following - get the list of all artists from the database and print them on a page. But besides of that, I need to make another query to the "albums" table and get the albums of each artist and print the images under each artist description. The code I've written is as follows:
require('db.php');
$query = "SELECT * FROM artists";
$result = mysql_query($query);
$artist_id = $result[id];
$artist_name = $result[name];
$artist_surname = $result[surname];
$artist_email = $result[email];
$artist_about = $result[about];
$artist_photo = $result[photo];
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result)) :
print "<div class='row'>";
print "<div class='artists_left'>";
print "<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>";
print "</div>";
print "<div class='artists_right'>";
print "<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>";
print "<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>";
print "<ul class='artists'>";
$albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
$albums_result = mysql_query($albums_query);
$album_id = $albums_result[id];
$album_photo = $albums_result[title_photo];
$album_dirname = $albums_result[dirname];
while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($result)) :
print "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
endwhile;
print "</ul>";
print "<a href='#' class='seemore_portfolio'>все работы мастера ></a>";
print "</div>";
print "</div>";
endwhile;
mysql_close($connect);
The outer query works fine, but the inner one - does not. Could anybody help me to figure this out?
Thanks in advance.
Change the second $result to $albums_result
You can also consider writing the whole thing like this...
<?
require('db.php');
$query = "SELECT * FROM artists";
$result = mysql_query($query);
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result))
{
$li = '';
$albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
$albums_result = mysql_query($albums_query);
while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($albums_result))
{
$li .= "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
}
echo "<div class='row'>
<div class='artists_left'>
<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>
</div>
<div class='artists_right'>
<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>
<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>
<ul class='artists'>
$li
</ul>
<a href='#' class='seemore_portfolio'>все работы мастера ></a>
</div>
</div>";
}