Code Help - This Time I've got something, but it needs fixing
10-18-2009, 09:25 PM
|
Code Help - This Time I've got something, but it needs fixing
|
Posts: 191
Name: Physicsguy
Location: On Earth
|
Hello all,
I've made a little code to pull the line that starts with $reviewID all the way to the delimiter using stream_get_line. Here's the code:
PHP Code:
$reviewID = $_GET["reviewID"]; //Set the variable 'reviewID' as from the URL.
$transback = array( "\n" => "<br />", //IMPORTANT, DO NOT CHANGE! ); //Simply changes all line breaks in the text file to <br/>s
echo "<br /><br />"; //Nice spacing ////////////////////////// $database = fopen("ffdatabase.txt", "r"); //Opens the database into a variable called '$database'. $start = strstr($database, $reviewID);; //Find the reviewID in $database $end = strstr($database, "|");; //For later use, this makes the | the delimiter for $database //////////////////////////
$MaxLenTotal = $MaxLenTitle + $MaxLenAuthor + $maxLenReview + $maxLenReason + 177; //Makes the pull length, not really important.
/////////////////THE PROBLEM!///////////////////// //$line1 = stream_get_line( $database, $MaxLenTotal [$end] ); //sets $line1 as: get $database, pull $MaxLenTotal characters from it, and end at $end. $line1 = stream_get_line($database, $MaxLenTotal, "|"); //sets $line1 as: get $database, pull $MaxLenTotal characters from it, and end at |.
$line2 = strtr("$line1", $transback); //Uses the $transback function above
echo "$line2"; //Echoes the finished product to the page
As you can see, I have a funciton where it takes the value in the URL, and looks for it in the database. Then it just formats it ($transback) and echoes to the page.
The problem is; I can't get it to look for the value in the URL.
If I comment the first one (so the second one works), I get just the first section up to the delimiter. If I comment the second one (so the first one works), it shows the entire database.
Can somebody please just fix the code above (not write a whole new one) and make it so itt works the way I want above?
Thanks very much!
-PG
|
|
|
|
10-19-2009, 08:09 AM
|
Re: Code Help - This Time I've got something, but it needs fixing
|
Posts: 330
Name: Mattias Nordahl
Location: Sweden
|
Are you not getting any errors or warnings? When you open a file with fopen() you get kind of a "link" (called resource at php.net) to the file, and you can't use strstr() on it. strstr() expects a string. You need to first read the content into one big string, then you could split the content at | into an array. Then go thru the array to find the one that starts with $reviewID.
PHP Code:
$filename = "ffdatabase.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);
$entries = explode('|', $content);
$myEntry = null; foreach ($entries as $entry) { $entry = trim($entry); // remove possible spaces in beginning and end if (substr($entry, 0, strpos($content, ' ')) == $reviewID) { $myEntry = $entry; break; } }
if ($myEntry == null) { // Couldn't find the entry } else { // Do something with $myEntry }
My example is based on that your file have this structure.
1 This is the first entry | 2 This is the seccond entry | 3 This is the third entry
or
1 This is the first entry |
2 This is the seccond entry |
3 This is the third entry
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
|
|
|
|
10-19-2009, 08:12 AM
|
Re: Code Help - This Time I've got something, but it needs fixing
|
Posts: 330
Name: Mattias Nordahl
Location: Sweden
|
Oh, and one more thing. There is a function built in to php for replacing line brakes with <br />, nl2br().
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
|
|
|
|
10-19-2009, 05:31 PM
|
Re: Code Help - This Time I've got something, but it needs fixing
|
Posts: 191
Name: Physicsguy
Location: On Earth
|
Yes, I know. I would prefer not to use nl2br because with what I'm using, you can add more translations.
And thanks for the code! My database is structured like this:
Mm62r - CONTENT
CONTENT
CONTENT
CONTENT
CONTENT CONTENT
CONTENT |
874yh - CONTENT...
..CONTENT |
But I can tweak it to work for me, thanks!
EDIT
Uhh, your code doesn't seem to be working... I have it to echo $myEntry if found, and everything is right, anyway here's my code:
PHP Code:
$reviewID = $_GET["reviewID"]; //Set the variable 'reviewID' as from the URL.
$transback = array( "\n" => "<br />", //IMPORTANT, DO NOT CHANGE! ); //Simply changes all line breaks in the text file to <br/>s
echo "<br /><br />"; $filename = "ffdatabase.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);
$entries = explode('|', $content);
$myEntry = null; foreach ($entries as $entry) { $entry = trim($entry); // remove possible spaces in beginning and end if (substr($entry, 0, strpos($content, ' ')) == $reviewID) { $myEntry = $entry; break; } }
if ($myEntry == null) { echo "Couldn't find your entry"; } else { echo "$myEntry"; }
I don't see anything wrong with that...
Last edited by Physicsguy; 10-19-2009 at 05:36 PM..
|
|
|
|
10-19-2009, 05:49 PM
|
Re: Code Help - This Time I've got something, but it needs fixing
|
Posts: 330
Name: Mattias Nordahl
Location: Sweden
|
Sorry, seems I missed a 's' in $content s, at this line
PHP Code:
$entries = explode('|', $contents);
Also, if you want to replace line brakes your way, you should at least do it properly :P
The strstr() function is used to find a string inside another string, it won't replace anything.
PHP Code:
$find = array('find1', 'find2', 'find3'); $replace = array('replace1', 'replace2', 'replace3');
$myEntry = str_replace($find, $replace, $myEntry);
Also, perhaps you know this already but still, for printing out variables there is no need to encase them in quotes.
PHP Code:
echo $myEntry; //will work just as fine as
echo "$myEntry";
// and is probably by most poeple concidered "better". In performance I // bet there is no difference, but it seems strange to make string out of // everything before printing them out. Plus, in this case $myEntry is // already a string.
// However, single quotes won't work the same way.
$var = "Hello World!";
echo $var; // will print 'Hello World!' echo "$var"; // will print 'Hello World!' echo '$var'; // will print '$var'
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
Last edited by lizciz; 10-19-2009 at 05:51 PM..
|
|
|
|
10-19-2009, 05:55 PM
|
Re: Code Help - This Time I've got something, but it needs fixing
|
Posts: 191
Name: Physicsguy
Location: On Earth
|
Thanks for that! It works now, but I want it so when the URL is something like userreview.php?reviewID=MmPf3, look for the line that starts with MmPf3 and stream_get_line up to the delimiter. I've gotten some progress, but it doesn't work quite yet, so I won't post it.
Thanks for your help so far though, and fantastic replies!
Ok I'll post it anyway.
PHP Code:
$reviewID = $_GET["reviewID"]; //Set the variable 'reviewID' as from the URL.
$transback = array( "\n" => "<br />", //IMPORTANT, DO NOT CHANGE! ); //Simply changes all line breaks in the text file to <br/>s
$database = fopen("ffdatabase.txt", "r"); $database2 = file_get_contents('ffdatabase.txt'); $datatrans = strtr("$database", $transback); // //echo $datatrans;
$datasplit = strstr($datatrans, $reviewID); $datadelim = stream_get_line($database, $MaxLenTotal, "|"); echo "ReviewID $datadelim";
For some reason, that won't work... It just displays the first 'review'...
Last edited by Physicsguy; 10-19-2009 at 05:58 PM..
|
|
|
|
10-19-2009, 08:56 PM
|
Re: Code Help - This Time I've got something, but it needs fixing
|
Posts: 330
Name: Mattias Nordahl
Location: Sweden
|
I don't know what you're trying to do in that piece of code, that's just messy :O
The code I posted, together with the fixes, should work fine. And now you're opening the file, then use file_get_contents(), and still try to replace stuff with strstr()... And I don't see why you want to use stream_get_line() either.
Try to make a new page and copy/pase all my code, with the fixes, and see if it works. Then work your way from there. You can use file_get_contents() if you wish. Basically this code
PHP Code:
$filename = "ffdatabase.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);
can be replaced with this
PHP Code:
$contents = file_get_contents("ffdatabase.txt");
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
|
|
|
|
|
« Reply to Code Help - This Time I've got something, but it needs fixing
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|