Jump to content

Archived

This topic is now archived and is closed to further replies.

pinguïn007

inheriting classes and foreach loops

Recommended Posts

[!--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?

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

[!--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!!!

Share this post


Link to post
Share on other sites

[!--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!

Share this post


Link to post
Share on other sites
[!--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!

302640[/snapback]

 

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.