How best to print an array to table?

  • Follow


Ok so here's the quick and dirt simple code:

<?php

  $Alist=array (

	"cover"=>"images/A/A001.jpg",
	"aname"=>"The A Team",
	"bname"=>"An Empty Glass",
	"artist"=>"SSgt Barry Sadler",
	"avideo"=>"A",
	"bvideo"=>"b",
	"label1"=>"[US] RCA Victor",
	"label2"=>"UK] RCA",
	"adate"=>"4-23-66"
);

print "<table border='1'>";
print "<tr>";
print "<td>hello</td>";
print "</tr>";
print "</table>";

?>

I want to replace "hello" with $Alist "aname".
How is that done correctly in this instance?
0
Reply member7172 (119) 5/24/2012 10:28:27 PM

..oO(richard)

>Ok so here's the quick and dirt simple code:
>
><?php
>
>  $Alist=array (
>
>	"cover"=>"images/A/A001.jpg",
>	"aname"=>"The A Team",
>	"bname"=>"An Empty Glass",
>	"artist"=>"SSgt Barry Sadler",
>	"avideo"=>"A",
>	"bvideo"=>"b",
>	"label1"=>"[US] RCA Victor",
>	"label2"=>"UK] RCA",
>	"adate"=>"4-23-66"
>);
>
>print "<table border='1'>";
>print "<tr>";
>print "<td>hello</td>";
>print "</tr>";
>print "</table>";
>
>?>
>
>I want to replace "hello" with $Alist "aname".
>How is that done correctly in this instance?

print "<td>{$Alist['aname']}</td>";

Curly syntax
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

Micha

-- 
http://mfesser.de/blickwinkel
0
Reply netizen9200 (1825) 5/24/2012 10:51:24 PM


On Fri, 25 May 2012 00:51:24 +0200, Michael Fesser wrote:

> .oO(richard)
> 
>>Ok so here's the quick and dirt simple code:
>>
>><?php
>>
>>  $Alist=array (
>>
>>	"cover"=>"images/A/A001.jpg",
>>	"aname"=>"The A Team",
>>	"bname"=>"An Empty Glass",
>>	"artist"=>"SSgt Barry Sadler",
>>	"avideo"=>"A",
>>	"bvideo"=>"b",
>>	"label1"=>"[US] RCA Victor",
>>	"label2"=>"UK] RCA",
>>	"adate"=>"4-23-66"
>>);
>>
>>print "<table border='1'>";
>>print "<tr>";
>>print "<td>hello</td>";
>>print "</tr>";
>>print "</table>";
>>
>>?>
>>
>>I want to replace "hello" with $Alist "aname".
>>How is that done correctly in this instance?
> 
> print "<td>{$Alist['aname']}</td>";
> 
> Curly syntax
> http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
> 
> Micha

thanks
0
Reply member7172 (119) 5/25/2012 1:44:19 AM

On Fri, 25 May 2012 00:51:24 +0200, Michael Fesser wrote:

> .oO(richard)
> 
>>Ok so here's the quick and dirt simple code:
>>
>><?php
>>
>>  $Alist=array (
>>
>>	"cover"=>"images/A/A001.jpg",
>>	"aname"=>"The A Team",
>>	"bname"=>"An Empty Glass",
>>	"artist"=>"SSgt Barry Sadler",
>>	"avideo"=>"A",
>>	"bvideo"=>"b",
>>	"label1"=>"[US] RCA Victor",
>>	"label2"=>"UK] RCA",
>>	"adate"=>"4-23-66"
>>);
>>
>>print "<table border='1'>";
>>print "<tr>";
>>print "<td>hello</td>";
>>print "</tr>";
>>print "</table>";
>>
>>?>
>>
>>I want to replace "hello" with $Alist "aname".
>>How is that done correctly in this instance?
> 
> print "<td>{$Alist['aname']}</td>";
> 
> Curly syntax
> http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
> 
> Micha

Does not work in this case.
www.1littleworld.net/sample/test2.php
0
Reply member7172 (119) 5/25/2012 2:19:21 AM

On Thu, 24 May 2012 18:28:27 -0400, richard wrote:

> Ok so here's the quick and dirt simple code:
> 
> <?php
> 
>   $Alist=array (
> 
> 	"cover"=>"images/A/A001.jpg",
> 	"aname"=>"The A Team",
> 	"bname"=>"An Empty Glass",
> 	"artist"=>"SSgt Barry Sadler",
> 	"avideo"=>"A",
> 	"bvideo"=>"b",
> 	"label1"=>"[US] RCA Victor",
> 	"label2"=>"UK] RCA",
> 	"adate"=>"4-23-66"
> );
> 
> print "<table border='1'>";
> print "<tr>";
> print "<td>hello</td>";
> print "</tr>";
> print "</table>";
> 
> ?>
> 
> I want to replace "hello" with $Alist "aname".
> How is that done correctly in this instance?

