Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 8th, 2008, 01:35 PM
Stewart Robert Hinsley
Guest
 
Posts: n/a
Default what am I missing? (arrrays)

I had revised some of my web pages

1) generate content from a CSV file using PHP
2) use PHP classses

for ease of maintenance.

I was looking at one of the scripts yesterday, as pages using the script
seem to be rather sluggish (it doesn't help that the CSV file is 0.5Mb),
and found that it was only working by accident.

The relevant bits are shown below. SelectGenus() specifies which parts
of the CSV file are of interest to the page, and ReadData() then reads
them in. Option 1 works; option 2 doesn't. (Option 2 was appearing to
work, because $a[19] is nearly always the null string, and
$this->Genera[0] seems to be the same.) I don't understand what I am
missing.

To confuse me further, the same construct as option 2 appears to be
working successfully in another script, except that it has the
equivalent of $this->Genera[0]['genus'] instead - but changing this
script to that format doesn't change anything. And if I print out $a[10]
inside the loop is has the correct value with option 1, but is a null
string with option 2. ($a[0] has the correct value in both cases]

class SynonymyData
{
var $Genera;
var $gen_no;
var $SData;

function SelectGenus($genus)
{
for ($i = 0; $i < $this->gen_no; $i++)
if ($genus == $this->Genera[$i])
break;

if ($i == $gen_no)
$this->Genera[$this->gen_no++] = $genus;
}

function ReadData()
{
global $path;

$IP = fopen("$path/Data/Synonyms.csv", "r");
if (!$IP)
{
echo "can't open /Data/Synonyms.csv: error
$OS_ERROR\n";
return;
}
#
# Skip column headings
#
$buffer = fgets($IP, 1024);

while ($a = fgetcsv($IP, 1024, ","))
{
foreach ($this->Genera as $i =$v) # option 1
# for ($i = 0; $i < $this->gen_no; $i++) # option 2
if ($a[0] == $this->Genera[$i]
|| $a[10] == $this->Genera[$i]
|| $a[19] == $this->Genera[$i])
{
$this->FillDataEntry($a);
break;
}
}

fclose($IP);
}
}

$Sy = new SynonymyData;
$Sy->SelectGenus("Asterotrichion");
$Sy->ReadData();

--
Stewart Robert Hinsley
  #2  
Old October 8th, 2008, 02:05 PM
Stewart Robert Hinsley
Guest
 
Posts: n/a
Default Re: what am I missing? (arrrays)

In message <T7lAlwDfFK7IFwCW@meden.invalid>, Stewart Robert Hinsley
<{$news$}@meden.demon.co.ukwrites
Quote:
>I had revised some of my web pages
>
>1) generate content from a CSV file using PHP
>2) use PHP classses
>
>for ease of maintenance.
>
>I was looking at one of the scripts yesterday, as pages using the
>script seem to be rather sluggish (it doesn't help that the CSV file is
>0.5Mb), and found that it was only working by accident.
>
>The relevant bits are shown below. SelectGenus() specifies which parts
>of the CSV file are of interest to the page, and ReadData() then reads
>them in. Option 1 works; option 2 doesn't. (Option 2 was appearing to
>work, because $a[19] is nearly always the null string, and
>$this->Genera[0] seems to be the same.) I don't understand what I am
>missing.
>
>To confuse me further, the same construct as option 2 appears to be
>working successfully in another script, except that it has the
>equivalent of $this->Genera[0]['genus'] instead - but changing this
>script to that format doesn't change anything. And if I print out
>$a[10] inside the loop is has the correct value with option 1, but is a
>null string with option 2. ($a[0] has the correct value in both cases]
>
>class SynonymyData
>{
var $Genera;
var $gen_no;
var $SData;
The answer bubbled up from the unconscious while I was busy gardening -
it turns out that I haven't properly internalised the "everything's a
string" data model; I should have written "var $gen_no = 0;"; in the
absence of this the key to the first array entry was "", rather than 0.
--
Stewart Robert Hinsley
  #3  
Old October 8th, 2008, 02:35 PM
Erwin Moller
Guest
 
Posts: n/a
Default Re: what am I missing? (arrrays)


Stewart Robert Hinsley schreef:
Quote:
In message <T7lAlwDfFK7IFwCW@meden.invalid>, Stewart Robert Hinsley
<{$news$}@meden.demon.co.ukwrites
Quote:
>I had revised some of my web pages
>>
>1) generate content from a CSV file using PHP
>2) use PHP classses
>>
>for ease of maintenance.
>>
>I was looking at one of the scripts yesterday, as pages using the
>script seem to be rather sluggish (it doesn't help that the CSV file
>is 0.5Mb), and found that it was only working by accident.
>>
>The relevant bits are shown below. SelectGenus() specifies which parts
>of the CSV file are of interest to the page, and ReadData() then reads
>them in. Option 1 works; option 2 doesn't. (Option 2 was appearing to
>work, because $a[19] is nearly always the null string, and
>$this->Genera[0] seems to be the same.) I don't understand what I am
>missing.
>>
>To confuse me further, the same construct as option 2 appears to be
>working successfully in another script, except that it has the
>equivalent of $this->Genera[0]['genus'] instead - but changing this
>script to that format doesn't change anything. And if I print out
>$a[10] inside the loop is has the correct value with option 1, but is
>a null string with option 2. ($a[0] has the correct value in both cases]
>>
>class SynonymyData
>{
> var $Genera;
> var $gen_no;
> var $SData;
>
The answer bubbled up from the unconscious while I was busy gardening -
it turns out that I haven't properly internalised the "everything's a
string" data model; I should have written "var $gen_no = 0;"; in the
absence of this the key to the first array entry was "", rather than 0.
Gardening is the best bugfix ever.
As is taking a shower, walk the dog, etc. ;-)

Glad you fixed it.

Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles