andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


Use Tag data more than once

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> MadLibs
View previous topic :: View next topic  
Author Message
spencer
Newbie


Joined: 09 Mar 2005
Posts: 3

PostPosted: Wed Mar 09, 2005 11:49 pm    Post subject: Use Tag data more than once Reply with quote

Is there any way to use the tag data more than once. Meaning, if I have {NAME} in a story and that I want to use that {NAME} rather than a new {NAME}, how would I input that into either the .txt file or the php calling the script?
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Thu Mar 10, 2005 6:40 am    Post subject: Reply with quote

Hi Spencer, and welcome to the board!

The MadLibs class isn't set up to do that kind of thing. Off the top of my head I'm not sure of the best way that it could be handled, either... Might require some thinking about... But one direction you could take is to use the 'addTag' function to add tags like VERB.1, NOUN.5. Then update the showForm method so that it doesn't display a textbox for any tag with '.' in it. Then you'd always want to update the replaceTags method so that if it comes across a tag with '.' in it, it gets the number and looks for the 1st occurrance of a VERB or the 5th of a NOUN and apply that value.

Make sense?

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
spencer
Newbie


Joined: 09 Mar 2005
Posts: 3

PostPosted: Thu Mar 10, 2005 9:11 am    Post subject: Reply with quote

Hmmm, that's a lot more php coding than I actually know how to do.

I really don't know how to write php code from scratch.
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Thu Mar 10, 2005 5:26 pm    Post subject: Reply with quote

Ooookay.... After a little playing around:

index.php

PHP:
<?php

    
require_once('class.extended_madlibs.php');

    
$ml =& new Extended_Madlibs();
    
$ml->addTag('MALE.1''Repeat of the first name');
    
$ml->addTag('MALE.2''Repeat of the second name');
    
$ml->useFS('.''.txt');
    
$ml->displayMadlib();

?>


class.extended_madlibs.php

PHP:
<?php

require_once('class.madlibs.php');

class 
Extended_Madlibs extends madlibs
{

    function 
showForm()
    {
        echo 
'<form name="madlibForm" action="'$_SERVER['PHP_SELF'], '" method="post">';
        echo 
'<input type="hidden" name="id" value="'urlencode($this->inText['id']), '">';
        echo 
'<input type="hidden" name="positions" value="'$this->compPositions(), '">';
        
        echo 
"<p>Please enter the following:</p>\n";
        echo 
'<table border="0" cellpadding="5" cellspacing="0" class="madlibTable">';
        foreach (
$this->tagPositions as $id => $type)
        {
            if (!
strstr($type'.')) {
                echo 
'<tr><td>'$this->tags[$type], '</td><td><input type="textbox" name="word['$id']"></td></tr>'"\n";
            }
        }
        echo 
'<tr><td colspan="2" align="center"><input type="submit" value="see your madlib"></td></tr>';
        echo 
"\n</table>\n</form>\n";
    }

    function 
replaceTags($word_list)
    {
        
$this->outText $this->inText;
        if (!empty(
$this->tagPositions))
        {
            
$tags array_reverse($this->tagPositionsTRUE);
            foreach (
$tags as $position => $type)
            {
                if (
strstr($type'.')) {
                    list(
$thistype$number) = explode('.'$type);
                    
$at 0;
                    foreach (
$this->tagPositions as $p => $t) {
                        if (
$t == $thistype) {
                            if (++
$at == $number) {
                                
$word_list[$position] = $word_list[$p];
                                break;
                            }
                        }
                    }
                }
                
$tag '{' $type '}';
                
$this->outText['story'] = substr_replace($this->outText['story'], '<span class="madlibWord">' $word_list[$position] . '</span>'$positionstrlen($tag));
            }
        }
    }

}

?>


hello.txt

Code:
People say hello.
"Hello, {MALE.2}," said {MALE}.  "How is your {VERB}?"
"Hi, {MALE.1}!" said {MALE}, "It's great, thanks.  What are you doing here in the {ROOM}?""
"I have lost my {NOUN}!", replied {MALE.1}.


And there you go... MALE.1 refers to the first occurrance of the '{MALE}' keyword, MALE.2 to the second, etc.

Make sure you have class.madlibs.php in the same directory as class.madlibs_extended.php.

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
spencer
Newbie


Joined: 09 Mar 2005
Posts: 3

PostPosted: Thu Mar 10, 2005 6:01 pm    Post subject: Extending madlibs Reply with quote

It works great!

I have it here: http://www.erinyes.org/spencer/humbug/Pooh/madlibs.php?id=game.txt

Thanks you for writing that! I was trying to directly call the tagPosition.[#] in the .txt file and that definitely didn't work.
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Fri Mar 11, 2005 3:21 am    Post subject: Reply with quote

Hi,

I'm glad it works! And thanks for the link back to the site. Smile

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> MadLibs All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group