Newspire
Newspire

Reputation: 104

foreach Loop Not Working With Object

I have an object called entry. I want to access a MySQL database and pull the relevant information for the object entry. These are my pages:

objects.php

class main {
    function setValues( $rs = array() ){
        foreach( $rs as $key => $val ){
            $this->$key = $val ;
        }
    }

    var $created ;
    var $updated ;
}

class entry extends main {
    var $id ;
    var $channel ;

    var $title ;
    var $content ;
}
$table_names['entry'] = 'archive' ;

functions.php

$dbconn = mysqli_connect( DB_HOST , DB_USER , DB_PASS , DB_NAME ) ;
$table_names = array() ;

function getTableValues( $object ){
    global $table_names ;
    $table_name = $table_names[ get_class( $object ) ] ;

    $r = 'SELECT * FROM '.$table_name ;
    return $r ;
}

function query( $q ){
    global $dbconn ;
    $stmt = mysqli_query( $dbconn , $q ) ;

    return $stmt ;
}

function fetchRow( $stmt ){
    return mysqli_fetch_array( $stmt , MYSQLI_ASSOC ) ;
}

function find( $object , $other = '' , $orderBy = '' ){
    global $dbconn ;
    $list = array() ;
    $q = getTableValues( $object ) ;

    $q .= $other.' '.$orderBy ;     
    $stmt = query( $q ) ;
    $objectClass = get_class( $object ) ;
    while( $rv = fetchRow( $stmt ) ){
        $obj = new $objectClass() ;
        $obj->setValues( $rv ) ;
        $list = $obj ;      
    }

    return $list ;
}

Then, on the actual page I have

<?php
    require_once( '../lib/functions.php' ) ;    
    require_once( '../lib/objects.php' ) ;

    $entries = find( new entry() ) ;
?>

<?php
    foreach($entries as $entry){
?>      
    <tr>
        <td><?php echo $entry->id ?></td>
        <td><?php echo $entry->channel ?></td>
        <td><?php echo $entry->title ?></td>
        <td><a href='' class='button'>Edit</a>&nbsp;<a href='' class='button'>View</a></td>
    </tr>
<?php
    }    
?>

I end up getting the error:

Trying to get property of a non-object

as a result of the foreach loop and it iterates this eight times even though right now there's only one entry for testing purposes. Using the foreach loop with only $entries, it properly pulled information but, again, iterated eight times. I'm pretty sure this is because there are eight columns in the SQL table, but I don't know why it would be doing this.

I also used used echo to make sure the SQL statements were correct, used count to make sure there was only one item in the arrays that were a result of the executed functions (there was) and used print_r() to print out the information stored in $entries (it was all correct). So, it seems to me that the reason this isn't working is because of the foreach loop. But I do not know why this is or how to fix it.

Any and all help is appreciated. Thank you!

Upvotes: 0

Views: 546

Answers (1)

Apul Gupta
Apul Gupta

Reputation: 3034

Just change your find function to:

function find( $object , $other = '' , $orderBy = '' ){
    global $dbconn ;
    $list = array() ;
    $q = getTableValues( $object ) ;

    $q .= $other.' '.$orderBy ;     
    $stmt = query( $q ) ;
    $objectClass = get_class( $object ) ;
    while( $rv = fetchRow( $stmt ) ){
        $obj = new $objectClass() ;
        $obj->setValues( $rv ) ;
        $list[] = $obj ;  // earlier you were looping through columns, now it will loop through rows      
    }

    return $list ;
}

Upvotes: 1

Related Questions