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?
php mysql sql
add a comment |
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?
php mysql sql
1
Use aWHERE
clause.
– Funk Forty Niner
Nov 10 at 20:56
1
SELECT * FROM inventory WHERE featured LIMIT 10
(orWHERE 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
add a comment |
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?
php mysql sql
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
php mysql sql
asked Nov 10 at 20:56
Michael White
5828
5828
1
Use aWHERE
clause.
– Funk Forty Niner
Nov 10 at 20:56
1
SELECT * FROM inventory WHERE featured LIMIT 10
(orWHERE 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
add a comment |
1
Use aWHERE
clause.
– Funk Forty Niner
Nov 10 at 20:56
1
SELECT * FROM inventory WHERE featured LIMIT 10
(orWHERE 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
add a comment |
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";
}
}
Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27
add a comment |
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";
}
}
Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27
add a comment |
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";
}
}
Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27
add a comment |
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";
}
}
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";
}
}
answered Nov 10 at 21:08
Bijay Regmi
678
678
Both of these solutions should work. I can't believe I was overthinking it that much lol.
– Michael White
Nov 10 at 21:27
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Use a
WHERE
clause.– Funk Forty Niner
Nov 10 at 20:56
1
SELECT * FROM inventory WHERE featured LIMIT 10
(orWHERE 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