Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Code Help - This Time I've got something, but it needs fixing
Old 10-18-2009, 09:25 PM Code Help - This Time I've got something, but it needs fixing
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 823
Name: Scott
Location: Ontario
Trades: 0
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
__________________
Check out my
Please login or register to view this content. Registration is FREE
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-19-2009, 08:09 AM Re: Code Help - This Time I've got something, but it needs fixing
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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($handlefilesize($filename));
fclose($handle);

$entries explode('|'$content);

$myEntry null;
foreach (
$entries as $entry) {
   
$entry trim($entry); // remove possible spaces in beginning and end
   
if (substr($entry0strpos($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
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-19-2009, 08:12 AM Re: Code Help - This Time I've got something, but it needs fixing
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Oh, and one more thing. There is a function built in to php for replacing line brakes with <br />, nl2br().
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-19-2009, 05:31 PM Re: Code Help - This Time I've got something, but it needs fixing
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 823
Name: Scott
Location: Ontario
Trades: 0
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($handlefilesize($filename));
fclose($handle);

$entries explode('|'$content);

$myEntry null;
foreach (
$entries as $entry) {
   
$entry trim($entry); // remove possible spaces in beginning and end
   
if (substr($entry0strpos($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...
__________________
Check out my
Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 10-19-2009 at 05:36 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-19-2009, 05:49 PM Re: Code Help - This Time I've got something, but it needs fixing
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Sorry, seems I missed a 's' in $contents, 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' 
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366

Last edited by lizciz; 10-19-2009 at 05:51 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-19-2009, 05:55 PM Re: Code Help - This Time I've got something, but it needs fixing
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 823
Name: Scott
Location: Ontario
Trades: 0
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'...
__________________
Check out my
Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 10-19-2009 at 05:58 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-19-2009, 08:56 PM Re: Code Help - This Time I've got something, but it needs fixing
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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($handlefilesize($filename));
fclose($handle); 
can be replaced with this

PHP Code:
$contents file_get_contents("ffdatabase.txt"); 
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Code Help - This Time I've got something, but it needs fixing
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.39409 seconds with 12 queries