riceje7 Posted August 7, 2009 Share Posted August 7, 2009 this is my first post so any help would be greatly appreciated. i am attempting to build a forum with a flat file database and have run into a snag that i can't figure out. what i am doing is saving a template script as a .php file and when users click on a hyperlink it will access that php file and display the forum posts. however when ever i click on the link it gives me this warning :Warning: Invalid argument supplied for foreach() in /Users/josephrice/Sites/top_secret/forums/knekjn/knekjn.php on line 10. i can't for the life of me figure out what is going wrong. here is the code: <?php $filename = "forumdata.txt"; $fp = fopen($filename, 'r'); $file = fread($fp, filesize($filename)); fclose($fp); $contents = explode('&&', $file); foreach($contents as $tmp) { foreach($tmp as $tmp2) { $contents2 = explode('%%', $tmp); foreach($contents2 as $tmp3) { echo $tmp3; } } } ?> if you need any more corresponding code just let me know. thanks Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 foreach is expecting an array. The variables $contents, $tmp or $contents2 dont appear to be an array. Can you post how your flat file database is formatted/constructed. Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893071 Share on other sites More sharing options...
watsmyname Posted August 7, 2009 Share Posted August 7, 2009 try to see if $contents holds an array first <?php echo "<pre>"; print_r($contents); echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893075 Share on other sites More sharing options...
riceje7 Posted August 7, 2009 Author Share Posted August 7, 2009 try to see if $contents holds an array first <?php echo "<pre>"; print_r($contents); echo "</pre>"; ?> i tried that. it is showing it has data. here is the construction code : <?php $username = $_POST['username']; $password = $_POST['password']; $threadname = $_POST['threadname']; $newthread = $_POST['newthread']; $textfield = $_POST['textfield']; $date = date('H:i d M, Y'); $forumname = $_POST['forumname']; $reply = $_POST['reply']; //echo $date; //echo $username."<br />"; //echo $password."<br />"; //echo $threadname."<br />"; //echo $textfield."<br />"; if($newthread == true) { $newthread = str_replace(' ', '_', $newthread); if(!file_exists("forums/".$newthread)) { mkdir("forums/$newthread"); //echo $threadname; $filename = "forums/forumlist.txt"; $fp = fopen($filename, 'a'); fwrite($fp, "&&".$newthread); fclose($fp); $newthreadtemplate = "newthreadtemplate.php"; $newfile = "forums/".$newthread."/".$newthread.".php"; $fp = fopen($newthreadtemplate, 'r'); $fp2 = fopen($newfile, 'a'); $file = fread($fp, filesize($newthreadtemplate)); fwrite($fp2, $file); fclose($fp); fclose($fp2); //echo "hello"; $forumfile = "forums/".$newthread."/forumdata.txt"; //echo $forumfile; $newthreaddata = "&&".$date."%%".$username."%%".$textfield; //echo $newthreaddata; $fp = fopen($forumfile, 'a'); fwrite($fp, $newthreaddata); fclose($fp); } else { echo "Thread already exists."; } } $filename = "forums/forumlist.txt"; $fp = fopen($filename, 'r'); $file = fread($fp, filesize($filename)); fclose($fp); $forumlist = explode('&&', $file); foreach($forumlist as $tmp) { if($tmp == true) { $name = str_replace('_', ' ', $tmp); echo "<a href=forums/$tmp/$tmp.php>$name</a>"; echo "<br />"; } } ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Forum</title> </head> <body> <br/> <br/> <br/> <br/> <br/> <br/> <form action="newthreadsubmit.php" method="post" enctype="multipart/form-data" > Thread Name<input type="text" id="newthread" name="newthread" /><br /> Username<input type="text" id="username" name="username" value= <?php echo $username; ?>><br /> Password<input type="password" id="password" name="password" /><br /> <TEXTAREA NAME="textfield" id="textfield" ROWS="8" COLS="50"></TEXTAREA><br /> <input type="submit" value="Submit New Thread" > <input type="reset" value="Reset"> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893080 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 try to see if $contents holds an array first <?php echo "<pre>"; print_r($contents); echo "</pre>"; ?> i tried that. it is showing it has data. here is the construction code What is the output? Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893087 Share on other sites More sharing options...
riceje7 Posted August 7, 2009 Author Share Posted August 7, 2009 try to see if $contents holds an array first <?php echo "<pre>"; print_r($contents); echo "</pre>"; ?> i tried that. it is showing it has data. here is the construction code What is the output? Array ( [0] => [1] => 12:55 07 Aug, 2009%%%% ) Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893091 Share on other sites More sharing options...
watsmyname Posted August 7, 2009 Share Posted August 7, 2009 your first code and the late codes are bit different...in first code you have foreach loop inside foreach loop in second you have "If" condition. Doesn't your second code work as well?? And you have no first element, i.e. 0 index in the output, that is causing error. Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893093 Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 where do you create the content array. I can't seem to find it in your code Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893096 Share on other sites More sharing options...
watsmyname Posted August 7, 2009 Share Posted August 7, 2009 where do you create the content array. I can't seem to find it in your code see his first post Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893102 Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 oh... I'm an idiot. well, assuming that your text file is valid I can't really see a problem. although it seems that, from your output, your first key has a null value (and the key itself is a null value) this probably has something to do with the error as wat has said Quote Link to comment https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/#findComment-893108 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.