开发者

how can I get these if statements working

i am trying to get my data from the database and wrap it with html tags. here is the working code so far:

function homethumb(){ $this->count; $i = 0;
   while($row = mysqli_fetch_object($this->result))
   {
      $this->count++; $i++;
      if($i == 1){echo '<div class="gal1">';}
      echo '<a href="portfolio.php?id=' . $row->id . '"> <div class="gal"><img src="img/' . $row->thumb2 . '.jpg"></div></a>';
      if($i == 2){
      echo '</div> <!开发者_JAVA技巧-- gal1 -->';
      $i=0;
      } 
   }
}

Here I am getting everything from the database (Select * from portfolio), but in the portfolio I have, websites, demos and graphics; so I wanted to get only the data where category = "web" from the above code, so I tried this:

function homethumb(){ $this->count; $i = 0;
   while($row = mysqli_fetch_object($this->result))
   {
      if($row->category = "web"){
               $this->count++; $i++;
         if($i == 1){echo '<div class="gal1">';}
         echo '<a href="portfolio.php?id=' . $row->id . '"> <div class="gal"><img src="img/' . $row->thumb2 . '.jpg"></div></a>';
         if($i == 2){
         echo '</div> <!-- gal1 -->';
         $i=0;
         } 
      }
   }
}

now the nested if statements do not generate the divs I need, how can I get this working

thanks for your help


I can't see your SQL based on your question, but you could just modify your SELECT query to include WHERE category="web"

This way, you're only selecting the rows you need, instead of looping over every row in that table.

Additionally, it appears that you're using assignment = instead of comparison == for your if statement.


Do you just need to have == instead of =?

if($row->category == "web"){

But it would be best to restrict the query to the results you need at the database level, unless you need the other rows for some reason.


1)You missed an equal sign:

if($row->category = "web") => if($row->category == "web")

Or better yet

if($row->category === "web")

2)If you want to only get fields with a specific category field, you can simply change your query:

[rest of your query] WHERE category="web"


OK, it should go like this, assuming the fields are sorted as follows

ID, category, website, thumb2, demo, graphics

function homethumb(){ $this->count; $i = 0;
   while($row = mysqli_fetch_object($this->result))
   {
      if($row[1] == "web"){
               $this->count++; $i++;
         if($i == 1){echo '<div class="gal1">';}
         echo '<a href="portfolio.php?id=' . $row[0] . '"> <div class="gal"><img src="img/' . $row[3] . '.jpg"></div></a>';
         if($i == 2){
         echo '</div> <!-- gal1 -->';
         $i=0;
         } 
      }
   }
}

and there is no need for the nested if, you can just use it in one line as follows:

if($row[1] = "web")
{
    echo '<div class="gal1">';
    echo '<a href="portfolio.php?id=' . $row[0] . '"> <div class="gal"><img src="img/' . $row[3] . '.jpg"></div></a>';
    echo '</div> <!-- gal1 -->';
}


1) Change your query to contain a WHERE category="web" clause

2) You have an assignment operator in your if clause (=), when you need an equality operator (==)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