|
|
Author |
Thread |
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 04-04-2001 00:58
I'm using PHP, and I want to write a script to take the row entries of a flat text file, break it into groups of 10 rows each, and display them with a next and previous at the bottom of the page ( i.e. search engine style). I could probably accomplish this with MySQL, which I don't have for this project, but don't quite know where to start on a text file. If you would please just send me in the right direction, I would be grateful.
Thanks
|
bitdamaged
Maniac (V) Mad Scientist
From: 100101010011 <-- right about here Insane since: Mar 2000
|
posted 04-04-2001 19:50
What you need is the split command. set everything in the file to one string variable
then use the split command.
my_array = split("\n", $my_string);
the thing here is I don't think back slash characters work in php so you need to get the PHP regular expression version of a line break. Hmm must do more research.
Walking the Earth like Kane
|
WarMage
Maniac (V) Mad Scientist
From: Rochester, New York, USA Insane since: May 2000
|
posted 04-04-2001 19:52
Reading from a file is not as hard a task as one may first think.
To simply read from a file you would use a fopen function.
$filename = "/samplefile.txt";
$openedfile = fopen($filename, "r") or die ("Error: could not open file.");
$filecontent = fread($openedfile,filesize($filename));
$fclose($openedfile);
That chunk of code would then place the entire content of the file into the variable $filecontent, using the "r" (read) method, which places the file cursor at the beginning of the file.
Since you now have the entire content into a file you can cut the file up using substring commands. So for this you would have to determine column length for a row, this is up to you, an easy way would be to determine an arbitrary row size, for example 50 columns per row, making the total file size 500 characters.
then to cut it up. (not sure if you can use double incrementers, so I will assume you can not)
$j = 0;
for ($i; i < filesize($filename); i+=500)
{
$block[$j] = $filecontent.substring($i,$i+500);
$j++;
}
That would then get you your different blocks of 500 characters into the array $block.
This is probabally not exactly what you want, because if a word is cut in half at the 500 character mark you will run into some problems. This can be solved in a number of different ways, going only 497 characters and then appending "..." to the end. Or maybe even searching for a space between 490 and 500 and then cutting the string there, and incrementing by the position (if a white space does not exist then increase the search size to 480 to 500, and then more if needed [while statement]).
The hardest part of this would be to come up with a good system for forwards and back.
One way would be to append information to the link.
I will explain it, yet not implement it.
<a href="#?i=<? echo "$i" ?>">Next</a>
By linking like this, you link to the same page. From this you pass the value of $i in the above for statement, and you can use this value to do your next block. So instead of a forstatement you simply use a substring.
$content_block = $filecontent.substring($i,$i+500);
$i += 500;
This will then incremenet $i by 500 and you have your content block, when they press the next button it will use the final value of $i. an example is in order.
You start the program, an if statement checks to see if $i exists, if not it creates $i and initializes to 0.
Since this is the first run it does not detect $i and therefor initializes it to 0
The program then runs,
$content_block = $filecontent.substring(0,500);
$i = 500 now.
<a href="#?i=500">Next</a>
They click next, the if statement now detects and $i and does nothing
$contnent_block = $filecontnent.substring(500,1000);
$i = 1000 now.
<a href="#?i=1000">Next</a>
And I think you now get the idea.
There is a lot more work that would need to be put into this, and you will probabally find a more effective way to do this. This is just an idea off of the top of my head, so don't take this as a final answer. I know that using MySQL this would be a much easier process, but as you said you have to deal with what you have.
Good luck, this will be a moderately hard project, if not closing in on advanced, I am sure you will have a lot of fun with it.
-mage-
|
WarMage
Maniac (V) Mad Scientist
From: Rochester, New York, USA Insane since: May 2000
|
posted 04-04-2001 19:53
\n does work, and bits explaination with the split method would work much easier.
|
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 04-04-2001 21:26
Thanks guys. I started off on my own while I waited for replies. Look this over for me please. I'm trying to do this with a csv text file. This works to a certian extent (just working on first page load now, not next or prev). The only thing is, I don't seem to be able to get it to loop through more than one iteration of the while loop. The script as it is now loads the page ok, and gives me one set of values from the csv file on the page.
<?
// first time page loads
if((!$next) && (!$prev)){
$next = 0;
$i = 0;
$pageLoop = $i + 3;
$j = $i * 3;
$next += $pageloop;
// page loaded at least once and the next 3 are being requested, $next should have a value
}elseif($next){
$i = $next;
$pageLoop = $i + 2;
$j = $i * 3;
$next += $pageloop;
// page loaded at least once, and the request is for prev 3, $next should have a value
}elseif($prev){
$i = $next - 3;
$pageLoop = $i + 2;
$j = $i * 3;
$next += $pageloop;
}
$filename = "winners.csv";
$fp = @fopen($filename, "r") or die("Couldn't open file for Winners.");
while($row = @fgetcsv($fp, 1000, "
|
linear
Paranoid (IV) Inmate
From: other places Insane since: Mar 2001
|
posted 04-05-2001 05:25
goddam that's a lot of code to do something simple. how come you're not passing $rows and $offset? then show rows $offset through ($offset+$rows).
<?php
$rows=30;
if (!$offset){$offset=0;}
$f = file("winners.csv");
for ($i=$offset;$i<=($offset+$rows);$i++) {
echo $f[$i]
}
// do some math, and print some links to yourself here
$newoffset=$offset+$rows;
echo <A href=\"#PHP_SELF?rows=$rows&offset=$newoffset\">next</A>
?>
like ten lines of code, man.
|
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 04-05-2001 21:59
To answer your question linear, I'm new to PHP, and relatively new to coding period. That could be why my code may be wordy. Forgive me if I've offended your programming knowledge.
Now, if you would care to enlighten someone looking to learn, I'd be happy to let you.
- I don't know how $rows = 30; applies to what I'm trying to do.
- I don't see anything in your code that breaks the csv file I'm using for a database, into a useable array.
- Your for() statement doesn't seem to address the fact that I only want three sets of data from my csv file printed to the screen, not the whole file.
- I don't see anything that provides for a previous link for the page.
- The csv file is not html formatted, so I needed to take care of that in my for loop.
Don't get me wrong, if your code does all that I need it to do, I would love an explanation, so I could learn to write less code for myself. If it does, and you feel up to it, I would be appreciative.
Thanks
[This message has been edited by butcher (edited 04-05-2001).]
|
linear
Paranoid (IV) Inmate
From: other places Insane since: Mar 2001
|
posted 04-06-2001 23:56
No offense taken. And no insult to your code was intended. What I was getting at, is that often stating the problem correctly will lead you directly to the best coding solution. My real beef with your code is that it doesn't express what you're *really* trying to do.
Problem statement: Display an N lines from a file starting with line M .
To do it, you effectively need to know two things: how many lines you want to show, and how far from the beginning of the file you want to start. In your case, it sounds like you always want 3 lines.
Again, with comments:
<?php
// this script expects to be called as showwinners.php?offset=3
$rows=30; // or 3, or whatever you like; how many rows to show
if (!$offset){$offset=0;} // don't assume arguments are provided
$f = file("winners.csv"); // this inhales the *whole file* into a convenient array, $f
// you will want some error handling here, too
for ($i=$offset;$i<=($offset+$rows);$i++) { // this shows $rows rows, starting with row $offset
echo $f[$i] // I'm just displaying the row, you will format it to your needs here, inside the for loop
}
// do some math, and print some links to yourself here
$nextoffset=$offset+$rows;
$prevoffset=$offset-$rows;
echo <A href=\"#PHP_SELF?rows=$rows&offset=$nextoffset\">next</A> // here's a next button link
echo <A href=\"#PHP_SELF?rows=$rows&offset=$prevoffset\">prev</A> // and a prev button
?>
This is a problem that you will tend to solve over and over.
butcher: "I don't know how $rows = 30; applies to what I'm trying to do."
don't hardcode things into your script. Put them in a variable, and you can change $rows to 6, 9, or anything you want later.
butcher: "I don't see anything in your code that breaks the csv file I'm using for a database, into a useable array."
Right. I'll let you do *some* of the work. That part goes inside the for loop.
butcher: "Your for() statement doesn't seem to address the fact that I only want three sets of data from my csv file printed to the screen, not the whole file."
Your right. It assumes you want $rows sets of data. You define $rows.
butcher: "I don't see anything that provides for a previous link for the page."
*sigh* since it's very similar to the next link (change + to -), I thought it would be apparent.
butcher: "The csv file is not html formatted, so I needed to take care of that in my for loop."
That's right. That's not the hard problem. Passing the offset (and optionally rows) to the script *totally eliminates* the need for the if-else-elseif business you cooked up. I also have no earthly idea how you want the output to look, so I'll let you grapple with that issue.
I just jumped in because this gave me a chance to make one of my favorite points: If you model the data correctly, the code will be vastly simpler.
Have fun with PHP, it is a blast to code.
|
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 04-07-2001 00:46
Thanks linear
Although I didn't really understand the code in your first post, ( like I said, I'm new to this stuff ) I did get the point. That my inexperience causes me to write code that's too long, and sloppy. Even though I'm just happy I can write code that works at all at this point. I went to work last night trying to clean it up, but didn't get very far. You have given me some new ammo to go back in with.
Also, I took no insult to your critique of my code. I just kinda felt like someone walked in the door... said "What the hell did you do it that way for!" and walked back out the door. I don't mind critique or criticism at all, as long as the correction follows. Now I'm off for a rewrite.
Thank you for your correction, and patient explanation.
|
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 04-07-2001 03:05
OK, here's what I ended up with after my lesson. It works great with one exception, if I leave $rows=3; the first time the page loads, I get four rows. Is there a way to reset the array from file() to start it's index at 1 instead of 0?
<?
$rows=3;
if (!$offset){$offset=0;}
$f = file("winnerstest.csv");
for ($i=$offset;$i<=($offset+$rows);$i++) {
$winners .= "<tr><td width=\"100%\" valign=\"top\" class=\"history\">";
$winners .= "$f[$i]";
$winners .= "</td></tr>";
}
$nextoffset=$offset+$rows;
$prevoffset=$offset-$rows;
$winners .= "<tr><td width=\"100%\" valign=\"top\" align=\"right\" class=\"next\">
<a href=\"winnerstest.php?rows=$rows&offset=$nextoffset\">next
|
linear
Paranoid (IV) Inmate
From: other places Insane since: Mar 2001
|
posted 04-07-2001 05:00
It should show 4 rows every time, right?
Try changing
for ($i=$offset;$i<=($offset+$rows);$i++)
to
for ($i=$offset;$i<($offset+$rows);$i++)
voila, 3 rows.
|
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 04-07-2001 19:44
Thanks, I thought of that, but didn't think it through. I thought if I did that, it would cut back to just showing 2 the rest of the time.
Thanks linear
|