Collecting page is "collecting" from wrong source

Hi,

I have this page: https://kroweb.dk/gfdev/arkivalier/canvas3/sognefakta_ByLand2.php

The page is collecting information from several sources based on the parish you are clicking (from another page) or searching for directly on the page itself.
It has 3 columns.
First column collects information from a database holding all infornation on churchbooks in any parish in the country.
Second column collects information from another database holding information on all censuses in any parish the country.
Third column (the one which is the problem right now) holds a fact-box information on the parish chosen.

In the third column I am building a table that consists several table heads and -bodies. This table pulls data from a database based on the name of the parish (DB column 'Sogn' and defined as $sogn and a DB column 'Amt' defined as $amt. They are defined in a classes.php file under a class named 'FaktaBoks'. In the same classes.php file classes for the other tables are defines, and the class used for the former table is named 'ftChild'.

The classes.php file is here:
<?php
abstract class RecordTypes
{
    const ftMaster = 'ftMaster';
    const ftChild = 'ftChild';
    const ftOrphan = 'ftOrphan';

}

class FaktaBoks 
{
    public $sogn;
    public $amt;
    public $herred;
    public $linkWIKI;
    public $praester;
    public $kirkehistorie;
    public $sognehistorie;
    public $herredsinfo;
    public $herredshistorie;
    public $byhistorie;
    public $specieltsogn;
    public $litteratursogn;
    public $gammelamt;
    public $laegdsnummer;
    public $laegdsrulleperiode;
    public $laegdsrullelink;
    public $bemaekrning;

    function __construct()
    {
        $this->sogn = $this->Sogn;
        $this->amt = $this->Amt;
        $this->herred = $this->Herred;
        
        $this->linkWIKI = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL1_Wiki, $this->URL1_Wiki);
        
        $this->praester = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_2Praester, $this->URL_2Praester);
        
        $this->kirkehistorie = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_4Kirkehistorie, $this->URL_4Kirkehistorie);

        $this->sognehistorie = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_7Sognehistorie, $this->URL_7Sognehistorie);

        $this->herredsinfo = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_3Herredsinfo, $this->URL_3Herredsinfo);

        $this->herredshistorie = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_9Herredshistorie, $this->URL_9Herredshistorie);

        $this->byhistorie = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_6Byhistorie, $this->URL_6Byhistorie);

        $this->specieltsogn = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_5Specialinfo, $this->URL_5Specialinfo);

        $this->litteratursogn = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_8Literatur, $this->URL_8Literatur);

        $this->gammelamt = $this->GammelAmt;

        $this->laegdsnummer = $this->Laegdnr;

        $this->laegdsrulleperiode = $this->Periode;

        $this->laegdsrullelink = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL_laegd, "Til Lægdsrullen");
        
        $this->bemaerkning = $this->Bemaerkning;
    }    
}

class KbSogn
{
    public $link;
    public $sogn;
    public $amt;


    function __construct()
    {
         $this->link = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL, $this->Kirkebog);
         $this->sogn = $this->Sogn;
         $this->amt = $this->Amt;

    }
}



class ftCapitol
{
    public $link;
    public $omraade;
    public $aar;

    function __construct()
    {

        $this->link = sprintf("<a href='%s' target='_blank'>%s</a>", $this->URL, $this->Område);
        $this->aar = $this->Aar;
     }
}   

class ftMaster
{
    public $type;
    public $id;
    public $amtherredsogngade;
    public $sogn;
    public $aar;
    public $mytype;
    public $children;

    function __construct($row)
    {
        $this->type = RecordTypes::ftMaster;
        $this->id = $row->pId;
        $this->amtherredsogngade = $row->pAmtHerredSognGade;
        $this->sogn = $row->pSogn;
        $this->aar = $row->pAar;
        $this->mytype = $row->pType;
        $this->children = array();
    }
}

class ftChild
{
    public $type;
    public $id;
    public $amtherredsogngade;    
    public $sogn;
    public $amt;
    public $aar;
    public $mytype;
    public $link;

    function __construct($row)
    {
        $this->type = (is_null($row->pId)) ? RecordTypes::ftOrphan : RecordTypes::ftChild;
        $this->id = $row->cId;
        $this->amtherredsogngade = $row->cAmtHerredSognGade;        
        $this->sogn = $row->cSogn;
        $this->amt = $row->cAmt;
        $this->aar = $row->cAar;
        $this->mytype = $row->cType;
        $this->link = empty($row->cURL) ? $row->cAmtHerredSognGade : sprintf("<a href='%s' target='_blank'>%s</a>", $row->cURL, $row->cAmtHerredSognGade);
    }
}

