Jump to content

flat file


Dorky

Recommended Posts

got a file, lets call it file a. file a has links stored in it so they can be used by include to populate a links list managed by the site owner. i need to figure out how to make the entry go to the next line instead of inline every time it gets added to. and how to delete the line from the array i pull it into. i know how to pull each line into the array and how to write each link to it, just not real well at organizing it.

Link to comment
Share on other sites

When you're writing a new line to the file, add this to the end "\n" without the quotes, to the end.  Here are a few examples, depending on how you're writing the url:

 

$newline = $someurl."\n";

 

or

 

$newline = "http://example.com/".$somepage."\n";

 

or

 

$newline = "http://example.com/somepage.html\n";

 

Either way, append "\n" (again, without the quotes) to the end of each new line you add.

Link to comment
Share on other sites

sweet, i just realized to pull the same data so i can populate the delete list i need to create a 2x array for each entry. one $var for the name and one $var for the url so i can pull from the same file to populate the delete list and the visitor side. i will need to be able to pull by $var[] and not sure how yet im going to give a distinction to each array. an array of arrays?

 

any advice on engineering this is very appreciated.

Link to comment
Share on other sites

ok im down to this. it returns the altered array but i dont have a clue about how to get this back into a string with the top level array ending in /n and deviding the lower level array with ||

 

$dumplink = $_GET['access'];
$dumplinkinfo = file("$dumplink.html");
foreach($dumplinkinfo as $dumplinkkey => $dumplinkval) 
{
   $dumplinkdata[$dumplinkkey] = explode("||", $dumplinkval);
}
for($dumplinkk = 0; $dumplinkk < sizeof($dumplinkinfo); $dumplinkk++) 
{
unset($dumplinkdata[$dumplinkk][0]);
unset($dumplinkdata[$dumplinkk][1]);
unset($dumplinkdata[$dumplinkk]);
print_r($dumplinkdata);
}

 

linkname || linkurl.com

linkname || linkurl.com

linkname || linkurl.com

linkname || linkurl.com

 

is the desired flat file format im trying to get the array down to.

Link to comment
Share on other sites

something like this:

 

<?php
$dumplink = $_REQUEST['access'];
$dumplinkinfo = file_get_contents("$dumplink.html"); // This will return the file into a string
$dumplinkinfo = nl2br($dumplinkinfo); // This converts all newline characters with line-breaks (<br>'s)
$dumplinkinfo = explode("<br />", $dumplinkinfo); // This will break the string into lines by line-break characters and put it into an array
foreach($dumplinkinfo as $dumplinkline)  { // Iterate through the array
  $linkline = explode(" || ", $dumplinkline); // Breaks the string up by " || " characters and puts the results into an array
  $linkname[] = $linkline[0]; // Assigns the first value to the $linkname array
  $linkurl[] = $linkline[1]; // Assigns the second value to the $linkurl array
  }

print_r($linkname); // Print the $linkname array
print_r($linkurl); // Print the $linkurl array
?>

 

I tested this with this string:

 

link1 || http://link1.com
link2 || http://link2.com
link3 || http://link3.com
link4 || http://link4.com

 

And it returned 2 arrays, $linkname and $linkurl.  To parse through these, given that you know the location (line number, starting with 0) of the link you want, do this:

 

$getlink = $_REQUEST['link']
$link_name = $linkname[$getlink'];
$link_url = $linkurl[$getlink];

 

Simple enough.  A question for you now, is there a reason you're using a flatfile and not an actual database?

 

I also know a way to make is so you can call up a link by it's name and not by it's number, if you're interested let me know.

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.