Updating and paging through arrays with a web form

My Hosting provider doesn’t provide a database so I am using a text file to store a blog. I am now attempting to write an editing tool for the blog. I open the text file and build an array and present the data in the form. I have navigation buttons that will index up and down to change the content in the form. I can’t seem to get the global variables to update and refresh the form fields after indexing the array. I will attach a text file example and the PHP below. Any help will be appreciated…
[data.php]
Jimmy 05/19/14 (19:49) When you’re through turn off the lights please.
David 05/19/14 (20:49) I can’t follow what I can’t see.
Blogger Time message
[php][PHP]
$fn = “data.php”;
$fer = fopen($fn, “r”) or die(“Unable to open file!”);
$lines = (fgetcsv($fer,0,"\r")); //creates an array for each blog
fclose($fer);
$count = count($lines);
$data = array();
$ctrow = 3;
$i = 0;
foreach($lines as $value) {
$detail[$i] = explode("\t", $value);
$i++;
}
[/PHP]

Edit the Blog

<input type=“text” name= “name” maxlength=25" value ="<?php echo $detail[$ctrow][0]; ?>">

<textarea cols=45" rows=10" name=thoughts" maxlength=500"><?php echo $detail[$ctrow][2]; ?>


[PHP] switch ($_POST["submit"]) { case 'Next': print "You pressed Button Next "; $ctrow = 1+ $_POST['record']; /* at this point the web form should update to the next record in the detail array echo $ctrow /* will add logic here to do what needs to be done after I figure out why this won't update after the click break; case 'Previous': print "You pressed Button Previous"; break; case 'Delete': print "You pressed Button Delete"; break; case 'Save': print "You pressed Button Save"; break; }[/PHP]

Are you paying for hosting? There are many free hosts who will provide a database. To be honest, I’ve never heard of a host not providing a database.

On to your question, I will reply in a little while, I’m on a new machine and don’t have XAMPP installed yet.

Well, first, in your code sample, you have it dummied out using a comment. Which is an HTML comment, not a PHP one.

You can NOT use /* as a comment in PHP, it should be // !

Also, you do not end the line “echo $ctrow” with a " ; " as it should have.

So, all of that code inside the switch command will never get executed.

Now, onto text files… Processing text files is a huge overhead for a server. That is why it is not used in this day and age. Also, text files can be hacked with ease so they are not secure.

I agree with Scottlpool as all hosting sites that I am aware of include a database system. Even the free hosting sites, do too. If you MUST use text files, XML seems to work well and PHP has a nice library for XML usage. Also, you can use arrays and store them. That makes it much easier as you just write a text file with the contents of your array. When you read it back, the array is all set up and you do not need to parse thru the lines of info. So, lots of possiblities to solve this one.

What is the name of your hosting company? Just curious on why they do not include databases…

You can NOT use /* as a comment in PHP, it should be // !

PHP.NET

PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments.

[PHP]

<?php echo 'This is a test'; // This is a one-line c++ style comment /* This is a multi line comment yet another line of comment */ echo 'This is yet another test'; echo 'One Final Test'; # This is a one-line shell-style comment ?>

[/PHP]

All valid comments: http://www.php.net/manual/en/language.basic-syntax.comments.php

Maybe you have misread the part that says don’t use /* in nested comments as don’t use them?

Scott, I know what I’m doing! Do you?

A comment “BLOCK” is a multi-line comment.

So, if you review his code, he OPENS the comment block and places one comment line there.
Then, he has more code. (Good and bad code, by the way…)
Then, he NEVER ends the comment block.

It was obvious that he was inserting a single comment NOT a comment block or multi-line comment !
Therefore, it should have been // not /* …

Perhaps I am missing something else?

Commenting out multiple lines of code while debugging, there is nothing wrong with that if you are not using something more advanced such as Zend. I did miss him not closing the comment though, easy mistake just like your

 miss. No need to get touchy, a quick comment out is perfectly fine to hide something from a user and to make your code more readable by other programmers.

Well, I’m not getting touchy. But, there is no ending for the block. No / anywhere in the code he showed.
AND, he put /
on to consecutive lines. Therefore the sane and simple assumption is that he was using
the /* as a // which does not work in the code he showed…

Ok you 2, back to your corners.

What Ernie was trying to say was that how the guy was trying to comment the line wasn’t ended, making anything after the /* a comment. He either needed to add a */ at the end, or change it to //. So while it is valid, its not correct for what he was trying to do, causing other issues.

Typically, what I when I’m messing with arrays is create a function that’ll display a print_r on the array, instead of having to type the entire block over and over. You know what the array should look like.

Exactly, Richei… Thought I was clear on that, but, guess I wasn’t…

Either way, Odivad never responded, so maybe he was not clear on it, too…

Just here to help everyone…

Sponsor our Newsletter | Privacy Policy | Terms of Service