SQL While loop select all rows that have a column checked - php

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";
}
}

Related

Display 3 images in a row using php

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

My Carousel only displays the first thing from the database but wont display the next one and onward

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++;

trying to show the same content in page and also in fancybox

Im trying to fill my articles with content that is coming from the database.
I have my articles with: title, image, date, content and with a link (read more) to open a fancybox that will show the same content of this article but in the fancybox.
So I have my div id="show-container" that corresponds to the div that shows the content in fancybox.
This div have display:none in css and it only appears when the user click in the link with #show href and class="fancybox", here:
<a class="fancybox" href="#show">Read more</a>
But I want to show the same content in fancybox so Im fill this "show" div with the same info that I put in the article.
My articles are working fine, each article is with right name,image and content.
But when I click to open the fancybox of each article all the fancyboxs have the same content, that is the content of my first article.
Anyone there know how I can fix this?
$readPost = $pdo->prepare("SELECT * FROM posts ORDER BY date DESC LIMIT 0,4");
$readPost->execute();
$folder = '../images/';
while ($readPostResult = $readPost->fetch(PDO::FETCH_ASSOC))
{
echo '<article id="loop-news">';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<h2>';
echo '<a href="#show" class="x" >'.$readPostResult['title'].'</a><br />';
echo '</h2>';
echo '<p>'.$readPostResult['content'].'<a class="fancybox" href="#show">Read more</a></p>';
echo '<div id="show-container">';
echo '<div id="show">';
echo '<h2>'.$readPostResult['title'].'</h2>';
echo '<br />';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<p>'.$readPostResult['content'].'<br /></p>';
echo '<a class="button" href="index.html">Back</a>';
echo '</div>';
echo '</div>';
echo '</article>';
}
?>
I think I need to pass the id of each news when I click in my a href:
echo '<p>'.$readPostResult['content'].'<a class="fancybox" href="#show">Read more</a></p>';
Do you know How I can do that?? Because Im already using "#show#" in my link to open the fancybox..
You have to use class for css instead of id
Your ID values are meant to be unique to the element. Because you're generate the content in a loop, you will have multiple duplicate ID values in your elements. Try changing it to this:
$i = 0;
while ($readPostResult = $readPost->fetch(PDO::FETCH_ASSOC))
{
echo '<article id="loop-news-' . $i . '">';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<i class="fa fa-download"></i>';
echo '<h2>';
echo '<a href="#show" class="x" >'.$readPostResult['title'].'</a><br />';
echo '</h2>';
echo '<span>'.date('d/m/Y H:i',strtotime($readPostResult['date'])).'</span>';
echo '<p>'.lmWord($readPostResult['content'],270).'<a class="fancybox" href="#show-' . $i . '" id="showFancy-' . $i . '">Read more</a></p>';
echo '<div id="show-container-' . $i . '">';
echo '<div id="show-' . $i . '">';
echo '<h2>'.$readPostResult['title'].'</h2>';
echo '<br />';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<p>'.$readPostResult['content'].'<br /></p>';
echo '<a class="button" href="index.html">Back</a>';
echo '</div>';
echo '</div>';
echo '</article>';
++$i;
}
You'll notice, I've created an $i variable outside the while loop, and it appends the value of $i to all your ID elements. Once at the end of the loop, it will increase $i by 1, so that all your IDs in your loop will be unique.

Wordpress Ordering (boolean?)

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.

Php foreach echo issue

I have this code which produces me a number which is equal to the number of id's i've got in my database of star rating system.
This code generates me a five star voting for each id i've got, but the problem is, it generates them all in a div, while i need them specifically in different div's. let's suppose i print out in a div information for each hostess i've got, i print out their photo and name with the following code:
$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "<div id='photo'>";
echo "<div id='picture'>";
echo "<div id='scotch'><img src='images/Scotch.png'></div>";
echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
echo "</div>";
echo "<div id='text'>";
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
echo "</div>";
echo "</div>";
echo "<div id='photo2'>";
echo "<div id='picture'>";
echo "<div id='notes'>";
echo '<form action="index.php" method="post" >';
echo "<label>Notes</label></br><textarea>".$row['courrent_occupation'] . "</textarea></br>";
echo '<input type="submit" value="edit" name="edit"></div>';
echo "</div>";
echo "<div id='notes'>";
echo "<label>profile</label></br><textarea>".$row['profile_en'] . "</textarea>";
echo "</div>";
echo "</div>";
}
?>
</div>
Now, i've got this other php which generates me all the star ratings for all hostess id's
<?php
// include update.php
include_once 'update.php';
// get all data from tabel
$arr_star = fetchStar();
?>
<?php
// start looping datas
foreach($arr_star as $star){ ?>
<h2>Star Rater - <?php echo $star['id'];?></h2>
<ul class='star-rating' id="star-rating-<?php echo $star['id'];?>">
<?php /* getRating($id) is to generate current rating */?>
<li class="current-rating" id="current-rating-<?php echo $star['id'];?>" style="width:<?php echo getRating($star['id'])?>%"><!-- will show current rating --></li>
<?php
/* we need to generate 'id' for star rating.. this 'id' will identify which data to execute */
/* we will pass it in ajax later */
?>
<span class="ratelinks" id="<?php echo $star['id'];?>">
<li>1</li>
<li>1.5</li>
<li>2</li>
<li>2.5</li>
<li>3</li>
<li>3.5</li>
<li>4</li>
<li>4.5</li>
<li>5</li>
</span>
</ul>
<?php } ?>
What i need is to assign each hostess profile i print their system rating.
I try to insert the foreach inside the first script but it then shows me just one profile, not all profiles.
The fetchstar() code is:
function fetchStar(){
$sql = "select * from `hostess`";
$result=#mysql_query($sql);
while($rs = #mysql_fetch_array($result,MYSQL_ASSOC)){
$arr_data[] = $rs;
}
return $arr_data;
}
First, you probably shouldn't use SELECT *. That aside I would combine the two queries you have to return a multidimensional array with MySQL and then use nested for each loops to echo out the data you want.
Someone answered a similar question for me here.
Looping through MySQL left join in php vs. 2 separate queries
$sql =" select * from hostess";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
if ($lastID <> $row['id']) {
$lastID = $row['id'];
$hostess[$lastID] = array('id' => $row['id'],
'first_name_en' => $row['first_name_en'],
etc
'arr_star' => array() );
}
$hostess[$lastID]['arr_star'][] = array('star_id' => $row['star_id'] etc);
}
Then you would use nested for each statements
for each($row as $rows){
//echo your hostess information
for each ($arr_star as $star){
//echo your star rating information
}
}

Categories