SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to work out these fuctions with phplib and the database module.

    I have a repeat query i want to do often. I made this into a function as follows:

    PHP Code:
    function template_get2($template,$mysql_link) {
    $query "SELECT * from email WHERE name='$template'";
    $result mysql_query($query$mysql_link);
    while ( 
    $row mysql_fetch_array($result) ) {
        
    $email $row['email'];
        }
        return 
    $email;
        } 
    which works if i call it like

    PHP Code:
    echo template_get2(admin,$mysql_link); 
    for example, the prints my "admin" template which is what i want.

    But i would really like to include this into the php-lib mysql class to make it easier. I tried this in the normal php script

    PHP Code:
    function template_get($template) {
    $query "SELECT * from email WHERE name='$template'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email_from_admin $db->f("email");
       return 
    $email_from_admin;
          }
         } 
    which didn't work and nor did trying to add a fuction to phplib itself:


    If anyone has a few pointers on how to do this sort of thing well that would be cool

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code didn't work because functions have their own local scope, and the $db variable you use to access the database class isn't valid in that scope. To fix it, add the following line to the start of your function (after the first { ):

    global $db;

    Then it should work fine.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that got rid of the error message anyway, but it is not returning results.

    A normal query using phplib works:

    PHP Code:
    $query "SELECT * from email WHERE name='admin'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email_from_admin $db->f("email");

    but doing

    PHP Code:
    function template_get($template) {
    global 
    $db;
    $query "SELECT * from email WHERE name='$template'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email $db->f("email");
       return 
    $email;
          }
         }


    echo 
    template_get(admin); 
    does nothing. I am at a loss. Am i doing something wrong here or missing something silly?

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A couple of things make sure you instantiate your db object before you call the functions, also
    PHP Code:
    function template_get($template) {
    global 
    $db;
    $query "SELECT * from email WHERE name='$template'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email $db->f("email");
       return 
    $email;
          }
         }

    echo 
    template_get(admin); 

    should be
    PHP Code:
    function template_get($template) {
    global 
    $db;
    $query "SELECT * from email WHERE name='$template'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email $db->f("email");
          }
       return 
    $email;
         }

    echo 
    template_get(admin); 
    Also if you are only retrieving one record you can do
    PHP Code:
    function template_get($template) {
        global 
    $db;
        
    $query "SELECT * from email WHERE name='$template'";
        
    $db->query($query);
        
    $db->next_record();
        return 
    $db->f(email);
        }

    echo 
    template_get(admin); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try putting admin in quotes:

    echo template_get("admin");

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you both SO much, that made it work - and thanks for all the help you have given me, it is really most appreciated i am learning so much here.

    status

    I now have:

    PHP Code:
    include "template.inc";
    include 
    "db_mysql.inc";


    $db = new DB_Sql;
    $t = new Template();


    $user_email template_get("user");
    $admin_email template_get("admin");


    $t->set_var(array(
        
    "user_email" => "$user_email",
        
    "admin_email" => "$admin_email",
        ));

    $user_body $t->parse("MyOutput1","user_email");
    $admin_body $t->parse("MyOutput2","admin_email");

    echo 
    $user_body;
    echo 
    $admin_body;


    function 
    template_get($template) {
    global 
    $db;
    $query "SELECT * from email WHERE name='$template'";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $email $db->f("email");
      }
        return 
    $email;
         } 
    which works brilliantly. One thing with it is that it uses an sql query for every template, which is a shame. I would really like to be able to pass the function an array of template names and get back an array of templates.

    Now i assume this is ok:

    PHP Code:

    $template_names 
    = array (
    "admin",
    "user"
    );

    $templates template_get("$template_names"); 
    now what i want back is templates to be a name based array so i can do

    echo $templates['user'];
    to get the user template if that makes sense.

    The thing is this is where i am a little unsure, both on the sql to use and also how to return it back.

    I assume that i need to build up the array from the loop (from the sql) and then just return that one variable back which is the array already created.

    But what SQL do i need to do and how do i build the array with the correct names? this is the bit i am unsure on.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is this approach, this is untested but should work
    PHP Code:
    include "template.inc";
    include 
    "db_mysql.inc";


    $db = new DB_Sql;
    $t = new Template();


    $templatestoget = array("email""admin");
    $templates template_get(implode(","$templatestoget));


    $t->set_var(array(
        
    "user_email" => $templates["email"],
        
    "admin_email" => $templates["admin"]
        ));

    $t->pparse("MyOutput1","user_email");
    $t->pparse("MyOutput2","admin_email");


    function 
    template_get($template) {
    global 
    $db;
    $query "SELECT * from email WHERE name in ($template)";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $tmp[$db->f(name)] = $db->f(templatefield);
      }
        return 
    $tmp;
         } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am getting an SQL error from this

    PHP Code:
    $query "SELECT * from email WHERE name in ($template)"
    i don't know how the "in" thing works didn't know you could do that? i tried = and that does not work, it needs the ' and '

    but

    PHP Code:
    $query "SELECT * from email WHERE name in ('$template')"
    didn't work with this error:

    Code:
    Database error: Invalid SQL: SELECT * from email WHERE name in 'email,admin'
    MySQL Error: 1064 (You have an error in your SQL syntax near ''email,admin'' at line 1)
    Session halted.
    close.... but...

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:

    PHP Code:
    $templatestoget = array("email""admin");
    foreach(
    $templatestoget as $key => $val) {
    $tmp .= "'$val',";
    }
    $tmp substr($tmp,0,-1);
    $templates template_get($tmp); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope...

    i don't think the query you are building works, that is the problem....

    you are trying to build:

    Code:
    SELECT * from email name in {'user','admin');
    but i don't think that works.. i think the function in is for doing

    SELECT email in {'email','star','word'};

    returns 1

    and

    SELECT email in {'blah','blah2'}

    returns 0

    i am not sure if it is used for actual queries...

    http://www.mysql.com/doc/C/o/Comparison_functions.html

    i could be very very wrong, i am brand new to this

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is, I was under the assumption you have a field in your table that holds the value of which template it is, for instance a field named type which either holds admin or email let's say so

    SELECT * from table WHERE type in ('email','admin');

    Should return rows with admin or email in the type field.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is the column is called 'name'

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take this example, this one works like it should as you can see:

    PHP Code:
    +----------------------------------+-----------+-----------------------------------+
    sesskey                          expire    value                             |
    +----------------------------------+-----------+-----------------------------------+
    bcb4153b5646e5e9a4efe72c8f604193 987716232 SESSION|a:1:{s:8:"beenhome";i:1;} |
    6f0422136749eadac74eff3f8d11c57c 987715487 SESSION|a:1:{s:8:"beenhome";i:1;} |
    ef026dcfde240511852c2f8d9873c4d8 987715351 SESSION|a:1:{s:8:"beenhome";i:1;} |
    a321d0d4642f1bf9be27a78f97e74610 987716395 SESSION|a:1:{s:8:"beenhome";i:1;} |
    6a91ea4a488664ce602023c165ae5964 987715206 SESSION|a:1:{s:8:"beenhome";i:1;} |
    adee4c21cd2bf7e5c9a76dfca22176c0 987715242 SESSION|a:1:{s:8:"beenhome";i:1;} |
    +----------------------------------+-----------+-----------------------------------+
    6 rows in set (0.00 sec)

    mysqlselect from sessions WHERE expire in ('987715487','987715351');
    +----------------------------------+-----------+-----------------------------------+
    sesskey                          expire    value                             |
    +----------------------------------+-----------+-----------------------------------+
    6f0422136749eadac74eff3f8d11c57c 987715487 SESSION|a:1:{s:8:"beenhome";i:1;} |
    ef026dcfde240511852c2f8d9873c4d8 987715351 SESSION|a:1:{s:8:"beenhome";i:1;} |
    +----------------------------------+-----------+-----------------------------------+ 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much freddy, got it to work now perfectly!

    I also tidied it up a bit, put the array sorter into another function (for some reason every way i tried to put it in as part of the first function failed) and then combined the lines:

    PHP Code:
    $email_to "domain@domain.com";
    // just testing template parsing

    include "template.inc";
    include 
    "db_mysql.inc";


    $db = new DB_Sql;
    $t = new Template();


    $templatestoget = array("user""admin");
    $templates template_get(array_sort($templatestoget));

    $t->set_var(array(
        
    "user_email" => $templates["user"],
        
    "admin_email" => $templates["admin"],
        
    "to" => "$email_to"
        
    ));

    $t->pparse("MyOutput1","user_email");
    $t->pparse("MyOutput2","admin_email");



    function 
    array_sort($array) {
    foreach(
    $array as $key => $val) {
    $tmp .= "'$val',";
    }
    $tmp substr($tmp,0,-1);
    return 
    $tmp;
    }


    function 
    template_get($template) {
    global 
    $db;
    $query "SELECT * from email WHERE name in ($template)";
    $db->query($query);
    while(
    $db->next_record()) {
          
    $tmp[$db->f(name)] = $db->f(email);
      }
        return 
    $tmp;
         } 
    all i need to get as many templates as i want back now is 1 line! thank you so much this is really cool, learnt so much doing it.

    thanks.


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
  •