Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/169245-invalid-argument-supplied-for-foreach/
Share on other sites

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>

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%%%%

)

 

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.