简体   繁体   中英

PHP MySQL If else Statement

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>

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM