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?)
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) {
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}";
}
This variable $this
is only usable when you are in a non-static method. Otherwise you have to use normal variables. $this
refers always to the current object.
see this: http://www.php.net/manual/en/language.oop5.basic.php
The pseudo-variable $this is available when a 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).
You have two solutions : If your code is inside class, instead of doing
$data['eee']=...
do
$this->data['eee']=...
If you are outside of class (but works also inside), replace
foreach ($this->data as $row) {
with
foreach ($data as $row) {