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.


How to access array keys

 
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
tryangle
Newbie


Joined: 08 Mar 2006
Posts: 5

PostPosted: Wed Mar 08, 2006 10:31 pm    Post subject: How to access array keys Reply with quote

I can access the array values with $this->tags[$type] for the form... But how can I access the array keys? I tried using $this->tags[$name] and $this->tags[$position] and some other variables but got nothing.
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 10, 2006 6:46 am    Post subject: Reply with quote

Hi, and welcome to the board.

You could access them by using the php function array_keys, such as:

PHP:
<?php

$keys 
array_keys($ml->tags);  // or $this->tags is used in the class
print_r($keys);

?>


Or you could do it in a loop, such as:

PHP:
<?php

foreach ($ml->tags as $tag_id => $tag_desc) {
    echo 
'Tag id = '$tag_id'<br />';
}

?>


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


Joined: 08 Mar 2006
Posts: 5

PostPosted: Fri Mar 10, 2006 7:55 pm    Post subject: Thank you Andy... Reply with quote

I'm trying to put each respective tag (like NOUN, VERB) in each respective textbox.
So far the closest I've gotten is by adding the foreach inside the

Code:

function showForm(){
foreach ($this->tags as $key => $keys)
...
echo ... <input type="textbox" ... value="'.$key.'">
}

but it only returns the last one
MADEUP
in all the textboxes

Thank you again...
Back to top
View user's profile Send private message
tryangle
Newbie


Joined: 08 Mar 2006
Posts: 5

PostPosted: Sat Mar 25, 2006 3:54 pm    Post subject: Thank You Reply with quote

I tried your example...
$keys = array_keys($this->tags);
honest... and it returns

Array

in each of the text boxes.

I was able to get something close with this...
foreach ($this->tags as $key => $keys);
it returns...

MADEUP

in each of the text boxes.

How I could get each of the identifiers in each of the textboxes?

NOUN
VERB
...
MADEUP

Thanks in advance for your help on this...
Tryangle
Back to top
View user's profile Send private message
amnuts
Site Admin


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

PostPosted: Sat Mar 25, 2006 4:26 pm    Post subject: Reply with quote

If it's only returning MADEUP then something's going wrong with the assignment of MADEUP to the tags list and it's over-writing all of the exisitng default tags rather than appending to the list. How are you adding MADEUP to the list?

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


Joined: 08 Mar 2006
Posts: 5

PostPosted: Sun Apr 02, 2006 6:01 pm    Post subject: Reply with quote

Hi Andy,

Thank you again for your response...

I think you're right... basically it was going through the array and ended up with the last key then plugged it in each box.

This is the code I used...
Code:

function showForm(){
foreach ($this->tags as $key => $keys)
...
echo ... <input type="textbox" ... value="'.$key.'">
}

Basically I followed the format you used to insert different phrases to the left of each textbox... but, I'm uncertain how come I don't get one distinctly different line like you do with the values.

Thank you again...
Tryangle
Back to top
View user's profile Send private message
tryangle
Newbie


Joined: 08 Mar 2006
Posts: 5

PostPosted: Sat Apr 08, 2006 12:44 pm    Post subject: Reply with quote

Hi again,

I'm still trying to put each "key" into each respective text box...
I was sortta able to get it by adding this line of code within the showForm() function
Code:

foreach ($this->tags as $key => $keys)

Here is the full line of code that renders the line in front of the textbox and the textbox itself with the "key"...
Code:

echo '<tr><td>', $this->tags[$type], '</td><td><input type="textbox" name="word[', $id, ']" value="'.$key.'"></td></tr>', "\n";

This renders "MADEUP" in each textbox... which is correct for the last textbox... but not for all the others.

I tried changing the value=
Code:

value="', $this->tags[$key], '"


But that rendered "Just make up a word" in all the textboxes... which is the "value" information instead of the "key" information.

I've tried some other ideas too... that displayed no results or a blank page.

Thank you again for any help on this,
Tryangle
Back to top
View user's profile Send private message
amnuts
Site Admin


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

PostPosted: Sun Apr 09, 2006 3:39 pm    Post subject: Reply with quote

OK, I wasn't getting what you wanted before, but I think I do now...

Make your for loop look like this:

Code:
foreach ($this->tagPositions as $id => $type) {
    echo '<tr><td>', $this->tags[$type], '</td><td><input type="textbox" name="word[', $id, ']" value="', $type, '" /></td></tr>', "\n";
}


Let me know if that's now what you want.

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