Jump to content


Photo

Printing an array


  • Please log in to reply
4 replies to this topic

#1 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 09 May 2006 - 01:52 PM

Hello,
I'm new to php and I have been having difficulty :P I have something similar to a shoutbox and in it I have 4 fields; User, Tag, Date, and Title.
My problem is when I try to display the string. I can't get it formatted the way I want and I can't find a tutorial out there that describes what I need.


<?php

$name = $_POST["name"];
$tag = $_POST["tag"];
$title = $_POST["title"];
$newdate = date("H:i");

if($name && $tag)
    {
$file = "blog.dat";
$filecopy = $title . ".dat";
$newtxt = ($name . "," . $newdate . "," . $tag . "," . $title ."\n");

.....

<?php
$boardarray = file("blog.dat");
while (list(,$oneline) = each($boardarray)) 
{
    $name = substr($oneline,0,strpos($oneline,","));

    /*$sdate = substr($online,strpos($oneline,",")+1,strlen($oneline));
    $tag = substr($online,strpos($sdate)+1,strlen($oneline));*/

    $tag = substr($oneline,strpos($oneline,",")+1,strlen($oneline));
    if ($name && $tag) echo "<b>" . $name . "</b>" . $tag .   "<br />";
}
?>

.....

Here is what I have been trying to get working. What is happening is it formats the User only and afterwards just posts the rest of the data in the file. I think the problem is in that I don't know how to set up the string commands to start reading at the second comma (since it's a comma delimited file).

Any help would be much appreciated.

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 09 May 2006 - 02:25 PM

looks like the long way arround. If you aren't oppsoed to arrays, you could just explode the contents:

while (list(,$oneline) = each($boardarray)) 
{
    $myarray=explode(",",$oneline);
    list($name,$newdate,$tag,$title) = $myarray;
    if ($name && $tag) echo "<b>" . $name . "</b>" . $tag .   "<br />";
}


#3 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 09 May 2006 - 05:29 PM

Thanks. I will try this and post my results. But this is all new to me so I don't really know what I'm doing exactly.

Ok,it worked ^_^ thanks a lot. But I was wondering if I could reverse the order it was displayed in so that the last tag is the first displayed

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 09 May 2006 - 06:14 PM

typically, sorting is not practical with text files.

you could write it in reverse order. backup your text file, then try the following:


$filedata=file_get_contents($file); // will copy the existing file to a varialbe.

then open your file using the "w" flag (write, file pointer at beginning), write the content, then write the $filedata variable behind it.

check your text file -- the last entry should be first, followed by the remainder of the file.

It's not perfect (you still can't sort), but the data is in the order you want.

#5 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 09 May 2006 - 10:04 PM

$stringData = $newtxt;
$stringData .= $filedata;

Thanks alot! I got it to work after figuring out what the '.=' does lol.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users