Jump to content

Printing an array


Aftermath

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.