Who here knows Smarty? I have a quick question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RedShoe
    赤い靴 call me 202-456-1111
    • Feb 2001
    • 14831

    #1

    Who here knows Smarty? I have a quick question

    according to this smarty doc, http://smarty.php.net/manual/en/config.files.php you can gain access to a mysql database.

    And according to this doc,
    http://smarty.php.net/manual/en/api.config.load.php you can load those configs into the php file.

    But how do you tell the php which table to use within that database?

    I'm really new to php, smarty, and mysql. But from what I can gather in mysql and php, you connect to the mysql and then you connect to the table you want to call info from.

    So how do you do that with smarty?

    OR

    Is it better to just create a config file of an associative array of data?


    I asked that on the smarty board but after being on GFY for years all other boards move slow as shit.

    SPECIALTY COSTUMES • PROPS • FX
    Superheroes • Monsters • Robots
    PM for details


    For any manufacturing needs. Adult or otherwise.

    aka BonsHigh on Insta
    Bonsai weed plants


  • RedShoe
    赤い靴 call me 202-456-1111
    • Feb 2001
    • 14831

    #2
    n e one?

    SPECIALTY COSTUMES • PROPS • FX
    Superheroes • Monsters • Robots
    PM for details


    For any manufacturing needs. Adult or otherwise.

    aka BonsHigh on Insta
    Bonsai weed plants


    Comment

    • Varius
      Confirmed User
      • Jun 2004
      • 6890

      #3
      You really shouldn't be mixing php code into your smarty templates as the whole purpose of smarty is to separate code from html.

      However Smarty has no mysql functions I know of, so to do it in the template you would have to use {php}{/php} to allow you to execte php code and then use php's mysql functions like mysql_connect(), mysql_db_query(), etc...

      I recommend since you are starting though to start the right way and use adoDB or another db abstraction layer class to keep things portable and clean.

      Do your SQL in the php file and assign it to whatever variables you need to access it with in smarty. Don't tdo your SQL in the template.
      Skype variuscr - Email varius AT gmail

      Comment

      • RedShoe
        赤い靴 call me 202-456-1111
        • Feb 2001
        • 14831

        #4
        Originally posted by Varius
        You really shouldn't be mixing php code into your smarty templates as the whole purpose of smarty is to separate code from html.

        However Smarty has no mysql functions I know of, so to do it in the template you would have to use {php}{/php} to allow you to execte php code and then use php's mysql functions like mysql_connect(), mysql_db_query(), etc...

        I recommend since you are starting though to start the right way and use adoDB or another db abstraction layer class to keep things portable and clean.

        Do your SQL in the php file and assign it to whatever variables you need to access it with in smarty. Don't tdo your SQL in the template.
        I guess, that's the part that boggles my mind.
        Smarty prides itself on not needing mysql, and doesn't even offer a way to use mysql... so then how do you use databases? Can you even use databases?

        I want to create database of all our girls, and display them with Smarty and be able to sort them, and rate them, all that (in time). And I'm willing to kinda play with it and learn it myself, but I just don't see how you do that WITHOUT the use of mysql.

        SPECIALTY COSTUMES • PROPS • FX
        Superheroes • Monsters • Robots
        PM for details


        For any manufacturing needs. Adult or otherwise.

        aka BonsHigh on Insta
        Bonsai weed plants


        Comment

        • Tempest
          Too lazy to set a custom title
          • May 2004
          • 10217

          #5
          Originally posted by RedShoe
          I guess, that's the part that boggles my mind.
          Smarty prides itself on not needing mysql, and doesn't even offer a way to use mysql... so then how do you use databases? Can you even use databases?

          I want to create database of all our girls, and display them with Smarty and be able to sort them, and rate them, all that (in time). And I'm willing to kinda play with it and learn it myself, but I just don't see how you do that WITHOUT the use of mysql.
          Download and check out the example they have of how to do things... Yes, Smarty doesn't "need" MySQL.. but if you're site needs data then your site needs MySQL.. I've only just started using Smarty myself and am really liking it so far.

          Comment

          • Varius
            Confirmed User
            • Jun 2004
            • 6890

            #6
            Red,

            Quick example to help you out:

            (this part is in your php file, assuming you already connected to the Db etc...)

            $sql = "SELECT field1, field2 FROM table";
            $result = $db->GetAll($sql); // This is if you are using adodb

            $smarty->assign('result',$result);

            (this is how you loop through that data in your template)

            {section name=a loop=$result}
            <tr><td>whatever HTML here</td><td>{$result[a].field1}, {$result[a].field2}</td></tr>
            {/section}

            Hope that helps
            Skype variuscr - Email varius AT gmail

            Comment

            • TheDoc
              Too lazy to set a custom title
              • Jul 2001
              • 13827

              #7
              You use smarty plugins. This allows you to create blocks of php that can do different functions, then call those functions to the template.

              Often in /smarty/libs/plugins

              You normally don't have to mysql connect directly when using a plugin, but you can if you want to connect to a different db other than the local setup. As an example

              Code:
              function smarty_function_getpassword($params, &$smarty)
              {
              	$user = $_SERVER[PHP_AUTH_USER];
              	$result = mysql_query("SELECT * FROM members WHERE username = '$user'");
              
                 $password=$row["pass"];
                 $smarty->assign ($params['xpassword'], $password);
                 return ($password);
              }
              
              
              Call it like: {getpassword values="xpassword"}
              ~TheDoc - ICQ7765825
              It's all disambiguation

              Comment

              Working...