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
https://forums.phpfreaks.com/topic/9395-printing-an-array/
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
https://forums.phpfreaks.com/topic/9395-printing-an-array/#findComment-34615
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
https://forums.phpfreaks.com/topic/9395-printing-an-array/#findComment-34680
Share on other sites

Archived

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

×
×
  • 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.