4 Replies - 752 Views - Last Post: 06 June 2011 - 04:11 AM Rate Topic: -----

#1 SeanKeenan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 74
  • Joined: 23-March 09

Code Separation and more

Posted 06 June 2011 - 02:59 AM

Ok so I looked into getting used to code seperation and so far I love it, it makes for much cleaner code and I can understand the benefits of it in the long run.
However.. I have a problem that someone might be able to help me with, I'm at work so this isn't my actual code but you'll get the basics.

Ok I have the following files for example

Index.php
<?php 
  include 'functions.php';

$p == '1';
switch ($p)
{
  case "1":
    createPage();
  break;
  etc..
}



Page.html
HEADER
  <div id="something">
    NOTE
  </div>



functions.php
function createPage()
{
  $output = file_get_contents("page.html");
  $replace = array("HEADER","NOTE");
  $with = array(include "header.html","Note info");
  $output = str_replace($replace, $with, $output);
  echo $output;
}



Say I do it like that, if I then want NOTE to say Welcome $user how would I get that data into there because I tried calling it on index.php like
$user = 'Sean'
; and include it in my function like a
rray(include "page.html","Note info" . $user)


But it doesn't work like that understandably.. anyone shed some light? Maybe I'm going about this in a bad way, I'm trying!

Is This A Good Question/Topic? 1
  • +

Replies To: Code Separation and more

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Code Separation and more

Posted 06 June 2011 - 03:17 AM

Hey.

You need to get the variable into the createPage() function, either by declaring it globally and using the global keyword, or by passing it as a parameter. (The latter is generally preferred.)

<?php
/** index.php */
include "functions.php";

$user = 'Sean';
createPage($user);


/** functions.php */
function createPage($user) {
    $with = array(include "header.html","Note info" . $user);
}


Was This Post Helpful? 1
  • +
  • -

#3 SeanKeenan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 74
  • Joined: 23-March 09

Re: Code Separation and more

Posted 06 June 2011 - 03:23 AM

View PostAtli, on 06 June 2011 - 11:17 AM, said:

Hey.

You need to get the variable into the createPage() function, either by declaring it globally and using the global keyword, or by passing it as a parameter. (The latter is generally preferred.)

<?php
/** index.php */
include "functions.php";

$user = 'Sean';
createPage($user);


/** functions.php */
function createPage($user) {
    $with = array(include "header.html","Note info" . $user);
}



I've read about that, thanks a lot for bringing it back to my attention.
Can I declare multiple variables?

/* index.php */
$user = 'Sean';
$name = 'Rupert';
createPage($user,$name);

/* Functions.php */
function createPage($user,$name) {
    $with = array(include "header.html","Note info" . $user);
}




Thanks for your extended help!
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Code Separation and more

Posted 06 June 2011 - 03:49 AM

Sure, you can do that.

You can even declare default values, so you don't have to provide a value unless needed. For example:
/** functions.php */

/**
 * Create a page from a template.
 * @param string $templateName OPTIONAL
 * @param string $title OPTIONAL
 * @param array $params OPTIONAL
 * @return type string
 */
function createPage($templateName='index.tpl', $title='My Site', $params=array()) 
{
    // Load the template providided via the function parameter.
    $pageText = file_get_contents($templateName . '.html');
    
    // Replace each of the optional $params key=>value pairs in
    // the page text.
    foreach ($params as $_key => $_value) {
        $pageText = str_replace($_key, $_value, $pageText);
    }
    
    // And finally replace the title, and return the now
    // filled out template page.
    return str_replace('TITLE', $title, $pageText);
}


/** index.php */

// No values provided, just using defaults.
$index = createPage();

// An "About page" using the default title.
$about = createPage('about.tpl');

// A user profile page, using a username and and
// a user ID.
$userData = array('NAME' => 'James', 'ID' => 7);
$userProfile = createPage('user.tpl', 'User Profile - My Site', $userData);


The last parameter $params is a way to pass multiple, non-specific values into a function. Allows us to make a function more flexible by not tying it down to much. - In this case, it allows me to pass a list of key=>value pairs that should be replaced in the template.
Was This Post Helpful? 2
  • +
  • -

#5 SeanKeenan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 74
  • Joined: 23-March 09

Re: Code Separation and more

Posted 06 June 2011 - 04:11 AM

View PostAtli, on 06 June 2011 - 11:49 AM, said:

Sure, you can do that.

You can even declare default values, so you don't have to provide a value unless needed. For example:
/** functions.php */

/**
 * Create a page from a template.
 * @param string $templateName OPTIONAL
 * @param string $title OPTIONAL
 * @param array $params OPTIONAL
 * @return type string
 */
function createPage($templateName='index.tpl', $title='My Site', $params=array()) 
{
    // Load the template providided via the function parameter.
    $pageText = file_get_contents($templateName . '.html');
    
    // Replace each of the optional $params key=>value pairs in
    // the page text.
    foreach ($params as $_key => $_value) {
        $pageText = str_replace($_key, $_value, $pageText);
    }
    
    // And finally replace the title, and return the now
    // filled out template page.
    return str_replace('TITLE', $title, $pageText);
}


/** index.php */

// No values provided, just using defaults.
$index = createPage();

// An "About page" using the default title.
$about = createPage('about.tpl');

// A user profile page, using a username and and
// a user ID.
$userData = array('NAME' => 'James', 'ID' => 7);
$userProfile = createPage('user.tpl', 'User Profile - My Site', $userData);


The last parameter $params is a way to pass multiple, non-specific values into a function. Allows us to make a function more flexible by not tying it down to much. - In this case, it allows me to pass a list of key=>value pairs that should be replaced in the template.



Fantastic, thank you very much. I think I understand these but I'll read through them properly in my break, I think that will be a better solution to what I'm trying to achieve so major thumbsup :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1