Jump to content

Multi-Dimentional Arrays


tidalik

Recommended Posts

I have written a page that shows offspring and offspring of that offspring (and so on). The page I have written just outputs 3 generations and I would like to be able to pass the number of generations to the file and output the offspring upto that specified generations.

I have used arrays, and I admit I am not to good with arrays but managed to get it to work.

So problem lies with putting this all into a loop for the generations - can I plug an array back into itself??

The page I have is here:

[a href=\"http://www.lowchenworld.com/health/reverse_old.php?name=Tilcha%20Fashions%20Pride\" target=\"_blank\"]http://www.lowchenworld.com/health/reverse...ashions%20Pride[/a]

and the messy code is here:

[code]
$Gens = $_GET[gens];
$name = $_GET[name];

//connect to the database
include_once (''); //log in to database

function getOffspring ($name)
{
    // query db to get results using $name
    $query1 = "select name from ped where sire = '$name' OR dam = '$name'";
    $result1 = mysql_db_query ("", $query1);
    if ($result1) {
        $numOfRows = mysql_num_rows ($result1);
        for ($i = 0; $i < $numOfRows; $i++){
        $offspring[$i] = mysql_result ($result1, $i, "name");

        }
        if ($offspring){
            return $offspring;
        }
        else {
            return false;
        }
}
        else {
            echo '
                <div id="maincontent">\n<center>\n <h3>';
                echo $name;
                echo 'does not have any offspring</h3>';
                echo '</font></div>';
                mysql_close ();

                include ('footer.inc');
                die;}
}


if (!$Gens)
{ $Gens = 2;}

echo '<div id="maincontent">
<center>
<h3>';
echo $name;
echo '</h3>';

;



//for ($k = 0; $k < $Gens; $k++){

$name = addslashes($name);
$get = getOffspring ($name);

for ($l = 0; $l < count($get); $l++){
$getget[$l] = getOffspring ($get[$l]);
}

//$numofget = (count($getget) + count($get));
$numofget = 0;
for ($l = 0; $l < count($get); $l++){
    for ($y = 0; $y < count($getget[$l]); $y++){
        $numofget++;
        }
    }


echo '<table CELLSPACING="1" CELLPADDING="5" border="1"><tr><td ROWSPAN=';
echo $numofget;
echo '>Reverse pedigree of:<br>';
echo $name;
echo '</td>';

for ($l = 0; $l < count($get); $l++){
    echo '<td rowspan=';
    echo count($getget[$l]);
    echo '><a href="reverse_old.php?name=';
    echo $get[$l];
    echo '">';
    echo $get[$l];
    echo '</a> </td>';
    $getget[$l] = getOffspring ($get[$l]);
    echo '<td>';
    echo '<a href="reverse_old.php?name=';
    echo $getget[$l][0];
    echo '">';
    echo $getget[$l][0];
    echo '</a></td></tr>';


    for ($y = 1; $y < count($getget[$l]); $y++){
        echo '<tr><td>';
        echo '<a href="reverse_old.php?name=';
        echo $getget[$l][$y];
        echo '">';
        echo $getget[$l][$y];
        echo '</a></td></tr>';

        }
    }

echo '</table>';

//}

echo '</font></div>';
mysql_close ();

include ('footer.inc');
?>[/code]
Link to comment
Share on other sites

I really didn't explain myself well, my first attempt was lost due with the 'quote' tag (highlight something and press the quote button and you lose the lot...).

This is basically what should happen:

1. Recieve a Name via $_GET and Number of Generations
2. Find Offspring of Name, save it to Array[]
3. Find the Offspring of Array[] Name, save it to Array[][]
4. Find the Offspring of Array[][], save it to Array[][][]
-and on and on until the # of Generations are complete-

Then I must find the total number of names in the final rotation, and output the names into a HTML table.

I can get the info into arrays (including multi-dimentional arrays) but not in a loop system which would allow me to pass the # of generations via $_GET.

So how do I go about setting up the arrays if I don't know the # of Generations needed until the script is run?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.