Welcome Guest, Not a member yet? Register   Sign In
Problems with Dynamic Variables in a View
#1

[eluser]Gwarrior[/eluser]
Thanks to anyone who reads this.

I created my own class file that basically grabs the URL typed in the browser and tells my script which one came into the site (I have three different domains that all route to the same place), and then I made functions that a) Create a logo for that URL, b) Echo the url itself c) Echo a link.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site {

    function generate_logo($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            echo '<a href="http://'.$site.'"><img src="/images/atc/logo.png" class="logo" alt="Around the Clock Answering Service" border="0" /></a>';
        }
        if ($site == 'ccooanswers.com' || $site == 'www.ccooanswers.com')
        {
            echo '<a href="http://'.$site.'"><img src="/images/ccoo/logo.png" class="logo" alt="Communication Centers of Ohio Answering Service" border="0" /></a>';
        }    
        if ($site == 'chi')
        {
            echo '<a href="http://'.$site.'"><img src="/images/ccoo/logo.png" class="logo" alt="Chicagoland Call Communications" border="0" /></a>';
        }                
    }
    function generate_name($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            echo 'Around the Clock';
        }
        if ($site == 'ccooanswers.com' || $site == 'www.ccooanswers.com')
        {
            echo 'Communication Centers of Ohio';
        }        
        if ($site == 'ccoo')
        {
            echo 'Chicagoland Communications" />';
        }        
    }
    function generate_url($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            echo 'http://www.atcanswers.com';
        }
        if ($site == 'ccooanswering.com' || $site == 'www.ccooanswering.com')
        {
            echo 'http://www.ccooanswering.com';
        }        
    }
    function generate_ss($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            echo 'Around the Clock';
        }
        if ($site == 'ccooanswering.com' || $site == 'www.ccooanswering.com')
        {
            echo 'Communication Centers of Ohio';
        }        
        if ($site == 'ccoo')
        {
            echo '&lt;link type="stylesheet" href="" /&gt;';
        }                
    }
}

?&gt;

All is fine and good and I load them in the controller, then pass them to the view as a variable.

Code:
&lt;?php
class Home extends Controller {
    function Home()
    {
        parent::Controller();
        $this->load->library('site');
    }

    function index()
    {
        $site = $_SERVER['SERVER_NAME'];
        $data['name'] = $this->site->generate_name($site);
        $data['url'] = $this->site->generate_url($site);
        $data['ss'] = $this->site->generate_ss($site);
        $data['logo'] = $this->site->generate_logo($site);
        $this->load->view('header', $data);
        $this->load->view('home/index');
        $this->load->view('footer');
    }
}
?&gt;

And we're still working fine, but then I go to use the variable in the view and it all goes to hell. No matter where I try to echo the variable, it put it right on the top, even before the DOCTYPE declaration.

Code:
Around the Clockhttp://www.atcanswers.comAround the Clock<a href="http://atcanswers.com"><img src="/images/atc/logo.png" class="logo" alt="Around the Clock Answering Service" border="0" /></a><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;

Any ideas?

Thanks guys.
#2

[eluser]TheFuzzy0ne[/eluser]
It's because your functions are echoing the text, and not returning it. This will work better for you:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site {

    function generate_logo($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            return '<a href="http://'.$site.'"><img src="/images/atc/logo.png" class="logo" alt="Around the Clock Answering Service" border="0" /></a>';
        }
        if ($site == 'ccooanswers.com' || $site == 'www.ccooanswers.com')
        {
            return '<a href="http://'.$site.'"><img src="/images/ccoo/logo.png" class="logo" alt="Communication Centers of Ohio Answering Service" border="0" /></a>';
        }    
        if ($site == 'chi')
        {
            return '<a href="http://'.$site.'"><img src="/images/ccoo/logo.png" class="logo" alt="Chicagoland Call Communications" border="0" /></a>';
        }                
    }
    function generate_name($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            return 'Around the Clock';
        }
        if ($site == 'ccooanswers.com' || $site == 'www.ccooanswers.com')
        {
            return 'Communication Centers of Ohio';
        }        
        if ($site == 'ccoo')
        {
            return 'Chicagoland Communications" />';
        }        
    }
    function generate_url($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            return 'http://www.atcanswers.com';
        }
        if ($site == 'ccooanswering.com' || $site == 'www.ccooanswering.com')
        {
            return 'http://www.ccooanswering.com';
        }        
    }
    function generate_ss($site)
    {
        if ($site == 'atcanswers.com' || $site == 'www.atcanswers.com')
        {
            return 'Around the Clock';
        }
        if ($site == 'ccooanswering.com' || $site == 'www.ccooanswering.com')
        {
            return 'Communication Centers of Ohio';
        }        
        if ($site == 'ccoo')
        {
            return '&lt;link type="stylesheet" href="" /&gt;';
        }                
    }
}

Hope this helps.
#3

[eluser]xwero[/eluser]
Echoing is considered bad in a class or even in a function because it starts the output. Even if you're certain the function will only be used in view files don't use echo because other developers could think of more creative uses of your function.
#4

[eluser]Gwarrior[/eluser]
Thanks both of you. Very insightful and clear help, and all is working as it should.




Theme © iAndrew 2016 - Forum software by © MyBB