function cityVariations($city) {
    $substitute = array(
        "æ" => "ae",
        "ø" => "oe",
        "å" => "aa",
        "ae" =>"æ",
        "oe" =>"ø",
        "aa" => "å",
        );

    $flatCity = mb_convert_case($city, MB_CASE_LOWER);

    $cities[] = $flatCity;
    $cities[] = strtr($flatCity, $substitute); 

    return $cities;
}
 

Open in new window


Well - the problem is, that when defining that I want to include $sogn in a string in the table file, it pulls the data in the first header, and apparently only there, from the DB just used in the former column (class = ftChild). All other data appears to be drawn from the right database.

The way I build the table is this:
<?php require_once('fbox_data_SQL.php') ?>

<?php if (count($data)): ?>

    <table id="FboxTable" class="table">


        <!-- Gammelt Amt og Lægder -->
        <thead>
            <tr>
                <th>Gammelt Amt og lægder  i <? echo $record->amt?> Amt, og <? echo $record->sogn?> Sogn</th>             
            </tr>
        </thead>

        <?php foreach ($data as $record): ?>

            
                <tr> <td> Gammelt Amt (1662-1793) for <? echo $record->sogn?> Sogn: <?php echo$record->gammelamt ?></td> </tr>
                   
                <tr> <td> Lægdsnummer indtil 1793 for <? echo $record->sogn?> Sogn: <?php echo$record->laegdsnummer ?> </td> </tr> 

                <tr> <td> Periode for Lægdsruller for <? echo $record->sogn?> Sogn: <?php echo$record->laegdsrulleperiode ?> </td> </tr> 

                <tr> <td> Link til Lægdsruller for <? echo $record->sogn?> Sogn: <?php echo$record->laegdsrullelink ?> </td> </tr> 

                

        <?php endforeach; ?>



        <!-- Sogn, Kirke og Præster -->
        <thead>
            <tr>
                <th>Sogn, Kirke og Præster i <? echo $record->sogn?> Sogn</th>             
            </tr>
        </thead>

        <?php foreach ($data as $record): ?>

            
                <tr> <td> Om <? echo $record->sogn?> Sogn: <?php echo$record->linkWIKI ?></td> </tr>
                   
                <tr> <td> Om præsterne i <? echo $record->sogn?> Sogn: <?php echo$record->praester ?> </td> </tr>  

                <tr> <td> Kirkehistorien i <? echo $record->sogn?> Sogn: <?php echo$record->kirkehistorie ?> </td> </tr>       
           
                <tr> <td> Historie om <? echo $record->sogn?> Sogn: <?php echo$record->sognehistorie ?> </td> </tr> 

        <?php endforeach; ?>


        <!-- Herredet -->
        <thead>
            <tr>
                <th>Herredsinfo</th>             
            </tr>
        </thead>

        <?php foreach ($data as $record): ?>

            
                <tr> <td> Herredsinfo for <? echo $record->sogn?> Sogn: <?php echo$record->herredsinfo ?></td> </tr>
                   
                <tr> <td> Gammel Herredshistorie i <? echo $record->sogn?> Sogn: <?php echo$record->herredshistorie ?> </td> </tr>  

                

        <?php endforeach; ?>

        <!-- Byens historie -->
        <thead>
            <tr>
                <th>Byens historie</th>             
            </tr>
        </thead>

        <?php foreach ($data as $record): ?>

            
                <tr> <td> Byhistorie for <? echo $record->sogn?> Sogn: <?php echo$record->byhistorie ?></td> </tr>
                   

                

        <?php endforeach; ?>


        <!-- Specielt -->
        <thead>
            <tr>
                <th>Specielle info</th>             
            </tr>
        </thead>

        <?php foreach ($data as $record): ?>

            
                <tr> <td> Specielt om <? echo $record->sogn?> Sogn: <?php echo$record->specieltsogn ?></td> </tr>
                   
                <tr> <td> Litteratur om <? echo $record->sogn?> Sogn: <?php echo$record->litteratursogn ?> </td> </tr>  

                

        <?php endforeach; ?>


        
        <!-- Bemærkninger -->
        <thead>
            <tr>
                <th>Bemærkninger fra <?php echo $record->sogn?> Sogn</th>             
            </tr>
        </thead>

        <?php foreach ($data as $record): ?>

            <tr>
                <td><?php echo $record->bemaerkning ?></td>      
            </tr>

        <?php endforeach; ?>


    </table>

