Jump to content
Sign in to follow this  
lewashby

constructing array of object and their properties

Recommended Posts

When I load this into the browser I get a bunch of zeros '0' stacked on top of each other on the left side of the screen instead of getting the name and numbers of the individuals. I added this line in one of the files to try and find the problem but as it turns out this line is outputting exactly what I wanted and expected it to output so I don't know why the rest of the program isn't working.

print_r('The $attribute variable = ' . $attribute . '<br />');   <- that's the line I added into the file inmate.php and it shows that the variable $attribute has exactly what it needs to have but when I try and output this information from the test.php file I get the zeros.

 

test.php

<?php


require_once("./database.php");
require_once("./inmate.php");


$inmates = Inmate::get_inmates();


foreach($inmates as $inmate)
{
    print $inmate->inmate_property['first_name'] . " "  . $inmate->inmate_property['last_name'] . " " . $inmate->inmate_property['number']  . "<br />";
}


?>

inmate.php

<?php


require_once("./database.php");


class Inmate
{


//    public $first_name;
//    public $last_name;
//    public $full_name;
//    public $race;
//    public $number;
//    public $facility;
//    public $type_of_transit;


    public $inmate_property = array('first_name' => '', 'last_name' => '', 'full_name' => '', 'race' => '', 'number' => 0, 'facility' => '', 'type_of_transit' => '');


    public function __construct($result)
    {
//        $this->first_name = $result['first_name'];
//        $this->last_name = $result['last_name'];
//        $this->number = $result['number'];
        foreach($result as $attribute)
        {
            print_r('The $attribute variable = ' . $attribute . '<br />');
            $this->inmate_property[$attribute] = $attribute;
        }
    }


    public function get_property($property)
    {
        return $this->$property;
    }


    public function set_property($property, $value)
    {
        $this->$property = $value;
    }


    public static function get_inmates()
    {        
        $connection = get_db_connection();
        $inmates = array();


        $sql = "SELECT * FROM inmate_board";
        $query = $connection->query($sql);


        while($result = $query->fetch(PDO::FETCH_ASSOC))
        {
            array_push($inmates, new Inmate($result));
        }


        return $inmates;
    }
}


?>

I'm trying to setup xdebug for vim so I don't have to post this many questions but I'm having some trouble getting that going.

Edited by lewashby

Share this post


Link to post
Share on other sites

First I would recommend not using static functions. I would also suggest maybe just pulling and displaying the data directly from the database table?

 

For example this is what I did for a small blog/forum that I did for my own website:

    public function read($category = "sysop") {

        $this->sql = 'SELECT id, creator_id, category, sticky, title, content,  date_updated, date_added FROM pages WHERE category=:category ORDER BY date_added DESC';
        try {
            $this->stmt = $this->pdo->prepare($this->sql);
            $this->stmt->execute([':category' => $category]);
            $this->stmt->setFetchMode(PDO::FETCH_OBJ);
            return $this->stmt;
        } catch (Exception $ex) {
            print $ex->getMessage();
        }
    }

Then I just use a view page that I called posts.php

<?php


while ($row = $stmt->fetch()) {
    $dateAdded = new DateTime($row->date_added, new DateTimeZone('America/Detroit'));
    $dateUpdated = new DateTime($row->date_updated, new DateTimeZone('America/Detroit'));
    ?>
    <article class="blogArticle">
        <header>
            <h1><?php echo $row->title; ?></h1>
            <p class="author">by <?php echo $blog->getUsername($row->creator_id); ?> created on <?php echo $dateAdded->format("F j, Y g:i A"); ?> updated on <?php echo $dateUpdated->format("F j, Y g:i A") ?></p>
        </header>

        <hr>
        <p class="blogParagraph"><?php echo nl2br(html_escape($row->content)); ?></p>
        <hr>
        <footer>
            <?php if ($user && ( $user->id === $row->creator_id || $user->security_level === 'sysop')) { ?>
                <a class="edit" href="edit.php?edit=<?php echo urlencode($row->id); ?>">Edit</a><a class="delete" href="delete.php?delete=<?php echo urlencode($row->id); ?>" onclick="return confirm('Are you sure you want to delete this thread?');">Delete</a>
            <?php } ?>
        </footer>
    </article>
<?php }  

that I use for my index.php page or what have you like so

$stmt = $blog->read();

require_once 'lib/includes/header.inc.php';
?>
<div class="container mainPage">
    <?php include 'lib/views/posts.php'; ?>
<?php
require_once 'lib/includes/footer.inc.php';

You can alway build an array as you go about displaying if you have other plans for it. Just a suggestion. 

Edited by Strider64

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.