Jump to content

silphium

New Members
  • Posts

    5
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

silphium's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. How do I access objects inside an array of database results? I have a method that returns MySQL results as an array "$projects". Running print_r($projects) gives me this: array(1) ( "projects" => object Database_MySQL_Result(6) { protected _internal_row => integer 0 protected _query => string(875) "SELECT [skipping remainder of long query]" protected _result => resource(mysql result) protected _total_rows => integer 53 protected _current_row => integer 0 protected _as_object => string(13) "Model_Project" } ) If I do this: foreach($projects as $project) { echo $project->PROJECT_NAME; } I get "Undefined property: Database_MySQL_Result::$PROJECT_NAME" If I do this: foreach($projects as $project) { echo $project[0]; } the browser will display projects.id for the first returned row *only* echo $project[1] returns the project.id for the second row only. And so on. Each returned row contains over a dozen cells. It's almost as if I'm referring to the array incorrectly, or referring to the wrong array. I feel as though I'm making a very simple (and perhaps dumb) mistake here, but can't quite figure out what it is. Thanks.
  2. Ok, that's an ugly subject. I don't know what else to call this. I'm doing a fairly simple app for an online business student contest. All of the contest data is in MySQL: entries, prizes categories, etc. On one page I need to display past winners for each of four prize categories. A category can have more than one winner. I first run a query to obtain the name of each prize category. No problem displaying these: foreach ($prizes as $pr) { echo '<pre>', $pr->name ,'</pre>'; } I then do a foreach loop to iterate through each category, and run a query ($projects) to find the winning entries for each category within a given year. Again, no problem -- all the queries are being run correctly. But I can't seem to retrieve and display the results of these $projects queries. If I do this: foreach ($prizes as $pr) { echo '<pre>', $pr->name ,'</pre>'; foreach($projects as $p) { echo '<pre>', $p->project_title ,'</pre>'; } } All I get is the same title for all prize categories -- from the last query. This seems like an issue of how to correctly iterate through the $projects queries and assign the results of each with the correct prize category. I used to do this sort of thing a lot when I did procedural PHP with PHP 4. But this is OOP in PHP 5, and I'm using a framework (Kohana). I know these are objects, maybe even iterator objects, and not the kinds of arrays I used to deal with. But I can't get my head around how to work with them in a situation like this. I'm sure that there's a simple answer. Can anyone prod me in the right direction? Thanks.
  3. Thank you Barand -- this solution works great. I have another question about dealing with multidimensional arrays (about which I'm feeling especially dense at the moment). Using the above scenario, how do you create (or extract) arrays of entire columns regardless of value? In other words, an array $foo that contains all of the values from the entire third column? Again, a second MySQL query would be easy, but I'm trying to get my brain wrapped around how to work with multidimensional arrays in PHP. I'll bet once I see see the answer, it will be so simple that I'll kick myself for not figuring it out on my own. ;-) FWIW, once I have array $foo, I'll want to apply array_unique(), sort(), and similar functions. thanks, ~s
  4. thank you, Barand. That's exactly what I was looking for -- almost. I neglected to mention that I don't want the keys preserved, which array_filter() does (sorry, my fault). I do have a function that resets the keys, so it's not a huge deal. But out of curiosity, is there a built-in function similar to array_filter() that does not preserve the keys? As for MySQL queries vs. PHP, generally I prefer to minimize the number of queries. Thanks again, ~s
  5. Data retrieved from MySQL sits in a multidimensional array: $categories. I want to loop through $categories and process records where the value in the third column is "foo". Then I want to do the same where the value in the third column is "bar". So I need to create two new arrays, $foo and $bar. But I can't seem to figure out how to do this, except by using multiple MySQL queries. It must be do-able in PHP without these extra queries (some kind of nested looping, I suppose). I've looked at functions like array_filter, but they require the first argument to be an array, but since I'm dealing with a multidimensional array, I feel like I'm stuck. Anyone? Any help greatly appreciated. ~s
×
×
  • 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.