/    Sign up×
Community /Pin to ProfileBookmark

need to nest arrays, logic escapes me

ok, i started with a javascript problem, its resolution creates a php issue for me

heres what i need to do

i have a database, table looks like this

[code=php]
+———————–+
| ID | RACE | CLASS |
+—-+——–+———+
| 1 | Elf | Hunter |
| 2 | Dwarf | Mage |
| 3 | Elf | Warrior |
| 4 | Human | Druid |
| 5 | Ogre | Mage |
| 6 | Dwarf | Druid |
| 7 | Elf | Druid |
| 8 | Human | Hunter |
| 9 | Human | Mage |
| 10 | Ogre | Druid |
| 11 | Dwarf | Hunter |
| 12 | Ogre | Hunter |
+———————–+
[/code]

i need to pull all this data, and format it like so

[code=php]
/*************************************************
* this will actually fit into a javascript code *
* this is the formatting i need, notice the *
* comma (or lack of) after the () *
*************************************************/

class = new Array(
new Array( // Dwarves
new Array(“Druid”, “Druid”), // comma
new Array(“Hunter”, “Hunter”), // comma
new Array(“Mage”, “Mage”) // no comma
),
// Duplicated to show the Option Value and the Option Choice
new Array( // Elves
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Warrior”, “Warrior”)
),
new Array( // Humans
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Mage”, “Mage”)
),
new Array( // Ogres
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Mage”, “Mage”)
)
);
[/code]

As you can see, I need to sort each array alphabetically, and each nested array should be alphabetical too

i dont need the comments, that is just to show you where the info was pulled from

im sure it involves nested while loops, but not sure how to get it formatted properly, and with comma’s where necessary, and missing where not

its probably simpeler than i am thinking, but who knows, i like to make my life difficult

make sence?

it will be used in this script

[code=php]<html>
<HEAD>
<title>-</title>

<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin

classes = new Array(
new Array(
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Mage”, “Mage”)
),
new Array(
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Warrior”, “Warrior”)
),
new Array(
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Mage”, “Mage”)
),
new Array(
new Array(“Druid”, “Druid”),
new Array(“Hunter”, “Hunter”),
new Array(“Mage”, “Mage”)
)
);

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;

// empty existing items
for (i = selectCtrl.options.length; i >= 0; i–) {
selectCtrl.options[i] = null;
}

prompt = (itemArray != null) ? goodPrompt : badPrompt;
if (prompt == null) {
j = 0;
} else {
selectCtrl.options[0] = new Option(prompt);
j = 1;
}
if (itemArray != null) {

// add new items
for (i = 0; i < itemArray.length; i++) {
selectCtrl.options[j] = new Option(itemArray[i][0]);
if (itemArray[i][1] != null) {
selectCtrl.options[j].value = itemArray[i][1];
}
j++;
}

// select first item (prompt) for sub list
selectCtrl.options[0].selected = true;

}
}

// End –>
</script>

</HEAD>
<BODY>

<FORM NAME=”main” action=”multidrop.html”>

<SELECT NAME=”Race” onChange=”fillSelectFromArray(this.form.Classes, ((this.selectedIndex == -1) ? null : classes[this.selectedIndex-1]));”>
<OPTION VALUE=”-1″>Select Race</option>
<OPTION VALUE=”Dwarf”>Dwarf</option>
<OPTION VALUE=”Elf”>Elf</option>
<OPTION VALUE=”Human”>Human</option>
<OPTION VALUE=”Ogre”>Ogre</option>
</SELECT>

<BR />

<SELECT NAME=”Classes”>
<OPTION>Select Class</OPTION>
</SELECT>

<input type=”submit” value=”ok” />
</FORM>

</body>
</html>
[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsJun 20.2005 — Ok, so you need to make the array in PHP (only difference is arrays in PHP are "array" instead of "new Array"), then sort it using array_multisort, then loop through each array and write it to the page in JavaScript format.
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — sounds good, i think..

now how would i do that... i have never had a need to print an array, and the wording change.. ?
Copy linkTweet thisAlerts:
@ShrineDesignsJun 20.2005 — try[code=php]$result = mysql_query("SELECT * FROM classes");
$classes = array();

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$classes[] = $row;
}
mysql_free_result($result);
$class = array();

foreach($classes as $c)
{
$tmp = array();

foreach($classes as $a)
{
if($a['race'] == $c['race'])
{
$tmp[] = $a;
}
}
if(!empty($tmp))
{
$class[] = $tmp;
}
}
print_r($class);[/code]
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — ok, this is my result
[code=php]Array (
[0] => Array (
[0] => Array ( [cat_a_val] => Human [title] => Druid )
[1] => Array ( [cat_a_val] => Human [title] => Fighter )
[2] => Array ( [cat_a_val] => Human [title] => Mage )
)
[1] => Array (
[0] => Array ( [cat_a_val] => Human [title] => Druid )
[1] => Array ( [cat_a_val] => Human [title] => Fighter )
[2] => Array ( [cat_a_val] => Human [title] => Mage )
)
[2] => Array (
[0] => Array ( [cat_a_val] => Human [title] => Druid )
[1] => Array ( [cat_a_val] => Human [title] => Fighter )
[2] => Array ( [cat_a_val] => Human [title] => Mage )
)
) [/code]


there are only 3 items in database at time, it repeats all 3, looks like it is running an array for every time an item appears (if i had 4 items, it would repeat 4 times? is what i am thinking)

i made a change, by only SELECTING what i wanted, and i ORDER BY ASC both items

also, is there a way to get it to change format of printing to remove the

[cat_a_val] =>

[title] =>

and just return the words, in quotes with a comma seperating? i am pretty sure formatting is going to be a factor
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — also, i had to format the results, it printed all on 1 line

is there a way to get a new line for each array value, i tried rn and got a php error.. i have never used print_r... well, as i said.. i have never had a need to print arrays
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — added some content for testing, i was right, 2 items, repeated 2x[code=php]Array (
[0] => Array (
[0] => Array ( [cat_a_val] => Dwarf [title] => Druid )
[1] => Array ( [cat_a_val] => Dwarf [title] => Priest )
)
[1] => Array (
[0] => Array ( [cat_a_val] => Dwarf [title] => Druid )
[1] => Array ( [cat_a_val] => Dwarf [title] => Priest )
)
[2] => Array (
[0] => Array ( [cat_a_val] => Elf [title] => Fighter )
[1] => Array ( [cat_a_val] => Elf [title] => Hunter )
)
[3] => Array (
[0] => Array ( [cat_a_val] => Elf [title] => Fighter )
[1] => Array ( [cat_a_val] => Elf [title] => Hunter )
)
[4] => Array (
[0] => Array ( [cat_a_val] => Human [title] => Druid )
[1] => Array ( [cat_a_val] => Human [title] => Fighter )
[2] => Array ( [cat_a_val] => Human [title] => Mage )
)
[5] => Array (
[0] => Array ( [cat_a_val] => Human [title] => Druid )
[1] => Array ( [cat_a_val] => Human [title] => Fighter )
[2] => Array ( [cat_a_val] => Human [title] => Mage )
)
[6] => Array (
[0] => Array ( [cat_a_val] => Human [title] => Druid )
[1] => Array ( [cat_a_val] => Human [title] => Fighter )
[2] => Array ( [cat_a_val] => Human [title] => Mage )
)
) [/code]
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — ok

using your lead, (thanks) i came up with this [code=php]$menudisp = 'Classes = new Array(';

$query = mysql_query("SELECT title from item_cat_a ORDER by title ASC") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)){

$menudisp .= '
new Array(';

$btitle = $row['title'];
$queryb = mysql_query("SELECT cat_a_val, title from item_cat_b WHERE cat_a_val = '$btitle' ORDER by title ASC") or die(mysql_error());
while ($rowb = mysql_fetch_assoc($queryb)){

$menudisp .= '
new Array("'.$rowb['title'].'", "'.$rowb['title'].'"),';
}

$menudisp .= '
),';

}

$menudisp .= '
)';
[/code]
which gives me a result of [code=php]Classes = new Array(
new Array(
new Array("Druid", "Druid"),
new Array("Priest", "Priest"),
),
new Array(
new Array("Fighter", "Fighter"),
new Array("Hunter", "Hunter"),
),
new Array(
new Array("Druid", "Druid"),
new Array("Fighter", "Fighter"),
new Array("Mage", "Mage"),
),
)
[/code]
Almost perfect you say? i agree!

now, i need to remove the comma (,) from the last line of the last of each set or arrays (Priest, Hunter, Mage)

any thoughts?
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — yes, i pulled from 2 different tables, but that was so i would get a empty responce if there was no item in a specific field
Copy linkTweet thisAlerts:
@kenderauthorJun 20.2005 — working code thanks for the help![code=php]$menudisp = 'classes = new Array(';

$query = mysql_query("SELECT title from item_cat_a ORDER by title ASC") or die(mysql_error());
$num_rows = mysql_num_rows($query);
$count = '0';
while ($row = mysql_fetch_assoc($query)){

$count = $count + '1';
$menudisp .= '
new Array(';

$btitle = $row['title'];
$queryb = mysql_query("SELECT cat_a_val, title from item_cat_b WHERE cat_a_val = '$btitle' ORDER by title ASC") or die(mysql_error());
$num_rowsb = mysql_num_rows($queryb);
$countb = '0';
while ($rowb = mysql_fetch_assoc($queryb)){

$countb = $countb + '1';
if ($countb!= $num_rowsb) {
$menudisp .= '
new Array("'.$rowb['title'].'", "'.$rowb['title'].'"),';
} else {
$menudisp .= '
new Array("'.$rowb['title'].'", "'.$rowb['title'].'")';
}
}

if ($count!= $num_rows) {
$menudisp .= '
),';
} else {
$menudisp .= '
)';
}

}

$menudisp .= '
);';
[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsJun 21.2005 — Actually, last time I checked, JavaScript doesn't care if you have an extra comma.
Copy linkTweet thisAlerts:
@kenderauthorJun 21.2005 — well, it works, im not going to worry about success
×

Success!

Help @kender spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...