while loops

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

[color=\"#000000\"]You\'re probably trying to confuse it a bit much. Try this: [code]<?php

$tnum = "Five";
$file = "Cups.txt";
$found = FALSE;
$desc = find_desc($tnum, $file);
echo $desc;

function find_desc($text, $file)
{
$lines = file($file);

foreach ($lines as $line_num => $line) {
$linenum = (int) $line_num;
$linenum++;
$t = strpos($line, $text);
if ($found === TRUE)
{
echo $line;
}
if($t !== false)
{
$found = TRUE;
}
}
}

?>[/code]

Not tested it, but it *should* work <img src=\'http://www.killanet.net/forum3/public/s ... unsure.gif\' class=\'bbc_emoticon\' alt=\':unsure:\' /> [/color]
Last edited by Scott on Mon Sep 26, 2005 3:34 pm, edited 1 time in total.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]err...that code returns the entire contents of the file <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' /> lol thanks anyway?[/color]
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

Ah, yeah. After echo $line; put $found = FALSE; That should stop it returning anything else.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]...now it returns nothing :| [/color]
Image

Image
radar
Hero Member
Hero Member
Posts: 632
Joined: Thu Jun 17, 2004 12:37 am

while loops

Post by radar »

Why are cups kept in a file? :\
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]probably for the same reason that suits, swords and shields are...


that reason? nfi tbh <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />[/color]
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

[code]<?php

$tnum = "Five";
$file = "Cups.txt";
$desc = find_desc($tnum, $file);
echo $desc;

function find_desc($text, $file)
{
$found = FALSE;
$lines = file($file);

foreach ($lines as $line_num => $line) {
$linenum = (int) $line_num;
$linenum++;
$t = strpos($line, $text);
if ($found === TRUE)
{
echo $line;
return;
}
if($t !== false)
{
$found = TRUE;
}
}
}

?>[/code]

All workie. At least, thats what I think you\'re trying to do.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]...it still doesnt show anything <img src=\'http://www.killanet.net/forum3/public/s ... #>/cry.gif\' class=\'bbc_emoticon\' alt=\':(\' /> [/color]
Image

Image
radar
Hero Member
Hero Member
Posts: 632
Joined: Thu Jun 17, 2004 12:37 am

while loops

Post by radar »

Can I have a copy of the file please?
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]Cups.txt? sure... [url=\"http://vercz.killanet.net/tst/Cups.txt\"]http://vercz.killanet.net/tst/Cups.txt[/url] =) [/color]
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

Ah...that makes sense. It is working, working too well. The script will print the line directly following the word \"Five\". Since your following line is a blank line...you see a blank line. Remove the empty lines and it\'ll work <img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]...im not stupid <img src=\'http://www.killanet.net/forum3/public/s ... el_not.gif\' class=\'bbc_emoticon\' alt=\'(not)\' />


thanks scott <img src=\'http://www.killanet.net/forum3/public/s ... iggrin.gif\' class=\'bbc_emoticon\' alt=\':D\' /> and you too radar <img src=\'http://www.killanet.net/forum3/public/s ... iggrin.gif\' class=\'bbc_emoticon\' alt=\':D\' /> <img src=\'http://www.killanet.net/forum3/public/s ... >/good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' /> <img src=\'http://www.killanet.net/forum3/public/s ... >/good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' /> [/color]
Image

Image
Post Reply

Return to “PHP &amp; MySQL”