<?php else: ?>

    <h6>Der er ingen resultater for denne søgning</h6>

<?php endif; ?>

Open in new window


I know that it is pulling data to the first header from the former database because the names of 'Sogn' in this database is more fragmented than in the other databases. An example is this: https://kroweb.dk/gfdev/arkivalier/canvas3/sognefakta_ByLand2.php?sogn=+Ebeltoft&amt=Randers, where you in the third column under the text: "Fakta om Sogn og Herred" can see  that the name of the $sogn string is "Ebeltoft Landsogn". This $sogn only exists in the census database, and as you can see the next header don't say "Ebeltoft Landsogn" but only "Ebeltoft" which is the one listed in the database this column is using (class = FaktaBoks). Also if I pull out the $sogn variable from the ftChild class it returns an error referring to ftChild.

So my question is how I can bind the $sogn in the class 'FaktaBoks' everywhere in the table in this third column.
Peter KromanSales ExecutiveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Peter,

Wow. There's a lot of information in that question and it's really difficult to identify exactly which parts are relevant. I don't really understand any of it !! Sorry.

Having said that, what I can see is a problem in your code (the second bit).

You are loading up fbox_data_SQL.php, which I assume is reponsible for creating a variable called $data. On line 11, you echo out $record->sogn, but it's not clear where that $record variable comes from.

After that, you do a foreach on the $data variable, assigning each iteration to the $record variable, so now the $record variable that you started out with no longer exists - you've overwritten it with whatever is in the $data collection.

By the time you get to line 35, your $record variable is not the same as it was on line 11 - it's whatever was stored in the $data variable.

What I would suggest is that you name your foreach iteration something else, for example $row:

<table id="FboxTable" class="table">

Gammelt Amt og lægder  i <? echo $record->amt?> Amt, og <? echo $record->sogn?> Sogn

<?php foreach ($data as $row): ?>

	Gammelt Amt (1662-1793) for <?php echo $row->sogn?> Sogn: <?php echo $row->gammelamt ?>
	Lægdsnummer indtil 1793 for <?php echo $row->sogn?> Sogn: <?php echo $row->laegdsnummer ?>
	Periode for Lægdsruller for <?php echo $row->sogn?> Sogn: <?php echo $row->laegdsrulleperiode ?>
	Link til Lægdsruller for <?php echo $row->sogn?> Sogn: <?php echo $row->laegdsrullelink ?>

<?php endforeach; ?>

Sogn, Kirke og Præster i <?php echo $record->sogn ?> Sogn

<?php foreach ($data as $row): ?>

	Om <?php echo $row->sogn?> Sogn: <?php echo $row->linkWIKI ?>
	Om præsterne i <?php echo $row->sogn?> Sogn: <?php echo $row->praester ?>
	Kirkehistorien i <?php echo $row->sogn?> Sogn: <?php echo $row->kirkehistorie ?>
	Historie om <?php echo $row->sogn?> Sogn: <?php echo $row->sognehistorie ?>

<?php endforeach; ?>

...

Open in new window

This way, you're keeping your $record variable intact.
0
Peter KromanSales ExecutiveAuthor Commented:
Thanks Chris,

I'll try it and get back asap :)
0
Peter KromanSales ExecutiveAuthor Commented:
Well - I followed your suggestion, but I still have a problem in line 11 - and still only in that line.

Now I get this error, which is telling me that it at least not is looking in the former class for the $sogn and $amt variables anymore :)

Gammelt Amt og Lægd i
Notice: Trying to get property of non-object in /var/www/kroweb.dk/public_html/gfdev/collecting_page2/fbox_table.php on line 11
Amt.
Notice: Trying to get property of non-object in /var/www/kroweb.dk/public_html/gfdev/collecting_page2/fbox_table.php on line 11
Sogn

The code in fbox_table.php now looks like this:

<?php require_once('fbox_data_SQL.php') ?>

