Newbie Problem with Foreach |
[eluser]Leovenous[/eluser]
Hello. I'm running a new CI install, so latest version. Be forewarned, I have a lot to learn but I really want to "get it". I have some experience in MCV... its just programming, syntax and OOP that gets me. What I want to accomplish is have my simple 3-field form take information, flash a confirmation, and add their name as the last-submitter. Something is awry. My model (don't laugh) is stealer.php Code: <?php In the above I realized that I don't need the 'cache' table at all. I could snatch the name of the most recent signer from the signer list (duh moment). That's easy enough to change in the model, but I'm not sure how that would play out in the controller. My controller is prerelease.php. Note that "pre" is for the view: pre.php. I'll include only the pertinent functions: Code: function index() In the view, pre.php, before the form I have this: Code: <p><?php echo $this->session->flashdata('notice'); ?></p> Then after the form, where I have my mention of the last signer: Code: <p style="padding-top:12px;">A cheer goes up for our latest signer, <?php foreach ($row as $r) { echo $r->latestu; } ?>!</p> I don't like running a foreach when I know there is only one bit of data, but I don't know how to pass a query as a single variable rather than an array. So from the model onward I treat the results like an array, even though I'm only after one column in one row. The companion question is why isn't the flash message displaying? (I am autoloading session) Feel free to put bogus entries in the form today (November 29th). I'll clear out that data tomorrow. I'd really like to get a grip on this. I'm impressed with CI so far.
[eluser]Michael Wales[/eluser]
Not running through all of your code, just a few tidbits here for you: The standard method of processing a form goes something like this (assume the user will fail, if they don't send them somewhere else): Code: function myForm() { And a quick rewrite of your model to prevent the foreach business: Code: function getLastSignup() {
[eluser]Leovenous[/eluser]
Michael, thanks for that. Couple things... There is a form validation function. I left it out because it's working. On the model, for one thing I'm ditching the 'cache' table and just using the 'interest' table to eliminate redundancy. But I'm still only after the 'fname' column in the most recent row. I'm still grasping how data is passed around. You gave: Code: return $query->row();
[eluser]Michael Wales[/eluser]
It's passed to the controller thanks to the return statement and would be passed to the view within load->view(). Here's a full MVC example: Controller Code: function index() { Model Code: function get_username() { View Code: <b><?php echo $username; ?></b>
[eluser]Leovenous[/eluser]
[quote author="Michael Wales" date="1291077945"] View Code: <b><?php echo $username; ?></b> Tried that. It's throwing me this: Code: A PHP Error was encountered Here is where I'm at. The model is autoloaded: Controller: Code: function index() Model: Code: function getLastSignup() { View (on line 43): Code: <?php echo $fname; ?> I'm trying to understand, does it not like the type of data in the var?
[eluser]dudeami0[/eluser]
What $query->row() returns is an object that has variables with the column names, so something like: Code: $last_signup = $this->stealer->getLastSignup(); Replacing column_name with whatever your first name column is named.
[eluser]Michael Wales[/eluser]
Because you are returning an object (whereas in my example, I was merely returning a string from my model). Rather than give you the answer, I'll show you how to figure it out yourself. In your view: Code: <pre> Take it from there.
[eluser]Leovenous[/eluser]
I'm fine with not getting the outright solution, but don't underestimate my ability to miss the obvious. I tried several things, even went back and reviewed PHP arrays. I keep coming back to: (I changed the 'fname' in the controller to 'stealer' to reduce confusion) Code: <?php echo $stealer['fname']; ?> Buuuut that doesn't work. If $stealer is the array that returns: Code: stdClass Object ( [id] => 30 [time] => 2010-11-29 11:46:15 [fname] => Marc [email] => [email protected] [zipc] => 12345 Then that means [fname] is the key, right? But if I were right it would work. Welcome to my life.
[eluser]dudeami0[/eluser]
stdClass Object != Array Code: <?php echo $stealer->fname; ?>
[eluser]Leovenous[/eluser]
Thanks. I will do more reading on objects. Like I said in my first post, good ole OOP. |
Welcome Guest, Not a member yet? Register Sign In |