Forum Moderators: coopster
I have a flat file that contains the pagename, body text, and image for each page. I can user $REQUEST_URI to get the actual filename (as 'basename'). but then I cant to read through the array and if the actual filename is X, then populate the page using the correct line/entry in the database.
Yes, I have to use a flat file. I could code each page separately, but I've got this handy flat file with all the info in it already.(see paragraph below)
I know how to explode the flat file into an array and use a foreach statement to populate a table using a counter and a loop...but I'm stymied as to how to use the basename variable to select the correct "entry" from the array.
Does anyone know how to do this, or even what search terms I should be using to google this info.
Thanks,
AuntieMame
How are you storing the information in the flat file (ie: where is the filename associated with each page's content located)? You must first know where the information is in order to run a comparison to retrieve it. I guess we'll see :)
the file contents look like this:
classnumber¦¦description¦¦date¦¦price
1001¦¦introductory class¦¦01/01/2007¦¦$50.00
1002¦¦next level class¦¦02/01/2007¦¦$50.00
I'm using the file to populate a table using a foreach statement and a loop to determine the number of time to run the statement. What I'd /like to do is (and hope this is possible) is to create links in the table using the database contents (which I know how to do) and have each class link to an individual page about the class. For example, the 1001 class would link to 1001.html, and 1001.html would have a call to the database.txt file and only put the info about class 1001 on that page.
so I figured out how to place the pagename in a variable:
$page = (basename ($REQUEST_URI));
and I already know how to explode the test file:
//takes data and creates a long string
$array = file("database.txt");
//count the number of entries in the array
$result = count($array);
foreach($array as $key => $value)
{
//explode the array into a new array
$data[$key] = explode("¦¦", $value);
}
{
//initialize variable as zero. loop through variable and increase
// each time. during the loop check the variable to make sure it is
// less than the column number.
for($i = 0; $i < $result; $i++)
but I don't know how to do is compare the $page variable to the array and only use the "line"/"entry" for the class that matches $page
I've tried so many things that I've completely confused myself. My last attempt was eregi, but that didn't work for me either.
Any ideas or suggestions would be *greatly* appreciated.
Thanks
<?php
$page = basename [php.net]($_SERVER['REQUEST_URI'], '.html');
$file = file [php.net]('database.txt');
foreach ($file as $line) {
// Get the headings from line one of the file:
if (!isset [php.net]($headings)) {
$headings = explode [php.net]('¦¦', $line);
continue [php.net];
}
$line = explode [php.net]('¦¦', $line);
$lines[array_shift [php.net]($line)] = $line;
}
print '<pre>'; print_r [php.net]($headings); print '</pre>';
print '<pre>'; print_r [php.net]($lines); print '</pre>';
print '<table>';
print '<tr><th>' . implode [php.net]('</th><th>', $headings) . '</th></tr>';
print '<tr><td>' . implode [php.net]('</td><td>', $lines[$page]) . '</td></tr>';
print '</table>';