Re: [PHP] [newbie] minor trouble with "while, list and explode" from a text-file

2001-02-06 Thread David Robley

On Wed,  7 Feb 2001 14:14, SED wrote:
> Hi, I'm trying to figure out how to do this right:
>
> I have this text file:
>
>   date 1 | head 1 | text 1
>   date 2 | head 2 | text 2
>   date 3 | head 3 | text 3
>
> And I want to display in a HTML-page like this:
>
>   date 1
>   head 1
>   text 1
>   date 2
>   head 2
>   text 2
>   date 3
>   head 3
>   text 3
>
> And I'm trying to do it with follwing code:
>
> //sniped
> 39>   while (list ($line_num, $line) = each ($contentlines))
> 40>   {
> 41>   $message = explode ("|", $line);
> 42>
> 43>   while (list ($date, $head, $text) = each($message))
> 44>   {
> 45>   echo "$date, $head, $text";
> 46>   };
> 47>   };
> //sniped
>
> But I get this result:
>
>   Warning: Undefined offset: 2 in c:\inetpub\wwwroot\read_txt_0007.php on
> line 43
>
>
> Can anyone point me to a solution for this?
>
> Regards,
> Sumarlidi Einar Dadason

List returns a key and value for each element in the array on which it 
operates. The array that you get from
 
$message = explode ("|", $line)

will be something like
KEYVALUE
0  date1
1  head1
2  text1

with spaces around the text (which you may want to chop()) so you need 
something like
41> $message = explode ("|", $line);
42>
43> while (list (, $value) = each($message))
44> {
45> echo "chop($value)";
46> };
The comma in Line 43 is important!

Alternatively, you could use str_replace to change the ' | ' (that's space 
pipe space) to 

Cheers
-- 
David Robley| WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES  | http://www.nisu.flinders.edu.au/
AusEinet| http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] [newbie] minor trouble with "while, list and explode" from a text-file

2001-02-06 Thread SED

Hi, I'm trying to figure out how to do this right:

I have this text file:

date 1 | head 1 | text 1
date 2 | head 2 | text 2
date 3 | head 3 | text 3

And I want to display in a HTML-page like this:

date 1
head 1
text 1
date 2
head 2
text 2
date 3
head 3
text 3

And I'm trying to do it with follwing code:

//sniped
39> while (list ($line_num, $line) = each ($contentlines))
40> {
41> $message = explode ("|", $line);
42>
43> while (list ($date, $head, $text) = each($message))
44> {
45> echo "$date, $head, $text";
46> };
47> };
//sniped

But I get this result:

Warning: Undefined offset: 2 in c:\inetpub\wwwroot\read_txt_0007.php on
line 43
0, date 1,
Warning: Undefined offset: 2 in c:\inetpub\wwwroot\read_txt_0007.php on
line 43
1, head 1,
Warning: Undefined offset: 2 in c:\inetpub\wwwroot\read_txt_0007.php on
line 43
2, text 1,
Warning: Undefined offset: 2 in c:\inetpub\wwwroot\read_txt_0007.php on
line 43
0, date 2,
etc.

Can anyone point me to a solution for this?

Regards,
Sumarlidi Einar Dadason

SED - Graphic Design

--
Phone:   (+354) 4615501
Mobile:  (+354) 8960376
Fax: (+354) 4615503
E-mail:  [EMAIL PROTECTED]
Homepage:www.sed.is <- New Homepage!
--


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]