[nycphp-talk] Making an array from a list item?
Dan Cech
dcech at phpwerx.net
Tue May 31 13:04:08 EDT 2005
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
More information about the talk
mailing list