SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 30
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Make smilies using str_replace and MySQL?

    I have a basic understanding of str_replace, and I know it can work with arrays. I'm just a little confused on how to put it into my other scripts and have it fetch the array from the DB.

    I have created a table called smilies. In it are the columns ascii and graphics. ascii holds the text-code for the smilie (like :-) and :-D ), graphics holds the names of the image-files (like happy.gif and grin.gif). That way I can then specify the path in the script iteslf, and don't have to edit the DB.

    If I understand it correctly this is how str_replace would look:
    PHP Code:
    $message str_replace('<img src="smilies/' $graphics$ascii$message);
    echo 
    $message
    The DB query might look like this:
    PHP Code:
    $conn db_connect();
        
        
    $query "select * from smilies where $ascii = $graphics";
        
    $result mysql_query($query) or die (mysql_error()); 
    I might be way off on this, and I'm sure there is a neat way of doing this. I'm not sure about the sql-query.

    This is the line that pulls out the message from the DB and displays it using proper HTML linebreaks.
    PHP Code:
    <?php echo nl2br($post['message']);?>
    So I would want to get my smilie-conversion to work with that.

    The posts I have read here mostly deals with arrays written directly into the code. I am hoping I don't have to do that.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically (if I understand the question)...

    $replaced = str_replace($ascii_array, $graphics_array, $subject);

    In the above situation, each instance of $ascii_array[0] would be replaced by $graphics_array[0], then [1] of each and so on. Does that makes sense? The only thing left to do would be to build the pair of arrays. Do you need help with that?
    Last edited by samsm; Apr 30, 2003 at 05:05.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Member insomica's Avatar
    Join Date
    Apr 2003
    Location
    Houston, TX
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow i never thought of that... i just made a str_replace for each face... lol
    BitWise Sreen Name - insomica
    File Size of Bitwise is only 595k.
    They are working on version for linux.
    http://www.schwippy.com/insomica/


  4. #4
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a small tutorial at another forum, and came up with this:
    PHP Code:
    <?php
        $path 
    "smilies";
        
        
    $conn db_connect();//The connection is specified elsewhere
        
    $query "SELECT ascii, graphic FROM smilies";
        
    $result mysql_query($query);

        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) { 
            
    $asciis[] = $row['ascii']; 
            
    $graphics[] = "<img src='" $path "/" $row['graphic'] . "'>"
        } 

        
    $smilie_message nl2br($post['message']); //This is what used to display the message taken from the database using echo.
        
    echo str_replace($asciis$graphics$smilie_message); 
    ?>
    Does it seem OK?

    Now I just have to figure out how to automatically display the smilies for people to choose from. I'm hardcoding the HTML for it now, but I could use the DB instead.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like that should work pretty well! :-)

    edit...
    Except... I'd go the other way with the quotes here:
    PHP Code:
    $graphics[] = '<img src="' $path '/' $row['graphic'] . '">'
    Conventional wisdom is that you should use single quotes whenever you can for strings. :-)
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey samsm, could you give me an example script of your idea? Didn't really catch on.
    Content Writing Service - Get custom SEO articles for 10$

  7. #7
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Reptilian Feline already posted one!

    Check it out, especially the two lines where the $asciis[] and $graphics[] arrays are created. Any questions about it?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  8. #8
    SitePoint Wizard dragonfly_7456's Avatar
    Join Date
    Dec 2002
    Location
    Moscow, USSR
    Posts
    1,092
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, very confused, don't even know what I should ask!!
    Content Writing Service - Get custom SEO articles for 10$

  9. #9
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dragonfly_7456
    Hmm, very confused, don't even know what I should ask!!
    Try breaking it into pieces, step by step.

    What steps don't you understand?
    Do you understand each line individually, but are confused about how several lines work together?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  10. #10
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Those quotes are driving me nuts! In the book I've been reading it is sometimes written with ' around a string and sometimes with ". When I wrote the part to display the smilies, I was almost giving up because it was so hard to keep track of them. I didn't want to use a table bacuse I didn't know how to make it understand that I needed more than one mysql-line on each table row. I came up with this:
    PHP Code:
    function print_smilies()
    {
        
    $path "smilies";
        
        
    $conn db_connect();
        
    $query "SELECT ascii, graphic FROM smilies";
        
    $result mysql_query($query);

        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) { 
            
    $asciis[] = $row['ascii']; 
            
    $graphics[] = "<img src='" $path "/" $row['graphic'] . " alt=" $row['ascii']."'>";
            
            echo 
    "<div class=\"smilie\">".$row['ascii'] . "<br /><img src=\"" $path "/" $row['graphic'] . "\" alt=\"" $row['ascii']."\"></div>";
        }

    The style class is specified externally.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  11. #11
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The quotes take a little while to get used to.

    Perhaps this will help you: http://www.sitepointforums.com/showp...&postcount=103
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  12. #12
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good post in that thread! Thanks!
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  13. #13
    SitePoint Addict tedleonard's Avatar
    Join Date
    Jun 2000
    Location
    Dark side of the moon
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick question, I've been trying to learn loops lately for myself. In the above post from reptilianline, in the while loop, is the line
    $asciis[] = $row['ascii'];
    neccessary? If so, why? Doesn't look like that var $asciis is being called anywhere, just declared there with a value of $row['ascii']. Sorry if that's a stupid question, I'm a newbie. Just trying to learn. [img]images/smilies/smile.gif[/img]

  14. #14
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its used in the str_replace.
    PHP Code:
    echo str_replace($asciis$graphics$smilie_message); 
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  15. #15
    SitePoint Addict tedleonard's Avatar
    Join Date
    Jun 2000
    Location
    Dark side of the moon
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, must be time for some coffee, missed that Thanks!

  16. #16
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Kampala Uganda
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hullo there! it's funny non of you had figured out the error in reptilianfeline Smilies Post just above! ie
    PHP Code:
     $smilie_message nl2br($post['message']); 
    .
    the $post does not work! ie i think he ment $_POST. that works..!
    And we know that all things work together for good
    To them that love God, to them who are the called
    According to his purpose. Romans 8:28

  17. #17
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $post works for me. I think globals are turned on, so that might be why. Good catch!

    EDIT: Sorry... it's not an error. $post is a variable created when pulling the message from the DB. This is from a guestbook, so it's a logical name for a variable. So in order for it to work you need to add this part before the smilie function.
    PHP Code:
    function display_post($post)
    {
     
      if(!$post)
        return;
    ?>
    <table class="book">
    <tr>
    <td>
    <b>From: <?php echo $post['poster'];?></b><br />
    <b>Date: <?php echo $post['posted'];?></b>
    </td>
    <td class="buttons">
    <a href="new_post.php?parent=0"><img src="images/new-post.gif" class="toolbar" alt="New post"></a>
    <a href="new_post.php?parent=<?php echo $post['postid'];?>"><img src="images/reply.gif" class="toolbar" alt="Reply"></a>
    <a href="index.php?expanded=<?php echo $post['postid'];?>"><img src="images/index.gif" class="toolbar" alt="To list"></a>
    </td>
    </tr>
    <tr><td colspan="2" class="alternate2">
    <?php
    //Here comes the smilie part that connects to db and excange smilie code for image
        
    $path "smilies";
        
        
    $conn db_connect();
        
    $query "SELECT ascii, graphic FROM smilies";
        
    $result mysql_query($query);

        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) { 
            
    $asciis[] = $row['ascii']; 
            
    $graphics[] = "<img src=\"" $path "/" $row['graphic'] . "\" alt=\"" $row['ascii']."\">"
        }

        
    $smilie_message nl2br($post['message']);
        echo 
    str_replace($asciis$graphics$smilie_message); 
    ?>
    </td></tr>
    </table>
    <?php
    }
    You see? it's part of a bigger thing. If you don't like $post then use another name for the variable.
    Last edited by reptilianfeline; May 21, 2003 at 22:27.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  18. #18
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Kampala Uganda
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hullo!
    What is the importance of
    PHP Code:
      MYSQL_ASSOC
    and
    PHP Code:
     nl2br 
    because to me it's like with out them your code still functions! ie
    PHP Code:
     $smilie_message = ($post['message']); 
    and
    PHP Code:
     $row mysql_fetch_array($result
    work just well. Why could i need them any way? Thanks in advance!
    And we know that all things work together for good
    To them that love God, to them who are the called
    According to his purpose. Romans 8:28

  19. #19
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MYSQL_ASSOC - I don't know. It just had it in the tutorial I found.

    n12br - makes proper <br>-tags for the message when it's pulled from the DB and put into HTML and is displayed. It takes the return-code and turns it into <br> or <br /> depending on PHP version.

    It's all part of a bigger system. This is just something I added with the help of a tutorial and so on. It's hard to trace all the steps through functions and so on. I'm afraid that if I remove something, then it will all fall appart.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  20. #20
    The short answer is yes... Herbster's Avatar
    Join Date
    Oct 2001
    Location
    Bay City, Oregon
    Posts
    715
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The optional second argument result_type in mysql_fetch_array() is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH is the default for this argument.
    By using MYSQL_BOTH, you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
    http://www.php.net/manual/en/functio...etch-array.php

  21. #21
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Herbster
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  22. #22
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Kampala Uganda
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Herbster BUT!, what are associative indices and NUM indices. I guess NUM indices are results of the type ,1,2,3,6,7,...49,900 etc{But only Numbers}.is it the case? Help me Please to understand this..the link you gave us at php.net didnt explain clearly the differences!. Thanks
    And we know that all things work together for good
    To them that love God, to them who are the called
    According to his purpose. Romans 8:28

  23. #23
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql "select foo, bar from sampleTable"
    $row mysql_fetch_assoc($sql);
    echo 
    $row['foo'// this works
    echo $row['bar'// so does this
    echo $row[0]; // this doesn't 
    PHP Code:
    $sql "select foo, bar from sampleTable"
    $row mysql_fetch_row($sql);
    echo 
    $row[0// this works (echos foo)
    echo $row[1// so does this (echos bar)
    echo $row['foo']; // this doesn't 
    With mysql_fetch _array, either format, numeric or associative indexes will work... both are available.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  24. #24
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Kampala Uganda
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know that
    PHP Code:
     echo $row['FirstName']
     echo 
    $row['LastName'
    echos only the first and last names in the respective table with very many more fields like age,etc.
    NOW What does
    PHP Code:
    echo $row[0] and 
    echo 
    $row[1
    echo in the same table with more than 2 fields? thanks otherwise.
    And we know that all things work together for good
    To them that love God, to them who are the called
    According to his purpose. Romans 8:28

  25. #25
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $row[0] would contain the first row column in the select, $row[1] the second, [2] the third... etc...

    Read the comments on these echos:
    PHP Code:
    $sql "select foo, bar from sampleTable"
    $row mysql_fetch_row($sql);
    echo 
    $row[0]; // this works (echos foo)
    echo $row[1]; // so does this (echos bar) 
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •