By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

what am I missing? (arrrays)

P: n/a
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
Oct 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In message <T7**************@meden.invalid>, Stewart Robert Hinsley
<{$******@meden.demon.co.ukwrites
>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
Oct 8 '08 #2

P: n/a

Stewart Robert Hinsley schreef:
In message <T7**************@meden.invalid>, Stewart Robert Hinsley
<{$******@meden.demon.co.ukwrites
>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/
============================
Oct 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.