0

OK so I'm quickly making my way through codeigniter...

It seems pretty pleasant and the MVC model seems Okay.

But I have come accross a problem in passing an array to a view and displaying it.

Following the MVC pattern, the database queries are separated into the Model file. [Good.]

The HTML is for the view and the controller kinda does all the trafficing.

So my db code for the controller is as follows:

$this->load->model('Site_model');
 $mya = $this->Site_model->getAll();


   //load the view
   $this->load->view('database',$mya);


    foreach ($mya->result() as $row) {
            echo $row->post_date;
            //echo '<br>';
    }

And when I print the foreach statement within the CONTROLLER it works fine.

But the problem is in the view:

I would think the following would work

<html>
<head>
<title>db web page</title>

</head>



<body>

 <p> <h1>
 <?php 

 foreach ($mya->result() as $row) {
            echo $row->post_date;
            //echo '<br>';
    } 

    ?> 
    </p></h1>

</body>

</html>

Does it fook? It complains about an undefined variable mya?!

But I just passed it that? What am I doing wrong.

Edited by iamthwee

3
Contributors
11
Replies
13
Views
5 Years
Discussion Span
Last Post by Dani
0

You can only pass an array of variables you want access to into the view, and the array indexes become the variable names. For example, if I have

$foo = array('name' => 'Dani', 'alias' => 'cscgal');
$this->load->view('database', $foo);

then, within the view I can do:

My name is <?= $name ?>

If you pass an object into the view, such as $mya, then it works similarly for all of the public variables defined by the class. In other words, if you use an object, the class variables will be turned into array elements.

0

Thanks Dani, but I'm a bit of a newbie... literally just a few hours into it.

What would the code look like for my example.

0

For the controller:

$this->load->model('Site_model');
$mya = $this->Site_model->getAll();

$dates = array();

foreach ($mya->result() as $row) {
    $dates[] = $mya->post_date;
}

$template_variables = array(
    'dates' => $dates,
    'name' => 'Dani'
);

$this->load->view('database', $template_variables);

For the view:

<html>
<head>
    <title>db web page</title>
</head>
<body>

    <p>My name is <?= $name ?></p>

    <?php foreach ($dates AS $date): ?>
        <h1><?= $date ?></h1>
    <?php endforeach; ?>

</body>
</html>

Edited by Dani: OOPS!

0

You're joking!

So I can't pass objects to a view and instead I have to pass an associate array! WTF! What's the point of that? I thought we're going for the DRY (don't repeat yourself mindset here).

This seems likes just a waste of time. In fact, if that is the case I wouldn't even bother with pulling out the query from the model as a sequence of objects but instead just go for an assoc array?

Is this about right?

0

@Dani that worked well just changed the line 7

$mya->post_date;

to $row->post_date

And I think we're bug free now. But still reeling at how stupid the process is to pass an array of objects into a view!

While I'm here are there any good GUI builders for html, and css styling.

Obviously there's dreamweaver but how about some opensource ones. Or am I just being lazy and do I have to code all the html and css by hand.... Ewggggh?!

What do you guys do?

Edited by iamthwee

0

Sorry, it was a typo :)

When deceptikon first started helping me with DaniWeb (without any prior php experience), he was trying to make everything all nice and pretty and object oriented. The very first thing I did was replace all of his objects with associative arrays.

It's NOT just CodeIgniter. PHP in general just plays very nicely with arrays. OOP support is very limited (the concept of an object was first introduced in PHP 4 and PHP 5 has "decent" support for classes, but not great). If you peruse the built-in php functions, you'll find that most of them are meant to manipulate arrays, take arrays as input, spit arrays out, and just about nothing works with objects. They're really just added as an after thought like, "Oh, ya know what, OOP seems to be the thing to do. If we don't have it, we'll be left behind. Let's offer it but not really support it." That's pretty much PHP's official stance on OOP.

0

Bad object support aside, start using PHP Short Tags for your view. PHP was purposefully designed to be a templating system through alternative (or short) syntax. Look how much cleaner my view is than yours. Never use curly braces for code blocks in your view. You'll end up spending half your life trying to figure out what stars and ends where, especially because you're constantly opening and closing php tags. I promise you: It makes it really easy to visualize the HTML :)

I handwrite all of my HTML/CSS. I'm actually a self-proclaimed CSS goddess. :) Never worked with any type of WYSIWYG editor so I can't recommend any, although Dreamweaver I remember used to be the leader in that. Not sure if they still are. Haven't really heard their name come up much recently.

Edited by Dani

0

Controller

$this->load->model('Site_model');
$mya = $this->Site_model->getAll();
//load the view
$data['mya'] = $mya;
$this->load->view('database',$data);

you can use $mya now in your view

foreach($mya as $row){
    // do your code here
}
0

^^Does that work?

I doubt it... In any case I'm just going to pass my array of objects back as just an array from model to controller.
This is easily done in the model view, so overall all is Okayish.

Edited by iamthwee

0

Your above code will work given the following:

Class Foo
{
    public $name;
    public $address;
    public $phone;
}

$mya = new Foo;

And then your view would essentially be:

foreach ($mya AS $row)
{
    // Would loop 3 times
}

The view would have $mya['name'], $mya['address'], $mya['phone']

Like I said, I work directly with arrays. Working with objects is just additional overhead because they get converted to arrays anyways.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.