Re: Passing Array to object
| From: | Chris Sherwood | Date: | Wed, 25 Jun 2003 20:11:57 +0000 |
| Subject: | Re: Passing Array to object | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-152865@lists.php.net to get a copy of this message | ||
function DisplayLinks($links,$names)
{
$namecount = sizeof($names);
for($a=0;$a<$namecount;$a++)
{
$name = $names[$a];
$url = $links[$a];
echo ":::<a href= '$url''>$name</a>:::\n";
} //End for
} // End DisplayLinks function
try this and assigning the names you want to be displayed to the names array
and links to the links array...
I know this isnt pretty but it should work
----- Original Message -----
From: "Daniel J. Rychlik" <drychlik@tcsconsult.com>
To: "Chris Sherwood" <csherwood@nortialearning.com>;
<php-general@lists.php.net>
Sent: Wednesday, June 25, 2003 12:59 PM
Subject: Re: [PHP] Passing Array to object
> I remember seeing somewhere that you could only pass a single deminsional
> Array to a class. But I cant even get this one deminsional to work. If I
> place the links inside the main class and call it an attribute it works
> fine.
>
> var $newlinks = array (...)'
>
> When I var_dump($myarray) I recieve the output as expected. For some
> reason its still giving me the warning. hmmm...... Ive tried everything
> that was suggested. I think I need to go back and take another look at OO
> programming in php.
>
>
> ----- Original Message -----
> From: "Chris Sherwood" <csherwood@nortialearning.com>
> To: "Daniel J. Rychlik" <drychlik@tcsconsult.com>;
> <php-general@lists.php.net>
> Sent: Wednesday, June 25, 2003 1:46 PM
> Subject: Re: [PHP] Passing Array to object
>
>
> > hmmm okay
> > cause list works by assigning elements in order from an array
> > each iterates thru each element in a list so you want to pass both
> elements
> > at the same time so you need to pass an array to get the effect your
> looking
> > for
> >
> > ie $name being from the first array element, and $url being from the
> second
> > array element...
> >
> > so I am thinking you wish to assign urls and names to a single array you
> > will want to make it a multidimensional array
> > ie array (array(name, link),array(name,link))
> >
> > hope this helps you daniel
> >
> > ----- Original Message -----
> > From: "Daniel J. Rychlik" <drychlik@tcsconsult.com>
> > To: "Daniel J. Rychlik" <drychlik@tcsconsult.com>; "Chris
> > Sherwood"
> > <csherwood@nortialearning.com>; <php-general@lists.php.net>
> > Sent: Wednesday, June 25, 2003 11:17 AM
> > Subject: Re: [PHP] Passing Array to object
> >
> >
> > > Its erroring at my display links function. This function is inside my
> > main
> > > class.
> > >
> > > function DisplayLinks($links) {
> > >
> > > while(list($name, $url) = each($links) {
> > >
> > > echo ":::<a href=
> > > '$url''>$name</a>:::\n";
> > >
> > > } //End while
> > >
> > > } // End DisplayLinks function
> > >
> > >
> > > I dont see why it wont work... I even try assigning $links to the
> array.
> > >
> > >
> > > ----- Original Message -----
> > > From: "Daniel J. Rychlik" <drychlik@tcsconsult.com>
> > > To: "Chris Sherwood" <csherwood@nortialearning.com>;
> > > <php-general@lists.php.net>
> > > Sent: Wednesday, June 25, 2003 1:08 PM
> > > Subject: Re: [PHP] Passing Array to object
> > >
> > >
> > > > yea, I gave that a shot, Same message. Logically it makes sense, but
> php
> > > > doesnt like it.
> > > >
> > > > by the way, thanks for your help Chris. You've helped me in the
past.
> > > > -Daniel
> > > >
> > > > ----- Original Message -----
> > > > From: "Chris Sherwood" <csherwood@nortialearning.com>
> > > > To: "Daniel J. Rychlik" <drychlik@tcsconsult.com>;
> > > > <php-general@lists.php.net>
> > > > Sent: Wednesday, June 25, 2003 1:02 PM
> > > > Subject: Re: [PHP] Passing Array to object
> > > >
> > > >
> > > > > Have you tried to assign the array to a variable then pass the
> > variable
> > > to
> > > > > the function ie
> > > > >
> > > > > $index = new main();
> > > > > $yourvar = array('array values');
> > > > >
> > > > >
> > > > > $index->SetLink ($yourvar);
> > > > >
> > > > > ----- Original Message -----
> > > > > From: "Daniel J. Rychlik" <drychlik@tcsconsult.com>
> > > > > To: <php-general@lists.php.net>
> > > > > Sent: Wednesday, June 25, 2003 10:46 AM
> > > > > Subject: [PHP] Passing Array to object
> > > > >
> > > > >
> > > > > Hello,
> > > > >
> > > > > I am developing a site that uses classes to reuse code and
objects.
> > > > >
> > > > > I have in my main class a
> > > > >
> > > > > function SetLinks($newlinks) {
> > > > >
> > > > > $this->links = $newlinks;
> > > > >
> > > > > }
> > > > >
> > > > > And another function that displays them in the same class main {
> > > > >
> > > > >
> > > > > I have another file called index.php that basically just passes
> > > attributes
> > > > > to the main class and the main class displays them.
> > > > >
> > > > > Im having a bit of trouble passing an array to the object
> > > > >
> > > > > in the index.php
> > > > >
> > > > > I create new.
> > > > >
> > > > > $index = new main();
> > > > > $index->SetLink = array ( 'Link1' =>
> > > > > 'link1.php',
> > > > > ..... );
> > > > >
> > > > > When I run I recieve "Variable passed to each() is not an array or
> > > object
> > > > "
> > > > >
> > > > > so I tried this
> > > > >
> > > > > $index->SetLink($newlink = array('link1' => ...);
> > > > >
> > > > > same error message. I was wondering if someone could shed some
> light
> > on
> > > > > what Im doing wrong.
> > > > >
> > > > > Thanks
> > > > > Daniel
> > > > >
> > > >
> > > >
> > > > --
> > > > PHP General Mailing List (http://www.php.net/)
> > > > To unsubscribe, visit:
> > > > http://www.php.net/unsub.php
> > > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> >
>
>