FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Multidimensional Arrays
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Multidimensional Arrays [message #180409] Mon, 11 February 2013 20:00 Go to next message
Rita Ferreira is currently offline  Rita Ferreira
Messages: 4
Registered: February 2013
Karma: 0
Junior Member
Hi,
I need some help with multidimensional arrays, if possible.

I have 3 classes, one to acces to DB called config, another called NoticiasController and another called Noticias and the index.php.

On index, I'm creating an object of NoticiasController and calling the method listar().
On NoticiasController I have an object of config and there on config I query the DB like this:

SELECT id, noticia FROM NOTICIAS;

On the config I only have a mysql_query command.

Then on NoticiasController I'm trying to manage the data returned by that query, and here's problem :S

I'm doing something like this:

NoticiasController.php

public function listar()
{

require_once "../config.php";
require_once'/Models/Noticia.php';

$a=new config();
$sql="SELECT id_noticia, corpo_noticia FROM Noticias";
$res=$a->listar($sql);

$e=array();

while ($row = mysql_fetch_assoc($res))
{
$temp=new Noticia($row["id_noticia"],$row["corpo_noticia"]);
$e[]=$temp; <------------ here is the problem
?> <br /> <?php
}


return $e;
}

Noticia.php
(…)
public function __construct($Id_Noticia, $Corpo_Noticia)
{
$this->Id_Noticia = $Id_Noticia;
$this->Corpo_Noticia = $Corpo_Noticia;
}


public function getCorpoNoticia(){
return $this->Id_Noticia;
}
(…)


My goal is to insert on $e every object of Noticia for then send it to index and print the values but when I try to do it I got the error "Fatal error: Call to a member function getCorpoNoticia() on a non-object in /NoticiaController ".

Can you help me? I'm having lots of problems working with arrays and I've tried another ways to do this but I have never reach what I want.

Thanks :)
Re: Multidimensional Arrays [message #180414 is a reply to message #180409] Mon, 11 February 2013 22:29 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/11/2013 3:00 PM, Rita Ferreira wrote:
> Hi,
> I need some help with multidimensional arrays, if possible.
>
> I have 3 classes, one to acces to DB called config, another called NoticiasController and another called Noticias and the index.php.
>
> On index, I'm creating an object of NoticiasController and calling the method listar().
> On NoticiasController I have an object of config and there on config I query the DB like this:
>
> SELECT id, noticia FROM NOTICIAS;
>
> On the config I only have a mysql_query command.
>
> Then on NoticiasController I'm trying to manage the data returned by that query, and here's problem :S
>
> I'm doing something like this:
>
> NoticiasController.php
>
> public function listar()
> {
>
> require_once "../config.php";
> require_once'/Models/Noticia.php';
>
> $a=new config();
> $sql="SELECT id_noticia, corpo_noticia FROM Noticias";
> $res=$a->listar($sql);
>
> $e=array();
>
> while ($row = mysql_fetch_assoc($res))
> {
> $temp=new Noticia($row["id_noticia"],$row["corpo_noticia"]);
> $e[]=$temp; <------------ here is the problem
> ?> <br /> <?php
> }
>
>
> return $e;
> }
>
> Noticia.php
> (…)
> public function __construct($Id_Noticia, $Corpo_Noticia)
> {
> $this->Id_Noticia = $Id_Noticia;
> $this->Corpo_Noticia = $Corpo_Noticia;
> }
>
>
> public function getCorpoNoticia(){
> return $this->Id_Noticia;
> }
> (…)
>
>
> My goal is to insert on $e every object of Noticia for then send it to index and print the values but when I try to do it I got the error "Fatal error: Call to a member function getCorpoNoticia() on a non-object in /NoticiaController ".
>
> Can you help me? I'm having lots of problems working with arrays and I've tried another ways to do this but I have never reach what I want.
>
> Thanks :)
>

