1

I have the following code

if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_array($result))
    {
    $output .= '
                            <div class="col-md-3">
                                <div class="single-post">
                                    <img src="img/cars/'.$row["fahrzeugBild"].'.jpg" alt="">
                                    <h4><span class="author-name">Modell: '.$row["fahrzeugName"].'</span></h4>

How is it possible getting $output into If else statement? So if $row is empty it should return nothing.

if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_array($result))
    {
    $output .= '
echo    <div class="col-md-3">
echo    <div class="single-post">
If .$row["fahrzeugBild"] empty then
nothing
else
echo    <img src="img/cars/'.$row["fahrzeugBild"].'.jpg" alt="">

If .$row["fahrzeugName"] empty then
nothing
else
echo    <h4><span class="author-name">Modell: '.$row["fahrzeugName"].'</span></h4>

0

1 Answer 1

1

You've made a pretty good effort and it shows in the details of your question.

There are a couple of places you might use an if statement, depending on what you mean.

OK, I think I understand now. You want the individual elements of the $row to control individual elements in additions to the $output.

That'll require a series of checks, sort of like you've done, but not inside the string itself.

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_array($result)) {
      if ($row == "") {
       // do one thing
      } else {
        $output .= '<div class="col-md-3"><div class="single-post">';
        if ($row["fahrzeugBild"] !== "") {
          $output .= '<img src="img/cars/'.$row["fahrzeugBild"].'.jpg" alt="">';
        }
        if ($row["fahrzeugName"] !== "") {
          $output .= '<h4><span class="author-name">Modell: '.$row["fahrzeugName"].'</span></h4>';
        }
// and so forth for all of your other cases
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello and thank you. Im looking just for the $row not for the whole $output as I tried to explain in my example. I'm giving this a try thank you!
I've tried editing my answer, perhaps better to correspond to what you are asking!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.