SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing default values for argument of type array

    Hello everyone,

    Any suggestions as to how to streamline this? Feels like I shouldn't need the extra $title etc. variables.

    ==

    Code PHP:
    <?php
     
    $variables['title'] = 'default title';
    $variables['content'] = 'default content';
    $variables['id'] = 'default id';
     
    function load($variables) {
    	$title = $variables['title'];
    	$content = $variables['content'];
    	$id = $variables['id'];
    	include 'index.html.php';
    	exit();
    }
     
    $page = 'home';
    if (!empty($_GET)) {
    	$getArray =  array_keys($_GET);
    	$page = $getArray[0];
    }
     
    switch ($page) {
     
    	case 'home':
    	$variables['title'] = 'home';
    	$variables['content'] = 'home' . '.php';
    	$variables['id'] = 'home';
    	load($variables);
    	break;
     
    	case 'about':
    	$variables['title'] = 'about';
    	$variables['content'] = 'about' . '.php';
    	$variables['id'] = 'about';
    	load($variables);
    	break;
     
    	#DEFAULT#
    	
    	default:
    	$variables['title'] = 'error';
    	$variables['content'] = 'error' . '.php';
    	$variables['id'] = 'error';
    	load($variables);
    	exit();
    }
     
    	/*
    		'title' => 'default title'
    	,	'content' => 'default content'
    	,	'id' => 'default id'
    	*/

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,089
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    foreach($variables as $key=>$value) {
        $$key = $value
    }
    Last edited by K. Wolfe; Feb 4, 2013 at 14:56. Reason: missed a $

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So, taking a url query string such as this: ?action=home

    PHP Code:
    //spoof a GET request for now:
    $_GET['action'] = 'home';

    // create a white list
    $permitted = array('home''about''error');

    // set some defaults:
    $variables['title'] = 'default title';
    $variables['content'] = 'default content';
    $variables['id'] = 'default id';

    if( 
    in_array($_GET['action'], $permitted) ){

    // set up some vars if they are in your white list $permitted

        
    $variables['title'] = $_GET['action'];
        
    $variables['content'] = $_GET['action'] . '.php';
        
    $variables['id'] = $_GET['action'];

    }

    var_dump$variables ); 
    I mean your example is simplistic, so the solution is also simplistic.

    In the real world are you not more likely to be dealing with output which is more akin to:
    PHP Code:
        $variables['title'] = "Welcome to the home page";
        
    $variables['content'] = 'home.php';
        
    $variables['id'] = 23

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you.

    However, if I put the foreach loop into the function, the default values automatically overwrite anything I have specified below.

    What I am looking for is a way to load the index.html.php page: this is why I want to pass the variables as an array.

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,089
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trilefile View Post
    Thank you.

    However, if I put the foreach loop into the function, the default values automatically overwrite anything I have specified below.

    What I am looking for is a way to load the index.html.php page: this is why I want to pass the variables as an array.
    Then create a whitelist before looping

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind. In my index.html.php, I was referring to stuff as $title instead of $variables['title']. Duh! Sorry to bug you.

    I think I got it figured out. I'll post it here once I'm finished in case it is of any help to anyone down the road.

    Thanks again :-)

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I came up with in the end. Seems to work ok.

    Code PHP:
    <?php
     
    $variables['title'] = 'default title';
    $variables['content'] = 'default content';
    $variables['id'] = 'default id';
     
    function load(array $variables) {
    	include 'index.html.php';
    	return;
    }
     
    $page = 'home';
    if (!empty($_GET)) {
    	$getArray =  array_keys($_GET);
    	$page = $getArray[0];
    }
     
    switch ($page) {
     
    	case 'home':
    	$variables['title'] = 'home';
    	$variables['content'] = 'home' . '.php';
    	$variables['id'] = 'home';
    	break;
     
    	case 'about':
    	$variables['title'] = 'about';
    	$variables['content'] = 'about' . '.php';
    	$variables['id'] = 'about';
    	break;
     
    	#DEFAULT#
    	
    	default:
    	$variables['title'] = 'error';
    	$variables['content'] = 'error' . '.php';
    	$variables['id'] = 'error';
    }
     
    load($variables);

    And the index.html.php:

    Code PHP:
    title is <?php echo $variables['title'];?> <br />
    content is  <?php echo $variables['content'];?> <br />
    id is <?php echo $variables['id'];?> <br />

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    57 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trilefile View Post
    Code PHP:
    <?php
     
    function load($variables) {
    	$title = $variables['title'];
    	$content = $variables['content'];
    	$id = $variables['id'];
    	include 'index.html.php';
    	exit();
    }
    Be aware that include resolves in the scope of that function - its code will not be able to see any variables that were set in the global scope.

  9. #9
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,089
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    So why are you statically calling page title, etc based on the request? This makes for messy code. Why not set up a simple cms?

    After setting up a simple .htaccess file:

    MySQL Table:
    Code:
    tbl_cms
    ---------
    page_slug --page url from root, PK
    page_title
    page_keywords
    page_content
    Index.php
    Code PHP:
    $pageRequest = $_SERVER['REQUEST_URI'];
     
    $dbs = new PDO('mysql:host=127.0.0.1;dbname=dbname', 'username', 'password');
     
    $statement = $dbs->prepare("select * from tbl_cms where page_slug = '$pageRequest'");
    $statement->execute();
    $result = $statement->fetchAll();
     
    if(count($result) == 1) {
    //pull vars from $result
    } else {
    //throw a 404
    }

    Over simplified of course, but makes for less, cleaner and more extendable code.

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Be aware that include resolves in the scope of that function - its code will not be able to see any variables that were set in the global scope.
    Ok, sure thing!

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you -- that's great advice!

  12. #12
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,089
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    BTW, i gave a bad example of PDO, make sure you use parameter binding to avoid sql injection.

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it. Thanks a million!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •