php 查询中的foreach mysql行用行值回显html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8884679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
foreach mysql row from query echo out html with row values
提问by Nick Fury
So basicly what I'm trying to accomplish is that foreach row in mysql query it prints out the html with the data from that row. Here's what I have, it keeps giving me an error on my foreach.
所以基本上我想要完成的是 mysql 查询中的 foreach 行,它打印出带有该行数据的 html。这就是我所拥有的,它一直在我的 foreach 上给我一个错误。
<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row=mysql_fetch_array($shots))
$data[]=$row;
foreach($shots as $data)
if (!empty($data)){
$id = $data["id"];
$shotby = $data["shot"];
$passby = $data["pass"];
$time = $data["time"];
?>
<div class="feedbody">
<div class="title"><?php echo $shotby; ?></div>
<div class="feed-data">: gets a pass from <span><?php echo $passby; ?</span> and he takes a shot!</div>
<img class="dot" src="images/dot.png" />
</div>
<?php
}
}
?>
Or something like that. Can anybody help point me in the right direction. I've been trying to find the answer.
或类似的东西。任何人都可以帮助我指出正确的方向。我一直在努力寻找答案。
EDIT:adding the error as requested.
编辑:按要求添加错误。
Warning: Invalid argument supplied for foreach() in /home/content/93/7527593/html/fusionboard/includes/feed.php on line 7
回答by Parrots
First, if you want to access the data by name (instead of index), you need to include MYSQL_ASSOC
as a second parameter to mysql_fetch_array, or use mysql_fetch_assoc.
首先,如果要按名称(而不是索引)访问数据,则需要将其MYSQL_ASSOC
作为第二个参数包含在 mysql_fetch_array 中,或使用 mysql_fetch_assoc。
Not really sure why you were copying the MySQL results to a second array just to loop through that later - you can loop through the results directly:
不确定为什么将 MySQL 结果复制到第二个数组只是为了稍后循环 - 您可以直接循环结果:
<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row = mysql_fetch_assoc($shots)) { ?>
<div class="feedbody">
<div class="title"><?php echo $row["shot"]; ?></div>
<div class="feed-data">: gets a pass from <span><?php echo $row["pass"]; ?></span> and he takes a shot!</div>
<img class="dot" src="images/dot.png" />
</div>
<?php } ?>
Update after you posted the error message:the error from your original code was that you first went through and copied each result row into $data
, but then in your foreach you tried to loop on $shots
(again) and have it call each item $data
.
发布错误消息后更新:原始代码中的错误是您首先遍历并将每个结果行复制到$data
,但随后在您的 foreach 中尝试$shots
(再次)循环并让它调用每个 item $data
。
What you probably wanted to do was have foreach ($data as $item)
and then copy the properties from $item
.
您可能想要做的是拥有foreach ($data as $item)
然后从$item
.
回答by Jeremy Holovacs
Something like this?
像这样的东西?
<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row=mysql_fetch_assoc($shots))
{
$id = $row["id"];
$shotby = $row["shot"];
$passby = $row["pass"];
$time = $row["time"];
?>
<div class="feedbody">
<div class="title"><?php echo $shotby; ?></div>
<div class="feed-data">: gets a pass from <span><?php echo $passby; ?</span> and he takes a shot!</div>
<img class="dot" src="images/dot.png" />
</div>
<?php
}
?>
回答by PaulJ
Perhaps you need another closing brace? (Another "}" at the end, I mean).
也许你需要另一个右括号?(我的意思是最后还有一个“}”)。
回答by Daniel Castro
You saved your data in the $data variable, but your foreach uses the $shots variable.
您将数据保存在 $data 变量中,但您的 foreach 使用 $shots 变量。
Just change it to foreach($data as $something) and $something["id"] (for example) to retrieve a value
只需将其更改为 foreach($data as $something) 和 $something["id"] (例如)以检索值
回答by Your Common Sense
you are using one variable instead of another.
and many useless code.
您正在使用一个变量而不是另一个变量。
和许多无用的代码。
while youneed only
而你只需要
<?php foreach($data as $row) { ?>
<div class="feedbody">
<div class="title"><?php echo $row['shotby'] ?></div>
<div class="feed-data">:
gets a pass from <span><?php echo $row['passby'] ?</span> and he takes a shot!
</div>
<img class="dot" src="images/dot.png" />
</div>
<?php } ?>