Need help with PHP and $node arrays

blogjunkie - January 11, 2008 - 00:37

I want to loop through $node->attachments and print out selected data into an unordered list. Presently I'm using this really bad solution:

<ul>
<?php
for ($row = 0; $row < 3; $row++) {
echo "<li>".$node->attachments[$row]['title']."</li>";
}
?>
</ul>

Please someone show me a better way. Thanks.

Try this...

zeta ζ - January 11, 2008 - 03:09

&lt;ul>
<?php foreach ($node->attachments as $atm) : ?>
  &lt;li><?php print($atm['title']); ?>&lt;/li>
<?php endforeach; ?>
&lt;/ul>

yay!

blogjunkie - January 11, 2008 - 03:43

it worked, thanks!

You cannot just print user

Heine - January 11, 2008 - 06:43

You cannot just print user input verbatim as this may easily lead to cross site scripting vulnerabilities.

See http://drupal.org/writing-secure-code and http://drupal.org/node/28984

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

thanks for the warning

blogjunkie - January 11, 2008 - 08:44

thanks for the warning heine, but as you can see from my question I'm a PHP noob. i wouldn't know how to code securely if my life depended on it.

in any case, i figured out how to do it by overiding some theme functions. thanks

But?

Heine - January 12, 2008 - 08:49

thanks for the warning heine, but as you can see from my question I'm a PHP noob. i wouldn't know how to code securely if my life depended on it.

Why the but? I'm not accusing you of anything.

I assumed you

  1. are able to read
  2. posted here to learn something

The pages on http://drupal.org/writing-secure-code were written* as part of a security education effort with the intent to inform all developers about security issues and — as a result — decrease the amount of vulnerabilities in both Drupal core and contributed modules.

We are very interested to receive feedback on these pages. Are they clear, too difficult? What can be done to make the info easier to digest (without dumbing down)?

Another part of the education effort is to increase awareness by posting "Remember to do x,y, see the security guide, [link here]"-comments on forum posts containing snippets and informing the wider Drupal community of issues via Planet Drupal.

That said; Enjoy learning PHP & webdevelopment :)

Tip: http://www.php.net/manual/en/langref.php is rather good.

* some pages are still in progress.
--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

 
 

Drupal is a registered trademark of Dries Buytaert.