Jump to content

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
https://forums.phpfreaks.com/topic/272505-loop/
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
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402104
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
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402169
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>";
}

Link to comment
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402171
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
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402173
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402176
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402177
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
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402185
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
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402206
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/272505-loop/#findComment-1402255
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.