Jump to content

Archived

This topic is now archived and is closed to further replies.

Aftermath

Printing an array

Recommended Posts

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.

[code]

<?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 />";
}
?>

.....
[/code]

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.

Share this post


Link to post
Share on other sites
looks like the long way arround. If you aren't oppsoed to arrays, you could just explode the contents:

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]
$stringData = $newtxt;
$stringData .= $filedata;
[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.