Foreach 循环在分解变量后不会循环..需要帮助
我无法显示数据库中的一行,该行具有以逗号分隔的数组形式的技能列表。完整注释的代码和下面的解释是我到目前为止所做的。非常感谢所有帮助。提前致谢。
// DB Connection here
// Execute Query
$sql = "SELECT id, skills FROM p_info";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
// Delimit array by comma
$myArray = explode(',', $row['skills']);
// Store as array
$myArr = array($myArray);
/**
Assume the value of is skills row in the database is: type 1, type 2, type 3, type 4
i would like to show these values each on a seperate <li> element hence using a loop.
<li> type 1</li>
<li> type 2</li>
<li> type 3</li>
<li> type 4</li>
My guess would be to use foreach loop..but im a little confused how to go about this. Here whats i think logically:
**/
?>
<ul>
<?
foreach ( $myArr as $skills ) {
?>
<li><?=$myArr;?></li>
<?
} // End loop
?>
</ul>
<!-- Can someone tell me what im doing wrong? im fairly new to PHP but im a quick learner. :) -->
I am having trouble showing a row from a database which has a list of skills as an array which is delimited by a commas. Fully annotated code with explanation below is what i've done sor far. All help is much appreciated. Thanks in advance.
// DB Connection here
// Execute Query
$sql = "SELECT id, skills FROM p_info";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
// Delimit array by comma
$myArray = explode(',', $row['skills']);
// Store as array
$myArr = array($myArray);
/**
Assume the value of is skills row in the database is: type 1, type 2, type 3, type 4
i would like to show these values each on a seperate <li> element hence using a loop.
<li> type 1</li>
<li> type 2</li>
<li> type 3</li>
<li> type 4</li>
My guess would be to use foreach loop..but im a little confused how to go about this. Here whats i think logically:
**/
?>
<ul>
<?
foreach ( $myArr as $skills ) {
?>
<li><?=$myArr;?></li>
<?
} // End loop
?>
</ul>
<!-- Can someone tell me what im doing wrong? im fairly new to PHP but im a quick learner. :) -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除
$myArr = array($myArray);
并执行foreach ( $myArray as $skills ) {
。您将数组放入一个新数组中,因此当您尝试循环它时,您最终会得到原始数组。
还将
更改为
,您正在循环
$myArray
并将每个部分临时存储在$skills
中,$myArray
不会改变,因此尝试echo
它是没有用的。Remove
$myArr = array($myArray);
and doforeach ( $myArray as $skills ) {
instead.You are putting your array within a new array, so when you try to loop through it you end up with the original array.
Also change
<?=$myArr;?>
to<?=$skills;?>
, you are looping through$myArray
and storing every part in$skills
temporarily,$myArray
doesn't change so it's useless trying toecho
it.explode
已经返回一个数组。将结果包装在数组中将为您提供如下结果:当循环外部数组时,当然您不会获得内部数组的结果。只需摆脱
$myArr = array($myArray)
并使用$myArray
即可。explode
already returns an array. Wrapping that result in an array will give you something like this:When looping over the outer array, of course you won't get the results of the inner array. Just get rid of
$myArr = array($myArray)
and use$myArray
.$myArray
已经是一个数组。而不是
$myArr
。这就是数组。),而是使用
$myArray
is already an array.<li><?php echo $skills; ?>
instead of$myArr
. That's the array.<?
) use<?php
instead不需要执行
$myArr = array($myArray);
因为explode()
已经返回一个数组。您始终可以对数组或任何其他变量执行
var_dump($myArray)
来查看其中的内容。No need to do
$myArr = array($myArray);
asexplode()
already returns an array.You can always do a
var_dump($myArray)
on your array or any other variable to see what's in there.