我怎样才能让这些 if 语句起作用
我正在尝试从数据库中获取数据并用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据您的问题,我看不到您的 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 yourif
statement.您只需要使用
==
而不是=
吗?但最好将查询限制为数据库级别所需的结果,除非由于某种原因需要其他行。
Do you just need to have
==
instead of=
?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)你错过了一个等号:
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,
并且不需要嵌套的 if,你可以直接在一行中使用它,如下所示:
OK, it should go like this, assuming the fields are sorted as follows
ID, category, website, thumb2, demo, graphics
and there is no need for the nested if, you can just use it in one line as follows:
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 (==)