simcoweb Posted September 26, 2008 Share Posted September 26, 2008 I've been so busy designing that i've neglected my PHP skills (if I ever had any ) I'm hacking around in a random quote display script I downloaded whereas i'm trying to make it work for testimonials. It uses a flat txt file for storing the records. So, i'm trying to write a simple function to display all the testimonials in a formatted table setting. Here's my function: <?php function showAll() { $filename = "quotes/testimonials.txt"; $fh = fopen($filename, 'r') or die("Can't open file"); $quotes = fread($fh, filesize($filename)); echo "<table width='90%' align='center' border='0' cellpadding='1' cellspacing='2'>"; foreach($quotes as $value) { echo "<tr><td>$value</td></tr>"; } echo "</table>"; fclose($fh); } ?> I get this error message when trying to execute but the argument seems to make sense but is being reported as invalid: Warning: Invalid argument supplied for foreach() in /home2/xxxxxx/public_html/showall.php on line 13 I'm sure it's simply my rusty code. I need to write some PHP scripts to keep in practice! Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/125903-a-bit-rusty-with-my-php-need-help-with-fopen/ Share on other sites More sharing options...
ranjuvs Posted September 26, 2008 Share Posted September 26, 2008 foreach works with arrays. in this case fread returns a string or FALSE Quote Link to comment https://forums.phpfreaks.com/topic/125903-a-bit-rusty-with-my-php-need-help-with-fopen/#findComment-651062 Share on other sites More sharing options...
fanfavorite Posted September 26, 2008 Share Posted September 26, 2008 What are you trying to do? I don't believe fread creates an array, which is why your foreach doesn't work. If you want to get each line, try: $quotes = file($filename); Quote Link to comment https://forums.phpfreaks.com/topic/125903-a-bit-rusty-with-my-php-need-help-with-fopen/#findComment-651065 Share on other sites More sharing options...
simcoweb Posted September 26, 2008 Author Share Posted September 26, 2008 Basically i'm trying to open the file, take each line and display it in its own table cell. I'm more used to working with MySQL databases and queries. Either I convert this script to MySQL or figure how to extract each line, separate them and display in a cell. I'll try your suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/125903-a-bit-rusty-with-my-php-need-help-with-fopen/#findComment-651076 Share on other sites More sharing options...
simcoweb Posted September 26, 2008 Author Share Posted September 26, 2008 Ok, changing the line to file($filename); did the trick as it now reads all the data. Now, another question on this. The info is in a pipe delimited format: some data here | their name | a link here Is there a way to separate these and not display the pipe? Right now it's just treating it all like txt, obviously, so it displays everything. Quote Link to comment https://forums.phpfreaks.com/topic/125903-a-bit-rusty-with-my-php-need-help-with-fopen/#findComment-651077 Share on other sites More sharing options...
vikramjeet.singla Posted September 26, 2008 Share Posted September 26, 2008 just explode the value using pipeline as delimiter explode("|", $value); Quote Link to comment https://forums.phpfreaks.com/topic/125903-a-bit-rusty-with-my-php-need-help-with-fopen/#findComment-651080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.