Fsoft Posted May 15, 2008 Share Posted May 15, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/ Share on other sites More sharing options...
DarkWater Posted May 15, 2008 Share Posted May 15, 2008 foreach($remove_content as $remove) { foreach($decoded as $dec) { echo $dec . " - " . $remove ; } } I added the hyphen in there for when you echo it so it looks nice. Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542293 Share on other sites More sharing options...
Fsoft Posted May 15, 2008 Author Share Posted May 15, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542313 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 foreach($decoded as $dec) won't work it needs to be an array not a string i don't understand the problem here, I need to have each content of my variable $decoded inside of an existing foreach can you explain that a little clear please Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542318 Share on other sites More sharing options...
Fsoft Posted May 15, 2008 Author Share Posted May 15, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542324 Share on other sites More sharing options...
KenDRhyD Posted May 15, 2008 Share Posted May 15, 2008 You have mismatched quotes in your code example! Also, a foreach must use different variable names on the left and right side of the "as". Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542338 Share on other sites More sharing options...
Fsoft Posted May 15, 2008 Author Share Posted May 15, 2008 Thanks Dear, Your a nice guy, but the problem is that ( It's not working Help Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542347 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 do you have some sample data of the database.txt file, as i am not sure what i am working with here.. your using double base64_decode but no encodeing with would mean it would only allow you to exit it once! Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542369 Share on other sites More sharing options...
Fsoft Posted May 16, 2008 Author Share Posted May 16, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542920 Share on other sites More sharing options...
MadTechie Posted May 16, 2008 Share Posted May 16, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-542991 Share on other sites More sharing options...
roopurt18 Posted May 16, 2008 Share Posted May 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-543004 Share on other sites More sharing options...
Fsoft Posted May 16, 2008 Author Share Posted May 16, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-543089 Share on other sites More sharing options...
Fsoft Posted May 16, 2008 Author Share Posted May 16, 2008 Ok, Finaly as I see no one was answering, I made it really really easy 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! Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-543217 Share on other sites More sharing options...
DarkWater Posted May 16, 2008 Share Posted May 16, 2008 Are they going to have the same number of lines in each file? Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-543222 Share on other sites More sharing options...
Fsoft Posted May 17, 2008 Author Share Posted May 17, 2008 Yes, Yes, Yes! Exactly Quote Link to comment https://forums.phpfreaks.com/topic/105815-foreach-inside-of-a-foreach/#findComment-543241 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.