我怎样才能让这些 if 语句起作用

发布于 2024-11-08 20:57:34 字数 1219 浏览 1 评论 0原文

我正在尝试从数据库中获取数据并用 html 标签包装它。这是到目前为止的工作代码:

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> <!-- gal1 -->';
      $i=0;
      } 
   }
}

在这里,我从数据库中获取所有内容(从投资组合中选择*),但在我的投资组合中,有网站、演示和图形;所以我只想从上面的代码中获取category=“web”的数据,所以我尝试了这个:

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

现在嵌套的if语句不会生成我需要的div,我怎样才能让它工作,

谢谢你的帮助

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> <!-- 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山有枢 2024-11-15 20:57:34

根据您的问题,我看不到您的 SQL,但您可以修改您的 SELECT 查询以包含 WHERE Category="web"

这样,您就只需选择您需要的行,而不是循环该表中的每一行。

此外,您似乎在 if 语句中使用赋值 = 而不是比较 ==

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.

╭ゆ眷念 2024-11-15 20:57:34

您只需要使用 == 而不是 = 吗?

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

但最好将查询限制为数据库级别所需的结果,除非由于某种原因需要其他行。

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.

梦太阳 2024-11-15 20:57:34

1)你错过了一个等号:

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

或者更好

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

2)如果您如果只想获取具有特定 category 字段的字段,您只需更改查询即可:

[查询的其余部分] WHERE Category="web"

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"

好的,应该是这样的,假设字段排序如下:

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

并且不需要嵌套的 if,你可以直接在一行中使用它,如下所示:

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

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 -->';
}
时光倒影 2024-11-15 20:57:34

1) 更改查询以包含 WHERE Category="web" 子句

2) 当您需要相等运算符 (==) 时,您的 if 子句 (=) 中有一个赋值运算符

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 (==)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文