<?php if (count($data)): ?>

    <table id="FboxTable" class="table">


        <!-- Gammelt Amt og Lægder -->
        <thead>
            <tr>
                <th>Gammelt Amt og Lægd  i <? echo $row->amt?> Amt. <? echo $row->sogn?> Sogn</th>             
            </tr>
        </thead>

        <?php foreach ($data as $row): ?>

            
                <tr> <td> Gammelt Amt (1662-1793) for <? echo $row->sogn?> Sogn:<strong> <?php echo$row->gammelamt ?></strong></td> </tr>
                   
                <tr> <td> Lægdsnummer indtil 1793 for <? echo $row->sogn?> Sogn: <strong><?php echo$row->laegdsnummer ?> </strong></td> </tr> 

                <tr> <td> Periode for Lægdsruller for <? echo $row->sogn?> Sogn: <strong><?php echo$row->laegdsrulleperiode ?></strong> </td> </tr> 

                <tr> <td> Link til Lægdsruller for <? echo $row->sogn?> Sogn: <?php echo$row->laegdsrullelink ?> </td> </tr> 

                

        <?php endforeach; ?>



        <!-- Sogn, Kirke og Præster -->
        <thead>
            <tr>
                <th>Sogn, Kirke og Præster i <? echo $row->sogn?> Sogn</th>             
            </tr>
        </thead>

        <?php foreach ($data as $row): ?>

            
                <tr> <td> WIKI <? echo $row->sogn?> Sogn: <?php echo$row->linkWIKI ?></td> </tr>
                   
                <tr> <td> Om præsterne i <? echo $row->sogn?> Sogn: <?php echo$row->praester ?> </td> </tr>  

                <tr> <td> Kirkehistorien i <? echo $row->sogn?> Sogn: <?php echo$row->kirkehistorie ?> </td> </tr>       
           
                <tr> <td> Historie om <? echo $row->sogn?> Sogn: <?php echo$row->sognehistorie ?> </td> </tr> 

        <?php endforeach; ?>


        <!-- Herredet -->
        <thead>
            <tr>
                <th>Herredsinfo</th>             
            </tr>
        </thead>

        <?php foreach ($data as $row): ?>

            
                <tr> <td> Herredsinfo for <? echo $row->sogn?> Sogn: <?php echo$row->herredsinfo ?></td> </tr>
                   
                <tr> <td> Gammel Herredshistorie i <? echo $row->sogn?> Sogn: <?php echo$row->herredshistorie ?> </td> </tr>  

                

        <?php endforeach; ?>

        <!-- Byens historie -->
        <thead>
            <tr>
                <th>Byens historie</th>             
            </tr>
        </thead>

        <?php foreach ($data as $row): ?>

            
                <tr> <td> Byhistorie for <? echo $row->sogn?> Sogn: <?php echo$row->byhistorie ?></td> </tr>
                   

                

        <?php endforeach; ?>


        <!-- Specielt -->
        <thead>
            <tr>
                <th>Specielle info</th>             
            </tr>
        </thead>

        <?php foreach ($data as $row): ?>

            
                <tr> <td> Specielt om <? echo $row->sogn?> Sogn: <?php echo$row->specieltsogn ?></td> </tr>
                   
                <tr> <td> Litteratur om <? echo $row->sogn?> Sogn: <?php echo$row->litteratursogn ?> </td> </tr>  

                

        <?php endforeach; ?>


        
        <!-- Bemærkninger -->
        <thead>
            <tr>
                <th>Bemærkninger fra <?php echo $row->sogn?> Sogn</th>             
            </tr>
        </thead>

        <?php foreach ($data as $row): ?>

            <tr>
                <td><?php echo $row->bemaerkning ?></td>      
            </tr>

        <?php endforeach; ?>


    </table>

<?php else: ?>

    <h6>Der er ingen resultater for denne søgning</h6>

<?php endif; ?>

Open in new window

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Peter KromanSales ExecutiveAuthor Commented:
If I change line 11 to this:
<th>Gammelt Amt og Lægd  i <? echo $record->amt?> Amt. <? echo $record->sogn?> Sogn</th>    

Open in new window

I get the $sogn and $amt from the ftChild class. Is there anyway I can bind line 11 to the FaktaBoks class?
0
Peter KromanSales ExecutiveAuthor Commented:
Halleluja - I think I have fixed it.

I wrap line 10-14 in a foreach like this:
 <?php foreach ($data as $record): ?>
        <thead>
            <tr>
                <th>Gammelt Amt og Lægd  i <? echo $record->amt?> Amt. <? echo $record->sogn?> Sogn</th>             
            </tr>
        </thead>
        <?php endforeach; ?>

Open in new window


And now it seems to be working :)
0
Chris StanyonWebDevCommented:
OK Peter,

The code you've just posted did the exact opposite of what I'd suggested. You need to keep your $record variable in place and create a new variable in the foreach loop:

