SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to require_once() file in the middle of single-quote variable

    I have a variable for template and need to include different menus for different user types. Here's the code:

    PHP Code:
    $template '
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                <html>
                <head>
                <title></title>
                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                <link href="style.css" rel="stylesheet" type="text/css" />
                </head>
                <body>
                <div id="container">
                    <div id="top"><img src="/i/header.gif"></div>
                    <div id="content">
                        <div id="left">
                            <div id="navigation">
        '
    .
                if(
    $_SESSION[type'] == 'Adm'){
                    require_once($_SERVER['
    DOCUMENT_ROOT']."/utility/menu_admin.php");            
                }elseif($_SESSION['
    type'] == 'User'){
                    require_once($_SERVER['
    DOCUMENT_ROOT']."/utility/menu_mem.php");            
                }
        
        .'
                            
    </div>
                        </
    div
    The error is "Parse error: parse error, unexpected T_IF" where the first IF is.

    The code in the menu files is simple HTML code. Any ideas how to include the code without rewriting more of my script that already depends on the require_once includes?

    Thanks

  2. #2
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you left out a ' on line if($_SESSION[type'] == 'Adm'){
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The . operator is to concatenate strings (and other data types will work).
    You can't just just go concatenating an if statement (another programming construct) like that.

    Try something like this (adapt to your own code)
    PHP Code:
    if($_SESSION['type'] == 'Adm') {
        
    $menu $_SERVER['DOCUMENT_ROOT']."/utility/menu_admin.php";
    }
    else 
    $menu $_SERVER['DOCUMENT_ROOT']."/utility/menu_admin.php";

    $menu file_get_contents($menu);
    $template '<DOCTYPE blah blah....' $menu '...</div>'
    Using require is not the best solution because it just drops code right where it is. Doesn't give you the ability to get the text into a variable, or manipulate it much at all.

    Using a class/function to return the HTML you want would probably be a better approach still.

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks guys. Got it working.

    I had though about trying OOP with this but this has to get cranked out and I am not up to speed completely on PHP OOP yet.

    Thanks again!


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
  •