designanddev Posted December 29, 2012 Share Posted December 29, 2012 i have this code here but i can not seem to to loop the $text = Category::get_category($photo->id); with the $photo. ive tried working around this but i keep getting all my loop fields saying Array, <?php // Find all the photos $photos = Photograph::find_all(); foreach ($photos as $photos){ $text = Category::get_category($photo->id); echo $photos->caption; echo "</br></br>"; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted December 30, 2012 Share Posted December 30, 2012 foreach ($photos as $photos){ Try "as $photo" and then reference $photo within the loop Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 i have tried this but the category::get_category(2) which i would like to be category::get_category($photo->id) is being set by another class the echo statement outputs either the numbers of (Array) but i want it to be outputting the the content string. i hope you understand i am trying to make sense of all the joins and the instantiation. // Find all the photos $photos = Photograph::find_all(); $text = Category::get_category(2); foreach ($photos as $photos){ echo $photos->caption; echo $photos->category(); echo "</br></br>"; } KInd Regards Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 30, 2012 Share Posted December 30, 2012 You have written photos as photos. Barand said to do photos as photo. Do you see the difference? Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 You have written photos as photos. Barand said to do photos as photo. Do you see the difference? No not quiet sure Quote Link to comment Share on other sites More sharing options...
sowna Posted December 30, 2012 Share Posted December 30, 2012 replace $photos as $photo in the loop, like this foreach ($photos as $photo){ echo $photo->caption; echo $photo->category(); echo "</br></br>"; } Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 replace $photos as $photo in the loop, like this foreach ($photos as $photo){ echo $photo->caption; echo $photo->category(); echo "</br></br>"; } Thanks for the reply i have tried this but the $photo->category(); keeps return Array instead of the information i need inside that Array This is the function inside the $photo->category(); public function category(){ return Category::get_category($this->id); } and this is the Category::get_category function public static function get_category($category_id=0) { global $database; $sql = "SELECT * FROM ".self::$table_name; $sql .= " WHERE id=" .$database->escape($category_id); return self::find_by_sql($sql); } The $photo->category(); keeps returning the Array but what i would like to do is access the the $category_name attribute within the Category:: class which is stored in my database. I hope some understands what i am trying to accomplish here. Kind Regards Quote Link to comment Share on other sites More sharing options...
Barand Posted December 30, 2012 Share Posted December 30, 2012 We need to know what the category() function actually returns. Run this and post results foreach ($photos as $photo){ echo $photo->caption; $catdata = $photo->category(); echo '<pre>'.print_r($catdata, 1).'</pre>'; echo "</br></br>"; } Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 We need to know what the category() function actually returns. Run this and post results foreach ($photos as $photo){ echo $photo->caption; $catdata = $photo->category(); echo '<pre>'.print_r($catdata, 1).'</pre>'; echo "</br></br>"; } Thank You This is what it returned jordan 26 Array ( ) jordans Array ( [0] => Category Object ( [id] => 4 [category_name] => ) ) jordan 8 Array ( ) Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 i want to be able to pull the category name out but it just returns the array at the moment the category name is set as an empty field in the database thats why its blank with the id of 4 Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 Hope this here is more clear for you jordan 26 Array ( [0] => Category Object ( [id] => 1 [category_name] => php ) ) jordans Array ( [0] => Category Object ( [id] => 2 [category_name] => HTML ) ) jordan 8 Array ( [0] => Category Object ( [id] => 1 [category_name] => php ) ) Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 ive tried using this to pull just the category_names.... foreach ($photos as $photo){ echo $photo->caption; $catdata = $photo->category(); echo $catdata->category_name; echo "</br></br>"; } still not working can anyone please help with get around this issue. Kind Regards Quote Link to comment Share on other sites More sharing options...
Barand Posted December 30, 2012 Share Posted December 30, 2012 try echo $catdata[0]->category_name; Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 Thanks Barand ive been up all last night working this out, i understand a little more how this is working now but how could i add this within my class to keep it more clean, i think i need to work on how the returns are working. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 30, 2012 Share Posted December 30, 2012 Instead of returning an array containing a single category object you could just return the object. Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 Instead of returning an array containing a single category object you could just return the object. how would i go about doing this ive added this (return $catdata[0]->category_name;) to my category function but is there any other way of accessing my category name without using this $catdata[0] public function category(){ $catdata = Category::get_category($this->category_id); return $catdata[0]->category_name; } Quote Link to comment Share on other sites More sharing options...
Barand Posted December 30, 2012 Share Posted December 30, 2012 Your find_by_sql() method returns an array so you need to get that first element with the [0] index. Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 30, 2012 Author Share Posted December 30, 2012 Thanks for your help Barand, you've bin great Quote Link to comment Share on other sites More sharing options...
cpd Posted December 30, 2012 Share Posted December 30, 2012 ... is there any other way of accessing my category name without using this $catdata[0] Unfortunately not unless you hack the Category::get_category() method to return the Category object itself; not a particularly good idea though. What's the problem with using the 0 index? Quote Link to comment Share on other sites More sharing options...
designanddev Posted December 31, 2012 Author Share Posted December 31, 2012 Unfortunately not unless you hack the Category::get_category() method to return the Category object itself; not a particularly good idea though. What's the problem with using the 0 index? not a problem at all, just wanted to know if this could be done any other way Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.