why this script dont work?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Angel
Forum Newbie
Posts: 3
Joined: Sat May 25, 2002 8:42 am
Contact:

why this script dont work?

Post by Angel »

i use 2 txt files , wanna asing 1 lines of txt1 to fist line of txt 2.
place.txt is as folow:
a
b
c
destination.txt is as folow:
brussels
paris
madrid

this the code i think should work:

Code: Select all

<?php
$i= 0;
$fd = fopen ("place.txt", "r");
while (!feof ($fd)) {
    $locationї$i] = fgets($fd, 4096);
    $i++;
}
$i= 0;
fclose ($fd);
$fd = fopen ("destination.txt", "r");
while (!feof ($fd)) {
    $placeї$i] = fgets($fd, 4096);
    $i++;
}
fclose ($fd);
$result_des = array($locationї0] => $placeї0] , $locationї1] => $placeї1] ) ;
echo "The first line destination is {$result_desї"a"]}.";
?>
result should be brussels

thanx for helping
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Firstly it helps if you explain what error messages you are getting and what the output is if it isn't what you want.

Ok, now try some debugging by placing the following below the "$result_des = ..." line:

Code: Select all

echo '<pre>';
print_r($result_des);
echo '</pre>';
This results in the following:

Code: Select all

Array
(
    їa
] => brussels 

    їb
] => paris 

)
As you can see there is a new line in each string so you need to remove this, alter the following lines:

Code: Select all

$locationї$i] = fgets($fd, 4096);
and
$placeї$i] = fgets($fd, 4096);
to

Code: Select all

$locationї$i] = trim(fgets($fd, 4096));
and
$placeї$i] = trim(fgets($fd, 4096));
The trim function removes the linebreaks.

Hope this helps,
Mac
Post Reply