Jump to content

Displaying data from a flat file?


Jam87

Recommended Posts

Hi, ive got a flatfile database sorted out, all i need now is how to store individual flat files per user and how to paginate that data.

 

Just to make sure i'm doing everything right, i'll post the code below

 

Form used:

<form name="input" method="post" action="add.php">
<p>
<label for="name">Name: </label>
<input type="text" name="name" id="name" />
<label for="number">Number: </label>
<input type="text" name="number" id="number" />
<p><input type="reset" name="reset" value="reset" /> 
<input type="submit" name="submit" value="submit" /></p>
</form>

 

add.php:

 

<?php

$name = $_POST['name'];
$number = $_POST['number'];

$filename=$user . "data/pbook/.txt";
$fp=fopen($filename, "a");

if(!$fp) {
    echo 'Error: Cannot open file.';
    exit;
}

fwrite($fp, $name."||".$number."\n");

fclose($fp);

header('Location: pview.php');

?> 

 

display:

 

<?php
$filename=$user . "data/pbook/.txt";
$fp=fopen($filename, "r");
if (!$fp) {echo 'ERROR'; exit;}

while (!feof($fp)) {
$line = fgets($fp, 1024); //use 2048 if very long lines
list ($name, $number) = split ('\|', $line);
echo '

<p>
'.$name.'
'.$number.'
</p>
';


$fp++;
}

fclose($fp);

?>

thanks for any help

 

Jam87

Link to comment
https://forums.phpfreaks.com/topic/43557-displaying-data-from-a-flat-file/
Share on other sites

I would have expected to see filenames like this

$filename="data/pbook/$name.txt";

so that Fred's file is "/data/pbook/fred.txt"

 

Your final display code (pview.php) also refers to $user, but it isn't defined anywhere.

 

At the end of add,php, use

 

header('Location: pview.php?name=$name');

 

Then at start of pview.php

$name = $_GET['name'];
$filename="data/pbook/$name.txt";

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.