Jump to content

Loop


designanddev

Recommended Posts

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>";
 }

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
(
)

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

... 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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.