首页 >后端开发 >php教程 >html标签如何使用php中的变量

html标签如何使用php中的变量

WBOY
WBOY原创
2016-07-06 13:53:201602浏览

html标签中使用php变量,提示:Undefined index: uid in /var/www//list.php
,list.php的具体代码如下:

<code><?php require 'redis.php';
    for ($i=0; $i <= ($redis->get("userid")) ; $i++)
    {
        $data[] = $redis->hgetall("user:".$i);
    }
    /* var_dump($data) 的结果如下:
    array (size=3)
  0 => 
    array (size=0)
      empty
  1 => 
    array (size=4)
      'uid' => string '1' (length=1)
      'username' => string 'jjj' (length=3)
      'password' => string '123' (length=3)
      'age' => string '20' (length=2)
  2 => 
    array (size=4)
      'uid' => string '2' (length=1)
      'username' => string 'lamp' (length=4)
      'password' => string '123' (length=3)
      'age' => string '20' (length=2)
      */
?>

<table border="1">
    <caption>user list</caption>
    <tr>
        <th>uid</th>
        <th>username</th>
        <th>age</th>
    </tr>
    <?php foreach ($data as $v) {?>

    <tr>
        <td><?php echo $v['uid'] ?></td>
        <td><?php echo $v['username'] ?></td>
        <td><?php echo $v['age'] ?></td>
    </tr>
    <?php } ?>
</table>
</code>

回复内容:

html标签中使用php变量,提示:Undefined index: uid in /var/www//list.php
,list.php的具体代码如下:

<code><?php require 'redis.php';
    for ($i=0; $i <= ($redis->get("userid")) ; $i++)
    {
        $data[] = $redis->hgetall("user:".$i);
    }
    /* var_dump($data) 的结果如下:
    array (size=3)
  0 => 
    array (size=0)
      empty
  1 => 
    array (size=4)
      'uid' => string '1' (length=1)
      'username' => string 'jjj' (length=3)
      'password' => string '123' (length=3)
      'age' => string '20' (length=2)
  2 => 
    array (size=4)
      'uid' => string '2' (length=1)
      'username' => string 'lamp' (length=4)
      'password' => string '123' (length=3)
      'age' => string '20' (length=2)
      */
?>

<table border="1">
    <caption>user list</caption>
    <tr>
        <th>uid</th>
        <th>username</th>
        <th>age</th>
    </tr>
    <?php foreach ($data as $v) {?>

    <tr>
        <td><?php echo $v['uid'] ?></td>
        <td><?php echo $v['username'] ?></td>
        <td><?php echo $v['age'] ?></td>
    </tr>
    <?php } ?>
</table>
</code>

echo 前 isset 下不就可以了吗

你的数组第第一个子元素为空啊找不到uid

php很多时候,很需要注意的一点就是要做好容错机制

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn