I think what I want is a recursive function, but please let me know if there's another way to achieve what I want to do! I'm not sure I'm going to explain this very well, so please let me know if you need further clarification.
I've got a function, findSeed() which takes an id and returns an array that contains information about the "seed":
function findSeeds($id) {
global $wpdb;
$query = "SELECT * FROM seedTable WHERE id = " . $id;
$seed = $wpdb->get_row($query, ARRAY_A);
return $seed;
}
It returns the following array:
Array
(
[id] => 9
[name] => Sign
[seed1] => 4
[seed2] => 3
)
Where seed 1 and seed 2 are the ids of the seeds needed to create it. I'm looking to make a function that will create a multidimensional array, in the following format, where the first part of the array contains the first "seed", the next contains the two seeds contained in the first, the next contains the two seeds contained in each of those, and so on:
Array
(
[1] => Array
(
[0] => Stripey Wallpaper
)
[2] => Array
(
[0] => Green Block
[1] => Aqua Block
)
[3] => Array
(
[0] => Bricks
[1] => Grass
[2] => Bricks
[3] => Glass Pane
)
[4] => Array
(
[0] => Rock
[1] => Grass
[2] => Dirt
[3] => Rock
[4] => Rock
[5] => Grass
[6] => Rock
[7] => Lava
)
)
I'm doing it so far with a (horrible) function, shown here, but the "map" could be anything between 2 and 8 rows, and I'm struggling to see how to turn it into a neat little function that works for anything:
function makeMap($id) {
// First Row
$rowNum = 1;
$oneSeed = findSeeds($id);
$map[$rowNum][] = $oneSeed[name];
// Second Row
$rowNum = $rowNum + 1;
$twoSeed = findSeeds($oneSeed[seed1]);
$threeSeed = findSeeds($oneSeed[seed2]);
$map[$rowNum][] = $twoSeed[name];
$map[$rowNum][] = $threeSeed[name];
// Third Row
$rowNum = $rowNum + 1;
$fourSeed = findSeeds($twoSeed[seed1]);
$fiveSeed = findSeeds($twoSeed[seed2]);
$sixSeed = findSeeds($threeSeed[seed1]);
$sevenSeed = findSeeds($threeSeed[seed2]);
$map[$rowNum][] = $fourSeed[name];
$map[$rowNum][] = $fiveSeed[name];
$map[$rowNum][] = $sixSeed[name];
$map[$rowNum][] = $sevenSeed[name];
// Fourth Row
$rowNum = $rowNum + 1;
$eightSeed = findSeeds($fourSeed[seed1]);
$nineSeed = findSeeds($fourSeed[seed2]);
$tenSeed = findSeeds($fiveSeed[seed1]);
$elevenSeed = findSeeds($fiveSeed[seed2]);
$twelveSeed = findSeeds($sixSeed[seed1]);
$thirteenSeed = findSeeds($sixSeed[seed2]);
$fourteenSeed = findSeeds($sevenSeed[seed1]);
$fifteenSeed = findSeeds($sevenSeed[seed2]);
$map[$rowNum][] = $eightSeed[name];
$map[$rowNum][] = $nineSeed[name];
$map[$rowNum][] = $tenSeed[name];
$map[$rowNum][] = $elevenSeed[name];
$map[$rowNum][] = $twelveSeed[name];
$map[$rowNum][] = $thirteenSeed[name];
$map[$rowNum][] = $fourteenSeed[name];
$map[$rowNum][] = $fifteenSeed[name];
return $map;
}
It should stop when all the "row" nodes are blank, so the following only needs to return the first two "rows":
Array
(
[1] => Array
(
[0] => Wood Block
)
[2] => Array
(
[0] => Dirt
[1] => Lava
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
)
Any tips, hints or solutions would be GREATLY appreciated,
Thank you.
Edit: Quick note on why I'd like it in the above format - I'm then printing out the results in a table, with the first seed spanning the entire row, then breaking it down with the children underneath, like so:
$map = makeMap($_POST['theItem']);
$colspan = count(end(array_values($map)));
echo '<h3>' . $map[row1][0] . '</h3><br/>';
echo '<table>';
foreach ($map as $row) {
echo '<tr>';
foreach ( $row as $cell) {
echo '<td colspan="'. $colspan .'"><div class="seed">' . $cell . '</div></td>';
}
$colspan = $colspan / 2;
echo '</tr>';
}
echo '</table>';
<?php
$array = array('one', 'two', 'three');
for($i =1; $i<= count($array); $i++){
for($j=0; $j<$i; $j++){
$arrymulti[$i][$j]= $array[$j];
}
}
echo '<pre>';
print_r($arrymulti);
?>
Output:
Array
(
[1] => Array
(
[0] => one
)
[2] => Array
(
[0] => one
[1] => two
)
[3] => Array
(
[0] => one
[1] => two
[2] => three
)
)
0 comments:
Post a Comment