Line 11 should remain using the $record variable ($row doesn't exist at that point, hence the errors you get). Anything inside a foreach loop should use a different name. If you don't do this, then by the time you've finished your foreach loop, you won't have access to the original $record.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KromanSales ExecutiveAuthor Commented:
OK. Right now I use the $record for line 11, and the $row for the rest of the foreach loops. And it seems to be working.
Do you want me to change the $row to something different for every foreach loop?
0
Chris StanyonWebDevCommented:
No!

In the code you posted with the errors, you'd changed line 11 to use $row - which was wrong. If you've now sorted that out and it's working, then leave it as it is.

From what I understood from your original question, line 11 was always working OK, so can't see why you changed it. As I said though, it's very difficult to identify the relevant parts of your question, so if it's now working, then that's good :)
0
Peter KromanSales ExecutiveAuthor Commented:
An answer giving the opposite result as expected can be the the best solution :) It made me think in other lines and it surely helped me to find the solution.

Thanks again Chris :)

PS: It was line 11 that was the problem form the start :)
0
Peter KromanSales ExecutiveAuthor Commented:
Hi,

Is it possible, when doing a foreach loop, to control if the results returned are identical, and only display identical results once. For example in a foreach loop like this:

 <?php foreach ($data as $row): ?>

            
                   
                <tr> <td> Lægdsnummer indtil 1793 for <? echo $row->sogn?> Sogn, <? echo $row->herred?> Herred: <strong>lægd <?php echo$row->laegdsnummer ?> </strong> i Gl. Amt (1662-1793):<strong> <?php echo$row->gammelamtlink ?></strong> </td> </tr> 

                <tr> <td> Periode for Lægdsruller for <? echo $row->sogn?> Sogn, <? echo $row->herred?> Herred: <strong><?php echo$row->laegdsrulleperiode ?></strong> </td> </tr> 

                <tr> <td> Link til Lægdsruller for <? echo $row->sogn?> Sogn, <? echo $row->herred?> Herred: <?php echo$row->laegdsrullelink ?> </td> </tr> 

                

        <?php endforeach; ?>

Open in new window

0
Chris StanyonWebDevCommented:
Hey Peter,

The term identical is often relative in these cases. Do you mean a given property is the same as on another record (same name, id, telephone number etc), or that ALL properties on one record are exactly the same as ALL properties on another. Generally, the best approach here is to make sure you don't have matching records in the first place - probably by ensuring your DB query only returns DISTINCT records or GROUPED records. Your DB design will dictate the best way forward on that.

To do it in PHP would more complex - potentially you would need to create a hash of all relevant properties for a given object and store it in an array (as a key). You would then check this array for a given hash key before deciding whether to add each record to the list. Slightly easier to do if your comparison only depends on a single property.
0
Peter KromanSales ExecutiveAuthor Commented:
The problem is that I have matching posts in records. It is not the entire record that is matching. And here I would like to display identical posts only once. I have actually tried to use DISTINCT, but it don't seem to be working.

Am I doing it right this way:
if (empty($_GET)) { return; }

$where = isset($_GET['amt']) ? "AND Amt = ?" : "";

$sql = <<<EOT
SELECT DISTINCT Sogn, Amt, Herred, URL1_Wiki, URL_2Praester, URL_4Kirkehistorie, URL_7Sognehistorie, URL_3Herredsinfo, URL_9Herredshistorie, URL_6Byhistorie, URL_5Specialinfo, URL_8Literatur, GammelAmt, URL_GammelAmt, Laegdnr, Periode, URL_laegd, Bemaerkning
FROM sognefakta
WHERE (Sogn LIKE ? OR Sogn LIKE ?)
$where

EOT;

Open in new window

0
Chris StanyonWebDevCommented:
Your query will return records for completely DISTINCT records, based on the columns you SELECT.  So even if 17 out of the 18 fields are the same, you will still get 2 records, because they're not seen as DISTINCT.

Sounds like you have a couple of options really:

1: Change your query to only select the parts of the record that need to be unique (you can then use DISTINCT).
2: Pull down your data as-is and then manipulate it in PHP to rebuild an array, stripping out any duplicated data that you don't need.
0
Peter KromanSales ExecutiveAuthor Commented:
Thanks,

Now I am a little wiser on DISTINCT :)
I think I have one more option though.

I could change the database structure so that i make the record unique, and place the the varying information in separate posts inside the record, in stead of making almost identical records that only is different on a few posts.

Do you agree?
0
Chris StanyonWebDevCommented:
Hey Peter,

That would be the preferred option. Thought I'd mentioned about normalising your db in my last comment, but it seems I thought it and didn't type it :)

Any time you find duplicate data in your DB, it's time to think about normalising it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.