Forum Moderators: coopster

Message Too Old, No Replies

Checking for double vars

         

xray012

8:11 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



Hello, i have a links.txt containing links and 3 keywords for each link, now i want to make a file containing the keywords, but dont want any keyword more than once.
the script below works, but i get the line i want, atleast a 100 times?, whats my mistake?

<?php

$thefile = fopen("data/links.txt", "r") ;

$no=1 ;
$row=1 ;

while ($data = fgetcsv ($thefile, 250, ":")) {
$num = count($data);
$row++;

$keyword[$no]=$data[4] ; $no++;
$keyword[$no]=$data[5] ; $no++;
$keyword[$no]=$data[6] ; $no++;

}

for ($a=0; $a<$no; $a++) { for ($b=0; $b<$no; $b++) { if ($keyword[$b]!= $keyword[$a]) { $output = $output . "$keyword[$b]:"; } } }

echo $output ;




?>

Also why cant i do something like: $thefile="/data/links.txt" >> fopen($thefile, 250, ":")

If aynone can help, great

xray012

10:00 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



k, ive solved my problem with the root-path, i found it afterall, great how almost all topics are to be found here.

[webmasterworld.com...]

i'me wondering if ime missing a simple sollution to my comparison problem? :S

jatar_k

10:07 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld xray012,

I have to say that crazy loop is hard to follow. Have you tried echo'ing the contents of what you are testing to see if it is just using/writing the wrong var?

That's usually the way I go about it to find mismatch probs. I am usually a victim of my own insanity and use the wrong var once in a while just to drive myself batty and always in the most complex little loop of the whole mess. ;)

jatar_k

10:13 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ok, this is what I see, tell me if I am insane or not

open the file
read a row (element 4,5,6 are the keywords)
add each keyword to the massive keyword array
go through the keyword array and check for duplicates
add all unique keywords to output
spit out output

that right? with a little confirmation i may have an idea

xray012

10:45 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



yes jatar thats what it was intended for, i couldn't come up with another sollution.
(does massive array's, have a downside)

the $output i get is a string with no duplicates, that gets re-produced atleast a 100 times?

any ideas welcome :)

[edited by: xray012 at 10:57 pm (utc) on Dec. 10, 2003]

jatar_k

10:54 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about something like this
[pre]$thefile = fopen("data/links.txt","r");
$no=0;
while ($data = fgetcsv ($thefile, 250, ":")) {
for($i=4;$i<=6;$i++) {
$keyword[$no]=$data[$i];
$no++;
}
}
$keyuniq = [url=http://ca.php.net/manual/en/function.array-unique.php]array_unique[/url]($keyword);
foreach($keyuniq as $newkey) $output .= "<br>" . $newkey;
echo $output;[/pre]

xray012

11:07 pm on Dec 10, 2003 (gmt 0)

10+ Year Member



Works Perfect Indeed!, thank you o'so much :)

what a nice piece of programing,

-could you advise a good php-manual-book to me?

jatar_k

11:19 pm on Dec 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



all I have ever used is php.net and tons of trial and error but try these

[webmasterworld.com...]
[webmasterworld.com...]

xray012

12:58 am on Dec 14, 2003 (gmt 0)

10+ Year Member




the array_unique was unknown too me, guess ill have to do more research on functions ;)

i ame a frequent visitor to php.net too, also phpfreaks has helped me out on occasions.

thnx again!

ergophobe

4:08 pm on Dec 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Xray

go to PHP.net and download the help file. Put it on your desktop one click away and pay frequent homage. Amazing what you can learn in a short while!

Tom

xray012

1:42 pm on Dec 15, 2003 (gmt 0)

10+ Year Member



Thanx ToM, i'v found that one too :)

jatar_k

5:48 pm on Dec 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have gotten into reading the changefile for new releases of php too. I can't tell you how many times I have found a function that I have had to custom build in the past.

Sometimes when I have a little time I go through the functions in the manual to see if I can find something exciting or to just see how I could use some of the functions that I don't normally play with.

The user comments for most of the functions are full of tricks as well.

ergophobe

4:19 pm on Dec 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I should do that too (read the release notes). It happens from time to time that someone posts something here and I'm about to write "That's not valid PHP", then I check the most recent documentation and lo and behold it's some new feature or something.

Tom

jatar_k

4:42 pm on Dec 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



lol, I've done the same thing myself, I always hope I haven't hit submit yet. ;)