Christopher.
Thank you for the amended code which has greatly simplified and reduced the size of this section of my PHP.
I think perhaps, as you suggested, I should go back and explain exactly what I'm doing.
My genealogy site uses a file called "display.php" which is passed a Unique Reference Number (URN) as follows:
display.php?urn=11164 where urn is the reference for the person being viewed. I'll use 11164 for this example. I then set $urn as $mainurn so that this remains constant throughout the rest of the page using:
Code: Select all
$mainurn = mysql_real_escape_string($_GET['urn']);
$mainurn = ucfirst($mainurn);
$invalidChars = array("/",".","\\","\"",";","http",":","!","*","&");
$mainurn = str_replace($invalidChars,"",$mainurn);
A query is sent which selects ALL fields from my database where the urn is 11164 ($mainurn).
The PHP goes on to echo HTML populating a table with details such as forename, middle names, surname, date of birth etc. etc.
When an individual has brothers or sisters, there are rows in my table called sibling1urn through to sibling16urn. The data in these rows is simply the URN for that individual.
So my code up until this point has downloaded all of the information for the person with urn 11164.
Including the urn of their brothers and sisters and these are stored in the strings $sibling1urn through to $sibling16urn.
What I want this code to do is this...
Download from the table the selected fields ($fields) where the URN matches that downloaded as $sibling1urn to $sibling16urn from the original query.
For example:
Kris DIDYMUS (Original person viewed)
$forename = Kris
$Middlenames = Paul
$Surname = Didymus
...
$sibling1run = 29783 (these are not incremental numbers by design)
$sibling2urn = 83983
So I want this code to set up a FOR loop where $i is the incremental number in the $sibling..urn string.
Then I want it to interogate my MySQL database and fetch the rows defined in $fields before echoing them as a hyperlink.
The following code WORKS but will (obviously) only download the details of the first sibling:
Code: Select all
for ($i=1;$i<=16;$i++)
{
$prefix = $i < 10 ? '0' : '';
$fields = "urn,surname,forename,middlenames,yearofbirth,bloodline";
// EXTRACT DATA AND DISPLAY
$query = "SELECT $fields FROM tree WHERE urn = '$sibling1urn'";
$result = mysql_query($query)
or die ("Couldn't execute query");
echo "$sibquery"; // I use this simply to check that the increments are correct
while($row = mysql_fetch_assoc($result))
{
extract($row);
if ($row['bloodline'] == "AA0000") {
$sibclass = 'bloodline';
} else {
$sibclass = 'footer';
}
echo " <tt>$prefix$i:</tt><a href='$link$urn' class='$sibclass'>$forename $middlenames $surname ($yearofbirth)</a> <br/>";}
}
This displays:
$sibling1urn 01:Richard John LUGG (1885)
$sibling2urn 02:Richard John LUGG (1885)
$sibling3urn 03:Richard John LUGG (1885)
$sibling4urn 04:Richard John LUGG (1885)
$sibling5urn 05:Richard John LUGG (1885)
$sibling6urn 06:Richard John LUGG (1885)
$sibling7urn 07:Richard John LUGG (1885)
$sibling8urn 08:Richard John LUGG (1885)
$sibling9urn 09:Richard John LUGG (1885)
$sibling10urn 10:Richard John LUGG (1885)
$sibling11urn 11:Richard John LUGG (1885)
$sibling12urn 12:Richard John LUGG (1885)
$sibling13urn 13:Richard John LUGG (1885)
$sibling14urn 14:Richard John LUGG (1885)
$sibling15urn 15:Richard John LUGG (1885)
$sibling16urn 16:Richard John LUGG (1885)
The problem occurs when I try to have the FOR loop automate the process of stepping through each sibling.
The code I now have for doing this is:
Code: Select all
for ($i=1;$i<=16;$i++)
{
$prefix = $i < 10 ? '0' : '';
$sibquery = '$sibling' . $i . 'urn';
$fields = "urn,surname,forename,middlenames,yearofbirth,bloodline";
// EXTRACT DATA AND DISPLAY
$query = "SELECT $fields FROM tree WHERE urn = '$sibquery'";
$result = mysql_query($query)
or die ("Couldn't execute query");
echo "$sibquery";
while($row = mysql_fetch_assoc($result))
{
extract($row);
if ($row['bloodline'] == "AA0000") {
$sibclass = 'bloodline';
} else {
$sibclass = 'footer';
}
echo " <tt>$prefix$i:</tt><a href='$link$urn' class='$sibclass'>$forename $middlenames $surname ($yearofbirth)</a> <br/>";}
}
I don't get any errors but no data is fetched.
I know therefore that whereas I'm asking my code to fetch the fields listed from an incremental string ($sibling{$i}urn) it's treating the first part ($sibling) as a variable and not as a straight forward string.
I really hopes this makes sense!