Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8362

Re: Multidimensional Arrays

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeeder.ewetel.de!eweka.nl!lightspeed.eweka.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <11348428.vGI7eJLS1r@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Wed, 13 Feb 2013 10:58:01 +0100
User-Agent KNode/4.8.5
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Multidimensional Arrays
Newsgroups comp.lang.php
References <5635299c-b6a1-4e75-bf8e-1b4f774b2702@googlegroups.com>
MIME-Version 1.0
Lines 138
NNTP-Posting-Date 13 Feb 2013 10:58:09 CET
NNTP-Posting-Host b226724a.newsspool1.arcor-online.net
X-Trace DXC=jDRg9XIgF:F6PJ?[X6JIXEic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@c43BI^Y;R1GDZm8W4\YJNLb@mF9jNikdEWWB88<dh9WEZ1IC=^@8N<A
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.php:8362

Show key headers only | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Multidimensional Arrays Rita Ferreira <ritaferreira83@gmail.com> - 2013-02-11 12:00 -0800
  Re: Multidimensional Arrays Jerry Stuckle <jstucklex@attglobal.net> - 2013-02-11 17:29 -0500
  Re: Multidimensional Arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-13 10:58 +0100

csiph-web