Solved

PHP MVC Basics OOP. How to return Array of Object class.

Posted on 2012-03-12
7
557 Views
Last Modified: 2012-03-13
Source code for Model View Controller Tutorial in PHP: http://php-html.net/tutorials/model-view-controller-in-php/

I was trying to replace this:
 
		//return array(
		//	"Jungle Book" => new Article("Jungle Booking", "R. Kipling", "2012-03-01", "A classic book.", "http://www.jungle.com"),
		//	"Moonwalker" => new Article("Moonwalker", "J. Walker", ""),
		//	"PHP for Dummies" => new Article("PHP for Dummies")
		//);

Open in new window


with this:

// Return Array of the objects inside the client's container
$objs = $container->list_objects();

$allArticles=array();

for ($i = count($objs)-1; $i >= 0; $i--) { 

$tempObject = new Article();
$tempObject -> Title=$objs[$i];
$tempObject -> Author="Testing Author";

$allArticles[$i] = $tempObject;

}

return array($allArticles);

Open in new window



But the results are not show. How can I also have the Title of the Article as variable instead of $tempObject.
0
Comment
Question by:glynco
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:glynco
ID: 37709931
I tried this but it still do not show in the articlelist.php

$tempObject = new Article();
$tempObject -> title=$objs[$i];
array_push($articles[get_class($tempObject)], $tempObject);

Open in new window


and here is the articlelist.php

foreach ($articles as $title => $article)
		{
			echo '<tr><td><a href="index.php?article='.$article->title.'">'.$article->title.'</a></td><td>'.$article->author.'</td><td>'.$article->description.'</td></tr>';
		}

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37711972
From the second code snippet at the original post, line 3
$allArticles=array();

Open in new window

and line 16
return array($allArticles);

Open in new window

I would surmise that the method returned an array, containing one element, and that one element was an array containing the contents of the $allArticles array.

You can use var_dump() to print out variables and learn what they are made of.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 37712642
greetings again glynco, , you seem to be trying to learn about making a programming "Structure" for the MVC using OOP as in the Tutorial you gave a web link for.  You are trying to do a substitution for the tutorial  return array, it seems you do not understand some of the core programming methods for Arrays and Objects in PHP.  It looks like you changed the tutorial "Books" naming convention to "Articles" naming for your instance. They have this code -

return array(  
            "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."),  
            "Moonwalker" => new Book("Moonwalker", "J. Walker", ""),  
            "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "")  
        );

Open in new window


Which you seem to want to KNOW how that code works and to have it be useful to you for your Articles Object. However, The tutorial does NOT do a good job of showing you how to use Object storage for Items (books, Articles, etc.) and using a VIEW class to list the storage Items.
You may need to learn about Array and Object methods before you get into MVC programming.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 34

Expert Comment

by:Slick812
ID: 37712646
Oh sorry, I do not have time today, but tomorrow, maybe some php object code for this sort of thing.
0
 

Author Comment

by:glynco
ID: 37715186
foreach ($articles['Article'] as $my_object)
{
    $title = (string)$my_object->title;
    $my_associative_array[$title] = $my_object;
}
0
 

Author Closing Comment

by:glynco
ID: 37715218
Thanks.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37715453
Was there something wrong here?  Why did you mark the grade down to a "B" ?

Here are the EE grading guidelines.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question