Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Imploding an empty array

NeakNeak Posts: 87Member
Hello again. -_-
I'm using a crazy text file based system for the news on my site. It works like this...

I have a file, called news.foo, which looks something like this:
[code]
Title
Author
Date
Content

Title2
Author2
Date2
Content2

etc...
[/code]
Notice that each news article is separated by two new lines(

) and each part of those is separated by one(
).

I explode this file by

to separate the parts, so now I have:[code]$exploded[0] == "Title
Author
Date
Content";
$exploded[1] == "Title2
Author2
Date2
Content2";
[/code]
Then, to separate them even more, I explode each element of $exploded by
... So I have:
[code]
$exploded[0][0] == "Title"
$exploded[0][1] == "Author"
$exploded[0][2] == "Date"
$exploded[0][3] == "Content"
$exploded[1][0] == "Title2"
$exploded[1][1] == "Author2"
$exploded[1][2] == "Date2"
$exploded[1][3] == "Content2"
[/code]

I've got the code for displaying these in a formatted page working. I've got the code for editing these working also. But the problem is in deleting them.

To delete or modify entries, I change part of the array, then implode it and write the imploded string. With modifying it, I have no trouble. But, I'm having trouble in deleting entries, because sometimes it ends up trying to implode an empty array. Here's my code for getting around that. I don't see the problem. -_-
[code]
if ($_GET['q'] == "delete")
{
unset($exploded[$_GET['c']]);
//---------------------------------------------------------------------------------------------
// implode the parts, then implode them all together
foreach ($exploded as $index => $line) // Go through each part of $exploded
{
$implodedFirst[$index] = implode("
", $line); // Implodes each part
// by new line.
}
if(!is_null($implodedFirst[0])) // Make sure there is at least one element
{
$imploded = implode("

", $implodedFirst); // Implode by 2x new line...
}
else // If there are no elements
{
$imploded = "";
}
fputs($Fopened, $imploded);
fclose($Fopened);
//---------------------------------------------------------------------------------------------
$middleHTML = 'The entry was deleted.';
}
[/code]

Comments

Sign In or Register to comment.