Jump to content

Procode

Members
  • Posts

    18
  • Joined

  • Last visited

    Never

Posts posted by Procode

  1. [quote author=ToonMariner link=topic=117925.msg481493#msg481493 date=1165629393]
    using define will make a constant which is available anywhere - so you don't need the $smarty->

    once you have defined it you can access it anywhere. (good practice to use uppercase for constants - helps you identify them in your code)
    [/quote]

    Can you give an example please?

    Do you mean do this
    [code]
    $smarty = new Smarty();
    DEFINE('SMARTY', $smarty);
    [/code]
    Then inside a class function i do[code]SMARTY->blah();[/code]
    Elaborate a little more please.
  2. Ok what i want to do is define one variable and use it throughout the whole class and it's functions.
    Say it's smarty. How can i do[code]$smarty = new Smarty();[/code] once and use it regulary throughout my functions in the class without having to define it every time in every function?

    Example:

    Somehow i define smarty and then

    [code]class MyClass{
        function MyFunction(){
              $smarty->define('var', $var);
        }
    }[/code]
  3. What am i missing?

    My MySQL class code:

    [code]function assoc_array($sql){
    $this->do_connect();
    $sql_result = mysql_query($sql) or die(mysql_error());
    if (!$sql_result)
            {
                $sql_error = mysql_error();
                echo "<br>";
                echo "<p>".$this->mysql['gen_error']."</p>"; 
                return(false);
            }
            else
            {
                $info = mysql_fetch_assoc($sql_result);
                return ($info);
            }
    [/code]

    And in my actual class the code is:

    [code]function members_main(){
    $smarty = new Smarty();
    $mysql = new mysql();
    $sql = "SELECT * FROM `user` WHERE username = '".$_SESSION['user']."' LIMIT 1";
    $info = $mysql->assoc_array($sql);
    $smarty->assign('full_name', $info['name']);
    $smarty->assign('username', $info['username']);
    $smarty->assign('email', $info['email']);
    }
    [/code]

    it basicly shows up blank in smarty, means it doesnt work. Whats wrong?

    The session does work i checked.
  4. Ok i am trying assign a var to smarty though my class.

    Here is the code:
    [code]function members_main(){
    $smarty = new Smarty();
    $sql = "SELECT * FROM `user` WHERE username = '".$_SESSION['user']."' LIMIT 1";
    $mysql = new mysql();
    $info = $mysql->assoc_array($sql);
    $smarty->assign('name', $_SESSION['user']);
    }

    function member_page(){
    $smarty = new Smarty();
    switch($_GET['do']){
    default:
    $this->members_main();
    $smarty->display('main.members.tpl');
    break;[/code]

    Where you see [code]$this->members_main();[/code] if i replace that with [code]$smarty->assign('name', $_SESSION['user']);[/code] It works fine. But if i do it through [code]$this->members_main();[/code] it doesn't work. What am i doing wrong?
  5. Sorry if it's noobish but i am just learning classes and i just can't get this fixed.

    Basicly i wrote a mysql class and now have hell of errors saying it does not let me connect to the database, is it me or am i doing something wrong. I use WAMP so i use root for username... no password, localhost and db name is correct as well.

    Here is the MySQL class.(part of it)

    [code]class mysql {

    //MySQL var

    var $mysql = array();

    //MySQL settings.

    function settings(){
    //MySQL settings.
    $this->mysql['host'] = $mysql['host'];
    $this->mysql['user'] = $mysql['user'];
    $this->mysql['pass'] = $mysql['pass'];
    $this->mysql['dbname'] = $mysql['dbname'];
    //MySQL errors.
    $this -> mysql['con_error'] = "<b>MySQL encountered a severe error while trying to connect to the host</b>n";
            $this -> mysql['db_error'] = "<b>MySQL encountered a severe error while trying to use the database</b>n";
            $this -> mysql['gen_error'] = "<b>MySQL Error: <b>Due to a database error this page cannot be displayed</b>n";
    }

    //MySQL connect function.

    function do_connect(){

    $this->settings();

    $connect = mysql_connect($this->mysql['host'], $this->mysql['user'], $this->mysql['pass']);
    $db = mysql_select_db($this->mysql['dbname']);

    if(!$connect){
    echo "<br>";
    echo $this->mysql['con_error'];
    }elseif(!$db){
    echo "<br>";
    echo $this->mysql['db_error'];
    }
    }

    //MySQL query function

    function sql_run($sql){
    $this->do_connect();
    $res = mysql_query($sql);
    if(!$res){
    echo "<br>";
    echo $this->mysql['db_error'];
    }else{
    return($res);
    }
    }

    //MySQL mysq_num_rows function.

    function num_rows($sql){
    $this->do_connect();
    $now = $this->sql_run($sql);
    $res = mysql_num_rows($now);
    if(!$res){
    echo "<br>";
    echo $this->mysql['db_error'];
    }else{
    return $res;
    }
    }[/code]

    Please help, what am i doing wrong?
  6. [quote author=ober link=topic=115268.msg469400#msg469400 date=1163736346]
    www.ajaxfreaks.com would be a good place... other than that, read some of the threads around here and try a few things. 
    [/quote]

    Thanks, i didnt think of that :P
  7. I am coding a simple guest book and i haven't made classes related to Smarty yet so i need help.

    My code is as follows:

    [code]function show_contacts($user_id){
    $this->mysql = new mysql();
    $sql = "SELECT * FROM `contacts` WHERE 'owner' = '".$user_id.'"'";
    $this->contacts = $mysql->assoc_aray($sql);
    while($this->contacts){
    //what here ?
    }
    }[/code]

    That is the code inside a class, i also made a mysql class before that but what do i have to put where it says "what here?" and what do i put in a .tpl file in smarty in order for it to list the contacts? :)

    Thank you.
×
×
  • Create New...

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.