I think I've found the way to do this.

http://1littleworld.net/sample/test2a.php
0
Reply member7172 (119) 5/25/2012 3:27:55 AM

richard <member@newsguy.com> wrote in news:1rna3htrhoisg.1ptae0yem1wvk$.dlg@
40tude.net:

[...]
>> 
>> I want to replace "hello" with $Alist "aname".
>> How is that done correctly in this instance?
> 
> I think I've found the way to do this.
> 
> http://1littleworld.net/sample/test2a.php

You *do* realize that none of us can see your php code, don't you?

0
Reply doug_at_milmac_dot_com (115) 5/25/2012 2:09:23 PM

On Thu, 24 May 2012 22:19:21 -0400, richard wrote:

> On Fri, 25 May 2012 00:51:24 +0200, Michael Fesser wrote:

>> print "<td>{$Alist['aname']}</td>";

> Does not work in this case.
> www.1littleworld.net/sample/test2.php

Perhaps you could show us the php you used, rather than link us to a 
website that only shows us the results of that php.

The code suggested will work if you type it correctly. If you don't type 
it correctly, eg miss out or change bits, it won't work.

Note for example, that:

The normal use may be $array["key"] or $array['key']

To be used inside a string delimited with "" needs the use of of '' for 
the array key:

$array['key']

the whole $array['key'] has to be enclosed in the curly brackets:

{$array['key']}

Then it can be included in a string that is enclosed in double quotes:

print "{$array['key']}";

which can also include other text:

print "<td>{$array['key']}</td>";

Which bit did you fuck up? No $ symbol? Didn't use the curly brackets? 
Put the curly brackets in the wrong places? Used the same quotes for the 
array key as the string? Got the array name or key name wrong?

Here's an example of it working:

http://www.sined.co.uk/tmp/array_print.php

Using this php (irrelevant bits snipped):

$array = array( "cold"=>"penguin" );

$content .= "<p>The value of \"{\$array['cold']}\" is: \"{$array['cold']}
\"</p>";

Rgds

Denis McMahon
0
Reply denismfmcmahon (354) 5/25/2012 4:24:12 PM

On Fri, 25 May 2012 14:09:23 +0000, Doug Miller wrote:

> richard <member@newsguy.com> wrote in
> news:1rna3htrhoisg.1ptae0yem1wvk$.dlg@ 40tude.net:
> 
> [...]
>>> 
>>> I want to replace "hello" with $Alist "aname". How is that done
>>> correctly in this instance?
>> 
>> I think I've found the way to do this.
>> 
>> http://1littleworld.net/sample/test2a.php
> 
> You *do* realize that none of us can see your php code, don't you?

No he doesn't. He thinks that we see all his php, and not just the result 
of that php. 

This is because he once opened a php file locally in his browser instead 
of using the url for his server, and thus saw the raw php.

He then decided that the server sends the php to the browser, and despite 
being told many times that he is wrong about this, has never accepted it.

I note that he is claiming that Micha's code was wrong, which it wasn't, 
so he obviously messed up the application of Micha's example, but he 
won't admit that, he'll just assert that Micha's example was wrong (It's 
not, I've used it here <http://www.sined.co.uk/tmp/micha.php>)  by 
substituting Micha's code into his own table and that he has invented his 
own solution instead.

Rgds

Denis McMahon
0
Reply denismfmcmahon (354) 5/25/2012 4:33:56 PM

On May 25, 3:09=A0pm, Doug Miller <doug_at_milmac_dot_...@example.com>
wrote:
> richard <mem...@newsguy.com> wrote in news:1rna3htrhoisg.1ptae0yem1wvk$.d=
lg@
> 40tude.net:
>
> [...]
>
>
>
> >> I want to replace "hello" with $Alist "aname".
> >> How is that done correctly in this instance?
>
> > I think I've found the way to do this.
>
> >http://1littleworld.net/sample/test2a.php
>
> You *do* realize that none of us can see your php code, don't you?

Of course he doesn't. This is RtS after all. Soon he willtell you that
it is your fault that yo cannot see his code, because you did it wrong.
0
Reply paul_lautman (2110) 5/30/2012 11:49:36 AM

8 Replies
37 Views

(page loaded in 0.437 seconds)


Reply: