Welcome Guest, Not a member yet? Register   Sign In
Is this possible with CI AR?
#1

[eluser]mistycabal[/eluser]
I am planning to build an application and am trying to decide whether to use CI or just skip frameworks all together.

I have been playing around with CI and found one thing I'm really stuck on figuring out how to do with the way things are organized in the database results (and I'm not a fan of active record so that could have something to do with it).

Over in a controller I have:

Code:
$data['products_query'] = $this->db->get('products');

And in a view I have:

Code:
<?php foreach($products_query->result() as $product): ?>
<p>Name: &lt;?php echo $product->product_name; ?&gt;</p>
<p>Details: &lt;?php echo $product->product_description; ?&gt;</p>
&lt;?php endforeach; ?&gt;

I want to be able to do something like this:

Code:
&lt;?php foreach($products_query->result() as $product): ?&gt;
<p>Name: &lt;?php echo $product_name; ?&gt;</p>
<p>Details: &lt;?php echo $product_description; ?&gt;</p>
&lt;?php endforeach; ?&gt;

For the life of me, I can't figure out how I would do this. Everything I try so far isn't working. I'm obviously missing the boat on how to make this work if it's easily doable.

Anyone know how?
#2

[eluser]kgill[/eluser]
You aren't going to be able to do that no matter what you use, even plain PHP. You can't do a foreach on a result set, store the row in $product and then magically make those values appear in a completely new variable called $product_name. Secondly if you don't like AR don't use it - that's the beauty of CI, it's very light on what restrictions it places on you. I can't stand AR either so it & the database class were the first thing I ditched, I use PDO in all my apps.
#3

[eluser]alboyd[/eluser]
Yeh I don't understand why people would use AR either. I just use plain old SQL queries directly using $this->db->query($query_str, array(values_passed_into_query))
Code:
&lt;?php foreach($products_query->result() as $product):
extract($product); ?&gt;
<p>Name: &lt;?php echo $product_name; ?&gt;</p>
<p>Details: &lt;?php echo $product_description; ?&gt;</p>
&lt;?php endforeach; ?&gt;

That should do it I'd guess?
#4

[eluser]mistycabal[/eluser]
I wasn't very clear it seems. Basically, I was just trying to show an example of how I'd like to be able to have the fields be called in the view. They could be variables or functions, don't care. It probably would've been more clear if I had just shown you the two paragraph sections instead of including the foreach that would have to go as well.

The ease of theme creation on web designers is high priority on this project, so think.. WordPress in terms of how you include functions in the theme files/views. That's what I'm wondering if is possible in a simple way with the built in functionality of CI. I can live with the AR if there is some way to make the views very designer friendly.
#5

[eluser]mistycabal[/eluser]
[quote author="alboyd" date="1250932480"]Yeh I don't understand why people would use AR either. I just use plain old SQL queries directly using $this->db->query($query_str, array(values_passed_into_query))
Code:
&lt;?php foreach($products_query->result() as $product):
extract($product); ?&gt;
<p>Name: &lt;?php echo $product_name; ?&gt;</p>
<p>Details: &lt;?php echo $product_description; ?&gt;</p>
&lt;?php endforeach; ?&gt;

That should do it I'd guess?[/quote]
I'll try and see if that works Smile




Theme © iAndrew 2016 - Forum software by © MyBB