Jump to content

foreach() inside of a foreach!


Fsoft

Recommended Posts

Hi,

 

I need a small help, actually stuck :(

 

actually the thing is that there is a variable called "$decoded"

 

and this variable have the data of a text file, each entry as a line of file. When I echo it. It's work perfectly :)

 

But Now I am doing a moderation task, in that I need to have each content of my variable $decoded inside of an existing foreach.

 

For eaxmple ;

 


<?php

$decoded = file_get_contents("database.txt");

//now the thing is i have a foreach...

foreach($remove_content as $remove)
{
foreach($decoded as $decoded entry)
{
echo $decoded_entry . $remove ;
}
}
?>

 

Thanks dear :) I need help> I want to use a foreach inside of an existing foreach to finish my project.

 

Thanks I am badly waiting for help :)

 

FAISAL!

Link to comment
Share on other sites

Thanks Dear, I get this problem in my php Engine, when I process code ;

 

Warning: Invalid argument supplied for foreach() in C:\server\htdocs\en\online_query.php on line 136

 

 

FAISAL!

Link to comment
Share on other sites

HMMM,

 

I think I will have to send you the Code,  so here is the code ;

 


<?php

	@include("contact/online_query.php");

//read system for Online Query system to be red,

$form = "<form method=\"post\" action=\"online_query.php\">
<p align=\"center\"><font color=\"red\" size=\"3\"><b>Password for Administration :</b> <input name=\"password\" type=\"password\" size=\"10\"></font></p>
<p align=\"center\"><input name=\"submit\" type=\"submit\" value=\"GO!\"></p>
</form>
";



if($_POST["password"] == my_password)
{
$_SESSION['access'] = "access";
}

if($_SESSION["access"] == access)
{

$db = file_get_contents("database.txt);

$db = explode("\n", $db);

$decoded = array();
foreach($db as $dbitem)
{
   $decoded[] = base64_decode(base64_decode($dbitem));
   
}

$decoded = join("\n ", $decoded);


//deleting code for Fsoft Software development personal website 

$db = "database.txt";

if($_GET["delete"])
{
$entry_remove = file("$db");
foreach($entry_remove as $remove)
{
	if(trim(str_replace("\r\n",'',$remove)) == $_GET["delete"])
		{
		$entry_remove = array_diff($entry_remove,array($remove));
		}
}
$entry_remove = implode("",$entry_remove);
$handle = fopen($db,'w');
fwrite($handle,$entry_remove);
fclose($handle);
}
$entry_remove = file("$db");


//these are my two foreach 
foreach($remove_content as $remove)
{
foreach($decoded as $decoded_entry)
{
echo $decoded_entry . "<a href='online_query.php?delete=" . $remove . "'>[Delete!]</a>";

}
}


}

else
{
echo $form;
}


?>

 

Thanks :)

Link to comment
Share on other sites

Dear,

 

Thanks for the answer. Actually this is a script where data is read, But the data is written in an other scrip :) I mean the script that is included at the top

 


<?php

include("contact/online_query.php");

?>

 

Though here is an example data / stuff of the file database.txt ;

 


UEdKeVBuQnZjM1JsWkNCaWVTQThZU0JvY21WbVBTSnRZV2xzZEc4Nk1TSStNVHd2WVQ0Z2IyNGdWR2gxTENBeE5TQk5ZWGtnTWpBd09DQXlNVG8xTWpvek1pQXJNREl3TUNCbWNtOXRJREV5Tnk0d0xqQXVNUTBLUEdKeVBnMEtNUTBLUEdKeVBnMEs=
UEdKeVBuQnZjM1JsWkNCaWVTQThZU0JvY21WbVBTSnRZV2xzZEc4Nk1pSStNand2WVQ0Z2IyNGdWR2gxTENBeE5TQk5ZWGtnTWpBd09DQXlNVG8xTWpvek9TQXJNREl3TUNCbWNtOXRJREV5Tnk0d0xqQXVNUTBLUEdKeVBnMEtNZzBLUEdKeVBnMEs=
UEdKeVBuQnZjM1JsWkNCaWVTQThZU0JvY21WbVBTSnRZV2xzZEc4Nk15SStNend2WVQ0Z2IyNGdWR2gxTENBeE5TQk5ZWGtnTWpBd09DQXlNVG8xTWpvME9DQXJNREl3TUNCbWNtOXRJREV5Tnk0d0xqQXVNUTBLUEdKeVBnMEtNdzBLUEdKeVBnMEs=

 

Ya, this is also decoded twice ;)

 

Hope it will give you an answer of what your using. Though It would be great to get your help As I am really stuck :)

 

