SQL While loop select all rows that have a column checked











up vote
-1
down vote

favorite












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?










share|improve this question


















  • 1




    Use a WHERE clause.
    – Funk Forty Niner
    Nov 10 at 20:56






  • 1




    SELECT * FROM inventory WHERE featured LIMIT 10 (or WHERE featured = true). This is assuming that the field featured is a boolean field, or a numeric field with 1 and 0 values, which is common for boolean values. (See Boolean literals)
    – GolezTrol
    Nov 10 at 20:58












  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27















up vote
-1
down vote

favorite












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?










share|improve this question


















  • 1




    Use a WHERE clause.
    – Funk Forty Niner
    Nov 10 at 20:56






  • 1




    SELECT * FROM inventory WHERE featured LIMIT 10 (or WHERE featured = true). This is assuming that the field featured is a boolean field, or a numeric field with 1 and 0 values, which is common for boolean values. (See Boolean literals)
    – GolezTrol
    Nov 10 at 20:58












  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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?










share|improve this question













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?







php mysql sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:56









Michael White

5828




5828








  • 1




    Use a WHERE clause.
    – Funk Forty Niner
    Nov 10 at 20:56






  • 1




    SELECT * FROM inventory WHERE featured LIMIT 10 (or WHERE featured = true). This is assuming that the field featured is a boolean field, or a numeric field with 1 and 0 values, which is common for boolean values. (See Boolean literals)
    – GolezTrol
    Nov 10 at 20:58












  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27














  • 1




    Use a WHERE clause.
    – Funk Forty Niner
    Nov 10 at 20:56






  • 1




    SELECT * FROM inventory WHERE featured LIMIT 10 (or WHERE featured = true). This is assuming that the field featured is a boolean field, or a numeric field with 1 and 0 values, which is common for boolean values. (See Boolean literals)
    – GolezTrol
    Nov 10 at 20:58












  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27








1




1




Use a WHERE clause.
– Funk Forty Niner
Nov 10 at 20:56




Use a WHERE clause.
– Funk Forty Niner
Nov 10 at 20:56




1




1




SELECT * FROM inventory WHERE featured LIMIT 10 (or WHERE featured = true). This is assuming that the field featured is a boolean field, or a numeric field with 1 and 0 values, which is common for boolean values. (See Boolean literals)
– GolezTrol
Nov 10 at 20:58






SELECT * FROM inventory WHERE featured LIMIT 10 (or WHERE featured = true). This is assuming that the field featured is a boolean field, or a numeric field with 1 and 0 values, which is common for boolean values. (See Boolean literals)
– GolezTrol
Nov 10 at 20:58














Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27




Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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





share|improve this answer





















  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243331%2fsql-while-loop-select-all-rows-that-have-a-column-checked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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





share|improve this answer





















  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27















up vote
2
down vote



accepted










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





share|improve this answer





















  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:08









Bijay Regmi

677




677












  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27


















  • Both of these solutions should work. I can't believe I was overthinking it that much lol.
    – Michael White
    Nov 10 at 21:27
















Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27




Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243331%2fsql-while-loop-select-all-rows-that-have-a-column-checked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python