foreach遍历后的二维数组[0]=>和[name]=>显示的一样?

世纪猛男 2016-08-19 01:12:27
显示结果为
Array
(
[0] => 6f79c73926857938c9f4b742c46b1a09.jpg
[path] => 6f79c73926857938c9f4b742c46b1a09.jpg
)
正常的不应该是
Array
(
[0] => array
[path] => 6f79c73926857938c9f4b742c46b1a09.jpg
)
吗?
下面上代码,这几段代码的功能是显示商品列表信息,点击详情显示更多信息包括上传的图片(代码有点长)
显示列表代码
<?php 
include "../../conf/configandconn.php";
require_once '../../comm/comm.func.php';
$sql="SELECT p.cid,p.date,p.product_id,p.product_content,p.product_level,p.product_name,p.product_price,p.status,c.catalog_name,c.id FROM product AS p JOIN catalog c ON p.cid=c.id";
$totalrows=getresultnum($sql);
$pagesize=2;
$totalpage=ceil($totalrows/$pagesize);
$page = isset($_REQUEST['page'])?(int)$_REQUEST['page']:1;
if($page<1||$page==null||!is_numeric($page)){
$page=1;
}
if($page>=$totalpage)$page=$totalpage;
$offset=($page-1)*$pagesize;
$sql="SELECT p.cid,p.date,p.product_id,p.product_content,p.product_level,p.product_name,p.product_price,p.status,c.catalog_name,c.id FROM product AS p JOIN catalog c ON p.cid=c.id LIMIT {$offset},{$pagesize}";
$res=mysql_query($sql);
if($res&&mysql_num_rows($res)){
while($row =mysql_fetch_assoc($res)){
$data[] = $row;
}
}else{
$data = array();
}
/* print_r($data);
exit; */
function showpage($page,$totalpage,$sep=" "){
$p="";
$url = $_SERVER ['PHP_SELF'];
$index = ($page == 1) ? "首页" : "<a href='{$url}?page=1'>首页</a>";
$last = ($page == $totalpage) ? "尾页" : "<a href='{$url}?page={$totalpage}'>尾页</a>";
$prev = ($page == 1) ? "上一页" : "<a href='{$url}?page=".($page-1)."'>上一页</a>";
$next = ($page == $totalpage) ? "下一页" : "<a href='{$url}?page=".($page+1)."'>下一页</a>";
$str = "总共{$totalpage}页/当前是第{$page}页";
for($i=1;$i<=$totalpage;$i++){
if($page==$i){
$p.="[{$i}]";
}else{
$p.="<a href='{$url}?page={$i}'>[{$i}]</a>";
}
}
$pagestr=$str.$sep.$index.$sep.$prev.$sep.$p.$sep.$next.$sep.$last;
return $pagestr;
}
?>
<html>
<head>
<meta charset="utf-8">
<title>-.-</title>
<link rel="stylesheet" href="../../css/backstage.css">
<link rel="stylesheet" href="../../css/jquery-ui-1.10.4.custom.css" />
<script src="../../js/jquery-1.10.2.js"></script>
<script src="../../js/jquery-ui-1.10.4.custom.js"></script>
<script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
</head>

<body>
<div id="showDetail" style="display:none;">