At this moment, the problem is very small, every thing is done, deleting, reading, writing of system. The problem is in the deleting section. When I echo out the $decoded. It gives me a wrong value. For instance I have 3 lines in my database. According to my deleting section it would be each line with a text link called [Delete!] and when we click it that line is deleted.

 

BUT now it shows all the three lines and a [Delete!] line, then three lines and [Delete] link.

 

NOW if i suppose click on the delete that is in center. The line Number two will be delete from each line, AND again it will process two line and one [delete!] text..

 

HOPE it don't make you confuse. Though I have posted all the code, if you want code of writing script as well,. please do write. Thanks :)

 

FAISAL!

Link to comment
Share on other sites

ok this is untested

 

but may help

<?php

@include("contact/online_query.php");

//read system for Online Query system to be red,

$form = "<form method=\"post\" action=\"online_query.php\">
<p align=\"center\"><font color=\"red\" size=\"3\"><b>Password for Administration :</b> <input name=\"password\" type=\"password\" size=\"10\"></font></p>
<p align=\"center\"><input name=\"submit\" type=\"submit\" value=\"GO!\"></p>
</form>
";

if(isset($_POST["password"]) && $_POST["password"] == "my_password")
{
$_SESSION['access'] = "access";
}

if($_SESSION["access"] == "access")
{
$dbfile = "database.txt";

$db = file_get_contents($dbfile);
$db = explode("\n", $db);
$decodedArray = array();
foreach($db as $dbitem)
{
   $decodedArray[] = base64_decode(base64_decode($dbitem));
   
}

//these are my two foreach 
foreach($decodedArray as $K => $decoded_entry)
{
	echo $decoded_entry . "<a href='online_query.php?delete=" . $K . "'>[Delete!]</a>";
}

//deleting code for Fsoft Software development personal website 

if(!empty($_GET["delete"]))
{
	if(isset($decodedArray[$_GET["delete"]]))
	{
		unset($decodedArray[$_GET["delete"]]);
	}
	foreach($decodedArray as $k => $item)
	{
		$decodedArray[$k] = base64_encode(base64_encode($dbitem));
	}
	$decoded = join("\n", $decodedArray);
	file_put_contents($dbfile, $decoded);
}

}else{
echo $form;
}


?>

Link to comment
Share on other sites

I know this is a programming forum, but I'm going to step in and give you a little bit of English help, since I doubt it's your first language.

 

Thanks dear .. I need help

Thanks Dear

 

In this sense you are using Dear in place of someone's name.  For example, instead of Thanks Bob you are saying Thanks Dear.  Using Dear in this sense usually implies fondness and emotional attachment to the person you are speaking with.  So unless you are in love or extremely close to the person you are speaking with, using Dear in this sense is inappropriate.

 

Dear,

Using Dear as an introduction is usually reserved for formal letters and e-mails.  In this sense, you usually address the person before the comma, as in Dear Bob,.  There is no implication of fondness or emotional attachment when addressing someone in this fashion. In other words, I would address both my boss and my wife as Dear Name, and while I love my wife I don't love my boss.

 

Long story short, don't use Dear when speaking unless talking to your wife, girlfriend, husband, etc.  Don't use Dear in writing unless addressing a person directly and you need to be formal, i.e. when applying for a job.

 

As far as programming help, it looks like MadTechie has you covered so I don't want to step on his shoes.

 

:D

Link to comment
Share on other sites

HEHEHEH

 

Thanks a lot. For the help :)

 

Actually i was very Crazy in my work / because it's stuck from last 2 weeks :(

 

Well, I am sorry if I wrote some thing wrong :) Though thanks for the lesson. Gave me some new Info as well ;)

 

Thanks and I am still waiting for help in My php project ;)

 

Thanks :)

 

FAISAL!

Link to comment
Share on other sites

Ok, Finaly as I see no one was answering, I made it really really easy  ;D

 

What I do is that Now I collect my encoded code, decode it using the script, and again re write it to a new temp_data.txt<it's not database>.

 

So here at one side I get the normal (READABLE) contents in temp_data.txt and on other hand I have database.txt <encoded Code>.

 

Now What I simply want is that When I use foreach. It give me the each each line of both files each and after according to this code ;

 


<?php

$f1 = file_get_contents("online_querydb.txt"); $f2 = file_get_contents("contact/temp_data.txt");

$enty_remove = $f2 . "<b><a href='online_query.php?delete=" . $f1 . "'>[Delete!]</a></b>";

foreach($entry_remove as $entry)

{
	echo $entry;

}

?>

 

Now, I think it can't be more simple as this ;) SO now please help me :)

 

As the code says exactly why I want to use foreach, What do to, to grab the lines for DB and the temp file and manage them In a correct order using some HTML(USED IN $remove_remove variable)..

 

Thank a lot. Badly waiting for answer :)

 

FAISAL!

Link to comment
Share on other sites

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.