Please analyze the following code. This is being used in CodeIgniter framework...

        $results["rows"]=$this->Category_model->getAll();
        $i=0;
        while($i<count($results["rows"]))
        {
            //$parentids[]=$results["rows"][$i]->cat_id;
            $disparray[]=array(
                "cat_id"        =>  $results["rows"][$i]->cat_id,
                "cat_name"    =>    $this->Category_model->fetchcatinfo($results["rows"][$i]->cat_id, "cat_name"),
                "cat_parent_id" =>  $this->Category_model->fetchcatinfo($results["rows"][$i]->cat_id, "cat_parent_id"),
                "cat_desc"    =>    $this->Category_model->fetchcatinfo($results["rows"][$i]->cat_id, "cat_desc"),
                "cat_num_posts" =>  $this->Category_model->fetchnumposts($results["rows"][$i]->cat_id),
                "cat_parent_id_name"=>$this->Category_model->fetchcatinfo($this->Category_model->fetchcatinfo($results["rows"][$i]->cat_id, "cat_parent_id"), "cat_name")
            );
            $array=explode(",",$this->Category_model->fetchallkids($results["rows"][$i]->cat_id,0));
            foreach($array as $item)
            {
                if(strlen($item)>0)
                {
                    $spacecount=substr_count($item,"&nbsp;");
                    $id=str_replace("&nbsp;","",$item);
                    $spaces=array();
                    while($spacecount>=0)
                    {
                        $spaces[]="&nbsp;";
                        $spacecount--;
                    }
                    $disparray[]=array(
                        "cat_id"        =>  $id,
                        "cat_name"    =>    implode($spaces).$this->Category_model->fetchcatinfo($id, "cat_name"),
                        "cat_parent_id" =>  $this->Category_model->fetchcatinfo($id, "cat_parent_id"),
                        "cat_desc"    =>    $this->Category_model->fetchcatinfo($id, "cat_desc"),
                        "cat_num_posts" =>  $this->Category_model->fetchnumposts($id),
                        "cat_parent_id_name"=>$this->Category_model->fetchcatinfo($this->Category_model->fetchcatinfo($id, "cat_parent_id"), "cat_name")
                    );
                }
            }
            $i++;
        }
//      echo "Parent ID : ".$parentids[0]["cat_id"]."<br><br>";

    //  echo "The array being passed to the view part: <br>";
        foreach($disparray as $item)
        {
            $finalarray["row"]=implode("|",$item);
        }
        $this->load->view("category", $finalarray);

In the view section when i try to output the array like print_r($row). It shows only the last element. When i try to display the contents of the $disparray in the controller part, its working fine.

I would like to know if instead of making and transferring $finalarray if i just transfer the $disparray how will i be able to display the contents of the array in the view part? Thanks in advance

Recommended Answers

All 7 Replies

I am not familiar with Codeigniter but it seems that in the last foreach loop you are overwriting the same element ($finalarray["row"]) over and over again:

$finalarray["row"]=implode("|",$item);

I do not know what the end result should be. If it should be string then you should use concatenation. If on the other hand array is expected, you should change the associative index for each iterration or add numeric indices.

thank you for your reply broj1...i had to copy the elements in $disparray to $finalarray so it helps me to display the content in the view section. In codeigniter when i transfer the elements from $finalarray, i can access the elements using $row and display the output as

foreach($row as $item)
{
    echo $item."<br>";
}

I am trying to display the categories of my CMS in the order of its inheritance.

$results["rows"]=$this->Category_model->getAll();

gives me all the parent classes i.e all the categories with parent id = 0;

then

$array=explode(",",$this->Category_model->fetchallkids($results["rows"][$i]->cat_id,0));

fetches and gives me all the subcategories and the subcategories of those subcategories...it could be N number of levels.

the code

$spaces=array(); 
while($spacecount>=0)
{
    $spaces[]=" ";
    $spacecount--;
}

is for adding space to the output as am doing all the manipulation on the controller and the view part will simple serve as an output screen.

Hope i could explain my problem in a much more clearer way!!

blimey... i've solved the problem!! :D All i had to do is $disparray["rows"][] and get it rid of $finalarray for better performance!! Take Care broj1!

I suppose it is time to start learning MVC approach ASAP :-).

So if you need to pass an array of strings over wouldn't it be ok if you build the $finalarray just as numeric array

foreach($disparray as $item)
{
    $finalarray[]=implode("|",$item);
}

and then you can use it as intended? I am not sure if that completely answers your question or solves the problem. Maybe MVC gurus on the form can help.

yeah...i am at newbie at MVC approach and Codeigniter framework is giving me a real good experience in learning it!! no broj1 it doesn't solve the problem a bit. I dont know if it the same in MVC architecture, but in CI the elements in an array situated at the controller part for example $finalarray['rows'] can be accessed on using $rows (according to the example i just gave.) more examples:

$data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );

$this->load->view('blogview', $data);

The view part would look like this :

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <h1><?php echo $heading;?></h1>
    <div>
    <?php echo $message;?>
    </div>
</body>
</html>

You have posted your post about the resolution of the problem just seconds before my post where I was trying (more or less blindly) to suggest how to go about it. Anyway, is your problem resolved now?

Next thing I will do in near future is starting to explore MVC (thanks also to your post). I already started trying Yii some time ago but haven't got far due to lack of time. Maybe I'll carry on with it from where I stopped.

yeah...my problem was solved...!! I would strongly recommend CodeIgniter and Zend! However, i haven't tried out Zend yet...but as a beginner CI is just perfect!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.