Jump to content


Photo

inheriting classes and foreach loops


  • Please log in to reply
4 replies to this topic

#1 pinguïn007

pinguïn007
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 October 2005 - 01:01 PM

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
[span style=\"color:#0000BB\"]<?php
[/span][span style=\"color:#007700\"]class [/span][span style=\"color:#0000BB\"]HtmlPage[/span][span style=\"color:#007700\"]{
   [/span][span style=\"color:#0000BB\"]public $styles[/span][span style=\"color:#007700\"];
   [/span][span style=\"color:#0000BB\"]public $scripts[/span][span style=\"color:#007700\"];
   
   [/span][span style=\"color:#0000BB\"]public [/span][span style=\"color:#007700\"]function [/span][span style=\"color:#0000BB\"]__construct[/span][span style=\"color:#007700\"](){
      [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles [/span][span style=\"color:#007700\"]= array( array([/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]=>[/span][span style=\"color:#DD0000\"]\'css/layout.css\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'media\'[/span][span style=\"color:#007700\"]=>[/span][span style=\"color:#DD0000\"]\'screen\'[/span][span style=\"color:#007700\"]));
      [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]scripts [/span][span style=\"color:#007700\"]= array([/span][span style=\"color:#DD0000\"]\'scripts/nav.js\'[/span][span style=\"color:#007700\"]);
      [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]buttons[/span][span style=\"color:#007700\"]=array();
   }
}

class [/span][span style=\"color:#0000BB\"]HtmlSubPage [/span][span style=\"color:#007700\"]extends [/span][span style=\"color:#0000BB\"]HtmlPage[/span][span style=\"color:#007700\"]{
   [/span][span style=\"color:#0000BB\"]private $type[/span][span style=\"color:#007700\"];
   [/span][span style=\"color:#0000BB\"]private $path[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#FF8000\"]//Path to main dir!

   [/span][span style=\"color:#0000BB\"]public [/span][span style=\"color:#007700\"]function [/span][span style=\"color:#0000BB\"]__construct[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$kind[/span][span style=\"color:#007700\"]){
      [/span][span style=\"color:#0000BB\"]parent[/span][span style=\"color:#007700\"]::[/span][span style=\"color:#0000BB\"]__construct[/span][span style=\"color:#007700\"]();
      [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#DD0000\"]\'../\'[/span][span style=\"color:#007700\"];
      foreach([/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles [/span][span style=\"color:#007700\"]as [/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"]){
         [/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]]=[/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]];[/span][span style=\"color:#FF8000\"]//seems to have no effect
         [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\'<!--\'[/span][span style=\"color:#007700\"].[/span][span style=\"color:#DD0000\"]\"\n\"[/span][span style=\"color:#007700\"];
         echo [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]].[/span][span style=\"color:#DD0000\"]\"\n\n\"[/span][span style=\"color:#007700\"];  [/span][span style=\"color:#FF8000\"]//outputs: ../css/layout.css
         
         [/span][span style=\"color:#0000BB\"]print_r[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles[/span][span style=\"color:#007700\"]);                   [/span][span style=\"color:#FF8000\"]//outputs (value): css/layout.css  
         [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\'-->\'[/span][span style=\"color:#007700\"];
      }
}
[/span][span style=\"color:#0000BB\"]?>
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

This is (a party of) a script which is used to create html-pages, and in the constructor from HtmlSubClass I want to adjust the path to some standard files which are the same for any script. (css, javascript,...)

But I have some problems with the last foreach...
Why is'nt the value adjusted? Can anyone help me?

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 04 October 2005 - 01:09 PM

you are only replacing the current $item value, not the actual $this->styles value in your foreach() loop. if you are wanting to make the $this->styles to have the $path on the front, you'll need to do something like this:
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
[span style=\"color:#0000BB\"]<?php
[/span][span style=\"color:#FF8000\"]// within the context of your class:
[/span][span style=\"color:#007700\"]foreach([/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles [/span][span style=\"color:#007700\"]as [/span][span style=\"color:#0000BB\"]$key [/span][span style=\"color:#007700\"]=> [/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"]) [/span][span style=\"color:#FF8000\"]// notice this slight change
[/span][span style=\"color:#007700\"]{
  [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles[/span][span style=\"color:#007700\"][[/span][span style=\"color:#0000BB\"]$key[/span][span style=\"color:#007700\"]] = [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#FF8000\"]// notice the change to this line
  [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\'<!--\'[/span][span style=\"color:#007700\"].[/span][span style=\"color:#DD0000\"]\"\n\"[/span][span style=\"color:#007700\"];
  echo [/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]].[/span][span style=\"color:#DD0000\"]\"\n\n\"[/span][span style=\"color:#007700\"];
  [/span][span style=\"color:#0000BB\"]print_r[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles[/span][span style=\"color:#007700\"]);                    
  echo [/span][span style=\"color:#DD0000\"]\'-->\'[/span][span style=\"color:#007700\"];
}
[/span][span style=\"color:#0000BB\"]?>
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

hope this makes sense, and i hope it helps.

good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 pinguïn007

pinguïn007
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 October 2005 - 01:38 PM

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php
[/span][span style=\"color:#007700\"]foreach([/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles [/span][span style=\"color:#007700\"]as [/span][span style=\"color:#0000BB\"]$key [/span][span style=\"color:#007700\"]=> [/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"])
[/span][span style=\"color:#0000BB\"]   $this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles[/span][span style=\"color:#007700\"][[/span][span style=\"color:#0000BB\"]$key[/span][span style=\"color:#007700\"]][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]]=[/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]];[/span][span style=\"color:#0000BB\"]?>[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]
This is the exact solution...
Because it was an 2D array it wasn't exactly the solution but it certainly put me on the way. Thanks a lot!
Greetz from Belgium!!!

#4 pinguïn007

pinguïn007
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 October 2005 - 01:49 PM

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php
[/span][span style=\"color:#007700\"]foreach([/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]styles [/span][span style=\"color:#007700\"]as &[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"]){
   [/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]]=[/span][span style=\"color:#0000BB\"]$this[/span][span style=\"color:#007700\"]->[/span][span style=\"color:#0000BB\"]path[/span][span style=\"color:#007700\"].[/span][span style=\"color:#0000BB\"]$item[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'href\'[/span][span style=\"color:#007700\"]];
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]
This is even more simplified! Watch the ampersand, just pass 'by reference'.
Thanks again for pointing me in the right direction!

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 04 October 2005 - 02:21 PM

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\\\"color:#0000BB\\\"]<?php
[/span][span style=\\\"color:#007700\\\"]foreach([/span][span style=\\\"color:#0000BB\\\"]$this[/span][span style=\\\"color:#007700\\\"]->[/span][span style=\\\"color:#0000BB\\\"]styles [/span][span style=\\\"color:#007700\\\"]as &[/span][span style=\\\"color:#0000BB\\\"]$item[/span][span style=\\\"color:#007700\\\"]){
   [/span][span style=\\\"color:#0000BB\\\"]$item[/span][span style=\\\"color:#007700\\\"][[/span][span style=\\\"color:#DD0000\\\"]\'href\'[/span][span style=\\\"color:#007700\\\"]]=[/span][span style=\\\"color:#0000BB\\\"]$this[/span][span style=\\\"color:#007700\\\"]->[/span][span style=\\\"color:#0000BB\\\"]path[/span][span style=\\\"color:#007700\\\"].[/span][span style=\\\"color:#0000BB\\\"]$item[/span][span style=\\\"color:#007700\\\"][[/span][span style=\\\"color:#DD0000\\\"]\'href\'[/span][span style=\\\"color:#007700\\\"]];[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]
This is even more simplified! Watch the ampersand, just pass 'by reference'.
Thanks again for pointing me in the right direction!

View Post


good call! i'm still thinking in the PHP4 mindset since that's what i do the bulk of my development in. for PHP5, you've definitely got the hang of referencing.

good luck! i'll mark this one solved
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users