</div>
<div class="details">
<div class="details_operation clearfix">
<div class="bui_select">
<input type="button" value="添  加" class="add" onclick="addPro()">
</div>
<div class="fr">
<div class="text">
<span>商品名称:</span>
<div class="bui_select">
<select name="" id="" class="select">
<option value="1">测试内容</option>
<option value="1">测试内容</option>
<option value="1">测试内容</option>
</select>
</div>
</div>
<div class="text">
<span>上架时间:</span>
<div class="bui_select">
<select name="" id="" class="select">
<option value="1">测试内容</option>
<option value="1">测试内容</option>
<option value="1">测试内容</option>
</select>
</div>
</div>
<div class="text">
<span>搜索</span>
<input type="text" value="" class="search">
</div>
</div>
</div>
<!--表格-->
<table class="table" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th width="10%">编号</th>
<th width="20%">商品名称</th>
<th width="20%">商品分类</th>
<th width="20%">是否上架</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php $i=1;foreach($data as $val): ?>
<tr>
<!--这里的id和for里面的c1 需要循环出来-->
<td><input type="checkbox" id="c1" class="check" value=<?php echo $val['product_id'];?>><label for="c1" class="label"><?php echo $i;?></label></td>
<td><?php echo $val['product_name']; ?></td>
<td><?php echo $val['catalog_name'];?></td>
<td>
<?php
$show=($val['status']==1)?"上架":"下架";
echo $show;
?>
</td>
<td align="center">
<input type="button" value="详情" class="btn" onclick="showDetail(<?php echo $val['product_id'];?>,'<?php echo $val['product_name'];?>')"><input type="button" value="修改" class="btn"><input type="button" value="删除" class="btn">
<div id="showDetail<?php echo $val['product_id'];?>" style="display:none;">
<table class="table" cellspacing="0" cellpadding="0">
<tr>
<td width="20%" align="right">商品名称</td>
<td><?php echo $val['product_name'];?></td>
</tr>
<tr>
<td width="20%" align="right">商品类别</td>
<td><?php echo $val['catalog_name'];?></td>
</tr>
<tr>
<td width="20%" align="right">商品货号</td>
<td><?php echo $val['product_id'];?></td>
</tr>
<tr>
<td width="20%" align="right">商品数量</td>
<td><?php echo $row['pNum'];?></td>
</tr>
<tr>
<td width="20%" align="right">商品价格</td>
<td><?php echo $val['product_price'];?></td>
</tr>
<tr>
<td width="20%" align="right">幕课网价格</td>
<td><?php echo $row['iPrice'];?></td>
</tr>
<tr>
<td width="20%" align="right">商品图片</td>
<td>
<?php
$images=getallimgbyproid($val['product_id']);
print_r($images);
foreach($images as $img){
echo "<img src='../../img/upload/proimg/img/{$img['path']}' alt=''/> ";
}
?>
</td>
</tr>
<tr>
<td width="20%" align="right">是否上架</td>
<td>
<?php
$show=($row['isShow']==1)?"上架":"下架";
echo $show;
?>
</td>
</tr>
<tr>
<td width="20%" align="right">是否热卖</td>
<td>
<?php
$hot=($row['isShow']==1)?"热卖":"促销";
echo $hot;
?>
</td>
</tr>
</table>
<span style="display:block;width:80%; ">
商品描述<br/>
<?php echo $val['product_content'];?>
</span>
</div>
</td>
</tr>
<?php $i++; endforeach;?>
<td colspan="7"><?php echo showpage($page,$totalpage);?></td>
</tbody>
</table>
</div>
<script type="text/javascript">
function showDetail(id,t){
$("#showDetail"+id).dialog({
height:"auto",
width: "auto",
position: {my: "center", at: "center", collision:"fit"},
modal:false,//是否模式对话框
draggable:true,//是否允许拖拽
resizable:true,//是否允许拖动
title:"商品名称:"+t,//对话框标题
show:"slide",
hide:"explode"
});
}
function addPro(){
window.location='addPro.php';
}
</script>
</body>
</html>
require_once '../../comm/comm.func.php';的内容为
[code=php]function getallimgbyproid($product_id){
$sql="select path from img where product_id={$product_id}";
$result = mysql_query($sql);
$arr=mysql_fetch_array($result);
return $arr;
}

结果的为

代码有点长,还望有经验的朋友指点一下。非常感谢!
...全文
248 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2016-08-20
  • 打赏
  • 举报
回复
如果你计划可以是多张图片,那么 getallimgbyproid 应返回二维数组
xuzuning 2016-08-20
  • 打赏
  • 举报
回复
$arr=mysql_fetch_assoc($result); 后 $images 是 array('path' => '....') 再 foreach($images as $img) $img 已是单值,再 echo "<img src='../../img/upload/proimg/img/{$img['path']}' alt=''/> "; 自然就是 Warning: Illegal string offset 'path' in ... 因为 $img 不是数组
世纪猛男 2016-08-20
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
如果你计划可以是多张图片,那么 getallimgbyproid 应返回二维数组
感谢版主的耐心解答,经过你的指点我改成 function getallimgbyproid($product_id,$result_type=MYSQL_ASSOC){ $sql="select i.path from img i where pid={$product_id}"; $result = mysql_query($sql); while(@$row=mysql_fetch_array($result,$result_type)){ $rows[]=$row; } return $rows; } 问题得到解决。非常感谢!
世纪猛男 2016-08-19
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
210 行 $arr=mysql_fetch_array($result); 应改为 $arr=mysql_fetch_assoc($result);
改了之后只显示一个了,但是path的那个错误依然存在。。这是为什么啊?
xuzuning 2016-08-19
  • 打赏
  • 举报
回复
210 行 $arr=mysql_fetch_array($result); 应改为 $arr=mysql_fetch_assoc($result);

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