[nycphp-talk] Making an array from a list item?
leam at reuel.net
leam at reuel.net
Wed Jun 1 07:10:15 EDT 2005
Dan;
That took me a couple minutes to transcribe. Probably a little longer to comprehend well enough to use again. ;)
Does this sond like a reasonable english translation?
Create an array called $output
Create a query. Make the query and store the result in $result.
Iterate through the rows in $result
if there is no variable $output[$row]['skillID']
Create one as an array
Add the $row['knackID'] onto the array $output[$row]['skillID']
print it so I can read it.
It works! Thanks! I think the coffee has to really start kicking in before I fully get it though...
ciao!
leam
On Tue, May 31, 2005 at 01:04:08PM -0400, Dan Cech wrote:
> Try:
>
> $output = array();
>
> $query = 'select skillID,knackID from characters.skill_knacks';
> $result = mysql_query($query,$link_id);
>
> while($row = mysql_fetch_array($result)) {
> if (empty($output[$row['skillID']])) {
> $output[$row['skillID']] = array();
> }
> $output[$row['skillID']][] = $row['knackID'];
> }
>
> print_r($output);
>
> Dan
>
> leam at reuel.net wrote:
> >Trying to read code from last year. *sigh* At least it isn't as difficult
> >as perl...
> >
> >Looking through the archives I don't think I've asked this before. If
> >there is a better way, and I'd not be surprised, feel free to suggest it.
> >;)
> >
> >My project is a character sheet for a game. Each character has a different
> >set of skills, and each skill has a sub-skill called a "knack". A
> >character can have a rank of 0-5 in a knack. "0" means they don't have
> >that knack. Skills are not ranked; you just have it or not. For example, a
> >character might have the Skill "Hunter" and have the knacks "Fishing (2)",
> >"Stealth (2)", and "Tracking (1)". A character may or may not have all the
> >knacks in a skill. Some knacks are in more than one skill.
> >
> >Each Skill and Knack has a unique short name like "hntr", "fshng", and
> >"stlth". I've been using the short name as array keys. The character sheet
> >pulls the list of skills that character has and then looks up what knacks
> >are in that skill. It then writes the Skill header, lists each knack and
> >the rank the character has in that knack.
> >The current problem is that I'm using an included file that manually sets
> >the array $htnr to have the knacks "fshg", "stlth", etc. I'd rather, if
> >possible, have the array built from a database pull. I'm failing because I
> >don't know how to iterate through a list and make the list item an array,
> >and then populate the array with the knacks.
> >
> >Line from the included file:
> >
> >$hntr = array( "ambsh", "anmltn", "fshg", "sknng", "stlth", "srvl",
> >"trkng", "trlsgns", "trps" );
> >
> >
> >So far the code looks like this:
> >
> >
> >foreach ( $skills As $s ) {
> > $a = $s;
> > $s = array ();
> > $$s[0] = $a;
> > echo "<p>s[0] is $s[0]! ";
> > $skill_knack_query = "select knackID from characters.skill_knacks
> > where skillID='$a'";
> > $skill_knack_result = mysql_query("$skill_knack_query", $link_id);
> > // echo "skill_knack_result is $skill_knack_result \n";
> > while($skill_knack_data = mysql_fetch_array($skill_knack_result)) {
> > // echo "hello $skill_knack_data";
> > echo " <p> hello $skill_knack_data[knackID]";
> > array_push($s , $skill_knack_data["$knackID"] );
> > }
> > echo " <p> s[0] is $s[0] ";
> >}
> >
> >If I comment out the included file and try the above code it gives output
> >like this:
> >
> >s[0] is !
> >hello ambsh hello anmltn hello fshg hello sknng hello stlth hello srvl
> >hello trkng hello trlsgns hello trps s[0] is
> >
> >So the "echo $skill_knack_data[knackID]" line shows the short name is
> >being gotten, I've just not gotten it onto the array. If I change the
> >bottom "s[0] is $s[0] to higher number it still shows nothing in the array.
> >
> >It seems easiest to build the array to match the included file, but there
> >may be a better, easier way.
> >
> >Thoughts? Suggestions? Crude comments?
> >
> >ciao!
> >
> >leam
> >
> >
> >_______________________________________________
> >New York PHP Talk Mailing List
> >AMP Technology
> >Supporting Apache, MySQL and PHP
> >http://lists.nyphp.org/mailman/listinfo/talk
> >http://www.nyphp.org
>
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>
More information about the talk
mailing list