What's your actual code and where are you getting the error? The error
you indicated will not occur on the like you pointed out.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Multidimensional Arrays [message #180428 is a reply to message #180409] Wed, 13 February 2013 09:58 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Rita Ferreira wrote:

> I need some help with multidimensional arrays, if possible.
>
> I have 3 classes, one to acces to DB called config, another called
> NoticiasController and another called Noticias and the index.php.
>
> On index, I'm creating an object of NoticiasController and calling the
> method listar(). On NoticiasController I have an object of config and
> there on config I query the DB like this:
>
> SELECT id, noticia FROM NOTICIAS;
>
> On the config I only have a mysql_query command.

The mysql extension is deprecated because its functions are inefficient,
unsafe, and not upwards-compatible:

<http://www.php.net/manual/en/function.mysql-query.php>

Use Prepared Statements with mysqli or PDO instead.

> Then on NoticiasController I'm trying to manage the data returned by that
> query, and here's problem :S
>
> I'm doing something like this:
>
> NoticiasController.php
>
> public function listar()
> {
>
> require_once "../config.php";
> require_once'/Models/Noticia.php';

I am surprised that this works. Surely you would not have your PHP code in
the root directory of the filesystem where it does not belong, or in the
document root where it is accessible by everyone?

Do not mix quotes. Do not use double-quotes where single-quotes suffice.

> $a=new config();

Name your variables properly, so that you can see at a glance what their
purpose is. Use white-space to make your code easy to read.

Let your class names start with a capital letter. Use namespaces to
dinstinguish them from built-in ones.

> $sql="SELECT id_noticia, corpo_noticia FROM Noticias";

While this particular query is safe, other queries of the same kind that use
user input are not. Use prepared statements instead.

> $res=$a->listar($sql);
>
> $e=array();
>
> while ($row = mysql_fetch_assoc($res))
> {

mysqli and PDO have fetch-all methods so that you do not have to write loops
anymore:

<http://www.php.net/manual/en/mysqli-result.fetch-all.php>
<http://www.php.net/manual/en/pdostatement.fetchall.php>

On the other hand, there is

<http://www.php.net/manual/en/mysqli-result.fetch-object.php>
<http://www.php.net/manual/en/pdostatement.fetchobject.php>

with which you can convert a result row into an object immediately.

> $temp=new Noticia($row["id_noticia"],$row["corpo_noticia"]);

You should consider passing only the array, and let the constructor use from
it what it needs. The advantage of that is that the signature of the
constructor does not need to change, and you can use a loop to map fitting
array items to object properties.

> $e[]=$temp; <------------ here is the problem

The problem is *definitely* _not_ at this line. It may be after.

> ?> <br />

Unless this is based in XHTML or HTML5, do not use this syntax. In HTML
4.01, the BR element is written <br>.

> <?php
> }
>
>
> return $e;
> }
>
> Noticia.php
> (…)
> public function __construct($Id_Noticia, $Corpo_Noticia)
> {
> $this->Id_Noticia = $Id_Noticia;
> $this->Corpo_Noticia = $Corpo_Noticia;
> }
>
>
> public function getCorpoNoticia(){
> return $this->Id_Noticia;
> }
> (…)
>
>
> My goal is to insert on $e every object of Noticia for then send it to
> index and print the values but when I try to do it I got the error "Fatal
> error: Call to a member function getCorpoNoticia() on a non-object in
> /NoticiaController ".

You need to find the line where getCorpoNoticia() is called to find out what
is going wrong here. It is not in the code that you posted.

Probably that line has …->CorpoNoticia on the right-hand side, and
getCorpoNoticia() is the getter for that property.

Use a debugger like Xdebug, supported by Eclipse PDT, to find out.

> Can you help me? I'm having lots of problems working with arrays and I've
> tried another ways to do this but I have never reach what I want.

In my experience, sloppy coders are also sloppy thinkers and vice-versa.
So I am not surprised.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: P = NP in Binary Portrait
Next Topic: Connection with Sql server on Windows
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 13:34:15 GMT 2024

Total time taken to generate the page: 0.02681 seconds