duanbage2161 2012-01-16 13:09
浏览 87

PHP使用foreach和$ this

This is my code for index.php. I know I can't use foreach outside class. I don't know what to use instead, there might be something else missing too. I get the error

Using $this when not in object context

I have a SQL table gallery with 6 attributes: name, thumb, display, original, title and description.

I want to list all rows, how am I going to do this if I can't use foreach outside class in PHP5?

<?php
while (($row = mysql_fetch_object($res)) !== false) {
    $data['name'] = $row->name;
    $data['thumb'] = $row->thumb;
    $data['display'] = $row->display;
    $data['original']= $row->original;
    $data['title']= $row->title;
    $data['description']= $row->description;
foreach ($this->data as $row){
    print "{$row['name']} some HTML {$row['thumb']} some HTML {$row['display']} {$row['original']} some HTML {$row['title']} some HTML {$row['description']} ";
}
?>

I now have this

if (($row = mysql_fetch_assoc($res)) == false) exit;
while (($row = mysql_fetch_assoc($res)) !== false) {
    $data['name'] = $row->name;
    $data['thumb'] = $row->thumb;
    $data['display'] = $row->display;
    $data['original']= $row->original;
    $data['title']= $row->title;
    $data['description']= $row->description;
}

foreach ($data as $row) { print "t {$row['title']}"; }

what I get is 't t t t t t' as if I have 6 empty records (Note that I have 6 attributes so its taking the attributes as rows?)

  • 写回答

3条回答 默认 最新

  • duanqian2368 2012-01-16 13:12
    关注

    Removing some confusion

    You can use a foreach outside a class, but you cannot refer to a class as $this outside itself. The manual describes it thus:

    The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

    Your code should refer to the class using a variable containing an instance:

    $object = new DataAccess();
    foreach($object->data as $row) {
    

    Possible solution in this case

    You can refer to the MySQL result object directly in a string without first assigning it to values in an array - see the code below. If you really want an array then you should be using mysql_fetch_assoc() instead of mysql_fetch_object().

    while (($row = mysql_fetch_object($res)) !== false) {
        print "{$row->name} some HTML {$row->thumb} some HTML {$row->display} {$row->original} some HTML {$row->title} some HTML {$row->description}";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)