Closed Thread Icon

Topic awaiting preservation: ok now i need help with a simple blog script... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12313" title="Pages that link to Topic awaiting preservation: ok now i need help with a simple blog script... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: ok now i need help with a simple blog script... <span class="small">(Page 1 of 1)</span>\

 
LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 01:33

for my site that i'm building i just want a simple blog where i can put my information in a form and that form can be written to a text file, then that text file will be imported and displayed in an swf. but so far i'm just working on the form to write to the text file part.

here's my php script...

<?php

$theupdatefile = 'updates.txt';

$open = fopen('updates.txt', 'r+');

if(!open)
{
die('The \'$theupdatefile\' file was not able to load');
}

$user = ('<a href=\"mailto:$email\" target=\"_blank\">$handle</a>');

$writedata = fwrite($open, '<br />$blog<br />$user<br />');

fclose($open);

?>

and here's my html for the form...

<html>
<head>
<title>word iono</title>
</head>
<body>
<table cellpadding="2" cellspacing="0" border="0" width="200" height="150">
<tr>
<td align="center"><form method="post" action="updates.php"></td>
<td><input type="text" name="handle" value="handle" /></td>
</tr>
<br />
<tr>
<td align="center"></td>
<td><input type="text" name="email" value="e-mail" /></td>
</tr>
<br />
<tr>
<td align="center"></td>
<td valign="top"><textarea name="blog" rows="8" wrap="wrap"></textarea></td>
</tr>
<br />
<tr>
<td align="center"><input type="submit" value="blog" /></td>
</form>
</tr>
</table>
</body>
</html>

yeh the html is all weird, but i was half asleep when i wrote it so i dont care

now here's the problem...when i download the updates.txt file to see if it wrote what i put in the form all i get is this...

<br />$blog<br />$user<br />


help please?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-16-2002 01:50

It's the single quotes.

You need double quotes to expand variable strings here's the relevant page in the manual

As a rule I usually do something like this with variables
$name = "Mike";
echo "Hi my name is ".$name." What's your name";

just for sanity.



.:[ Never resist a perfect moment ]:.

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 02:12

ha ha thanks...i feel so dumb

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 02:23

greaaaaaaaaaaat...now i get a parse error on line 14 which would be...

$writedata = fwrite($open,"<br />$blog<br />$user<br />");

WHAT could possibly be wrong with that? so confusedddddd

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-16-2002 03:20

If you are going to put strings in double quotes you need space around them (rule of thumb not truely necessary all the time)

Instead of $blog for the variable it's looking for "$blog<br " a space after blog will fix it or the format I put earlier





.:[ Never resist a perfect moment ]:.

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 03:52

ok...well here's my 'edited' version of the script now...

<?php

$theupdatefile = "updates.txt";

$open = fopen("updates.txt", "r+");

if(!open)
{
die("The \"$theupdatefile\" file was not able to load");
}

$user = "<a href=\"mailto:$email\" target=\"_blank\">$handle</a>";

$writedata = fwrite($open,"<br /> $blog <br /> $user <br />");

fclose($open);

?>

it writes the hyperlink to the text file but not my message that i type in the blog field of the form.

still confused

but thanks so far bitdamaged you've been a BIG help



[This message has been edited by LiKwiD (edited 07-16-2002).]

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 07-16-2002 04:43

You best be it to do some simple error checking.

Try placing echo($blog); right before the line where blog is to be printed.

If that doesn't work then try some error checking (which you should probabally have anyways), such as,

if(!$blog) $blog = "Someone Goofed";

or better yet

if(!$blog) header("Location: [some error page]");

You should also check the name and email. Because if you have malformed data or missing data you will run into trouble.

When I start work on my code I make sure to put some data verification in there. Something along the lines of:

/** CHECK FOR [VARIABLE NAME] **/
echo("<p>[varibale name] at line x : " . $variableName . "</p>/n");

If it works you can comment out the check, else you will need to fool around for a bit to figure out what the error is.

The checks will make you page look like crap, however it will really help you out in the long run.

Lord_Fukutoku
Paranoid (IV) Inmate

From: West Texas
Insane since: Jul 2002

posted posted 07-16-2002 05:38

I'm definately gonna back WM on this. Whenever I start a new program or something I always try and have all sorts of data checks throughout... Makes it so much easier to find the problems when they arise, and these checks are usually real easy to edit out in comment blocks (for future troubleshooting, though you don't have to leave all in, just a few well placed ones work). One reason I like java is the catch{} block, and/or user defined exception handling. But since I am just starting out with php, I'm not for sure if these are available for what you're doing...

Just my 2cents...


________________________________________________________________
-- Jack of all trades, master of that which has my attention at
the moment.

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 05:44

the only person that is going to be using it is me. so i'll know how to use it and stuff...not like i'm going to be letting random people use my form to blog on my site.

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 06:08

well it's confirmed, you're a genius warmage. thank you sooooooooooooooooooo much. and thank you bitdamaged.

both of you were a BIG help...here's my new working php script. wahoooooooo

<?php

$theupdatefile = "updates.txt";

$open = fopen("updates.txt", "r+");

if(!open)
{
die("The \"$theupdatefile\" file was not able to load");
}

$user = "<a href=\"mailto:$email\" target=\"_blank\">$handle</a>";

echo($blog);

$writedata = fwrite($open,"<br /> $blog <br /> $user <br />");

fclose($open);

?>

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-16-2002 06:13

hmmmmmmmmmmm....now what if i wanted to set a maximum file size/maximum amount of charectors for the text file? so it wont be to big and wont take forever to load in the browser?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-16-2002 07:42

You have a couple of choices here

The first is that the fwrite function actually take a third argument which is the number of bytes to write to a file (with 1 char equalling a byte usually). So you could write

fwrite($open, '<br />$blog<br />$user<br />', 300);

Where 300 would limit you to about 300 characters.
but that's not the way I'd do it

I'd count the length of the string using strlen Then you have a choice to either hack the string using substr or return an error.

Here's what I'd do. With PHP a lot of times I make my form and my script the same page using the action attribute to tell me what to do. So I'd do something like this.

code:
<?php
// If the form has been submitted we'll do this:
if ($action == "submit") {

// First let's validate our data
$length = strlen($blog);

// Here 200 is the length we are using you will want to change $maxlen to what ever you want
$maxlen = 200;
if ($length > $maxlen) $error = "Your Blog entry is too long";

// if it's not too long then let's try writing the textfile
if (!$error) {
$theupdatefile = 'updates.txt';
$open = fopen('updates.txt', 'r+');

// If it's open then keep going
if(!$open) {
$error = 'The \'$theupdatefile\' file was not able to load';
} else {
$user = ('<a href=\"mailto:$email\" target=\"_blank\">$handle</a>');
$writedata = fwrite($open, '<br />$blog<br />$user<br />');
fclose($open);

// Let's make sure everything wrote okay
if (!$writedata) $error = "Could not write to file";
}
}
// Now if there is no error we'll write a all okay
if (!$error) {
echo "Your form was submitted!";
} else {
// Here's a trick we want to print the form but we have it in
// The php delimiters so we're going to drop out of php mode so
// we don't have to write "echo "<html> ... blah blah blah"
// and escape out all the quotes
?>
<html>
<head>
<title>word iono</title>
</head>
<body>
<table cellpadding="2" cellspacing="0" border="0" width="200" height="150">
<tr>
<td align="center">
<?
// Drop back into PHP to write out any errors
echo $errors;
?>
<!--
"<?= " is the same as "<? echo" so we are going to make this
submit to itself
-->
<form method="post" action="<?= $PHP_SELF"></td>
<td><input type="text" name="handle" value="handle" /></td>
</tr>
<br />
<tr>
<td align="center"></td>
<td><input type="text" name="email" value="e-mail" /></td>
</tr>
<br />
<tr>
<td align="center"></td>
<td valign="top"><textarea name="blog" rows="8" wrap="wrap"></textarea></td>
</tr>
<br />
<tr>
<td align="center"><input type="submit" value="blog" /></td>
</form>
</tr>
</table>
</body>
</html>
<?
// Drop back into PHP mode to close the if/else statement.

}
?>






.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 07-16-2002).]

gibbon
Obsessive-Compulsive (I) Inmate

From: Oakland, CA USA
Insane since: Jul 2002

posted posted 07-16-2002 11:44

OK here's a working version of bitdamaged's nice work. I couldn't resist since I'm about to write a bloggish tool for a client but needs are alittle different if anyone has thoughts - I want two pages updated at once, one for a linked article title and another for the actual article (the one the title's linking TO).

anyway here's the script:

code:
<?php

// If the form has been submitted we'll do this:
// changed from bitdamaged
if ($submit && $user && $email && $blog)
{
// First let's validate our data
$length = strlen($blog);

// Here 200 is the length we are using you will want to change $maxlen to what ever you want
$maxlen = 200;
if ($length > $maxlen) $error = "Your Blog entry is too long";

// if it's not too long then let's try writing the textfile
if (!$error)
{
$theupdatefile = 'updates.txt';
$open = fopen('updates.txt', 'r+');

// If it's open then keep going
if(!$open)
{
$error = 'The \'$theupdatefile\' file was not able to load';
}
else
{
//NOTE - this is changed from bitdamaged version so it prints values not literals
//ALSO renamed variables handle/user - I was getting confused
$handle = ("<a href=\"mailto:" .$email ."\" target=\"_blank\">" .$user ."</a>");
$writedata = fwrite($open, $blog ."\n" .$handle);
fclose($open);

// Let's make sure everything wrote okay
if (!$writedata) $error = "Could not write to file";
}
}
// Now if there is no error we'll write a all okay
if (!$error)
{
echo "Your form was submitted!";
}
}
else
{
// Here's a trick we want to print the form but we have it in
// The php delimiters so we're going to drop out of php mode so
// we don't have to write "echo "<html> ... blah blah blah"
// and escape out all the quotes
?>

<html>
<head>
<title>word iono</title>
</head>
<body>
<?
// Drop back into PHP to write out any errors
echo $errors;
?>
<table cellpadding="2" cellspacing="0" border="0" width="200" height="150">
<tr>
<td align="center">
<!--
now we are going to make this
submit to itself
-->
<form method="post" action="<? echo $PHP_SELF; ?>"></td>
<td><input type="text" name="user" value="my name is" /></td>
</tr>
<br />
<tr>
<td align="center"></td>
<td><input type="text" name="email" value="e-mail" /></td>
</tr>
<br />
<tr>
<td align="center"></td>
<td valign="top"><textarea name="blog" rows="8" wrap="wrap"></textarea></td>
</tr>
<br />
<tr>
<td>&nbsp;</td>
<td align="center"><input type="submit" name="submit" value="submit" /></td>
</form>
</tr>
</table>
<a href="updates.txt">view update</a>
</body>
</html>
<?
// Close the if/else statement
}
?>




enjoy....

there is neither Being nor non-Being, only Nothing, which is neither Being nor Non-Being

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-17-2002 05:56

wow...you're a beast, ha ha.

i hate to do this to you but...what if i just wanted a maximum amount of charectors to be in the text file and each time i would blog it would just write the new data and erase some of the old data to keep the amount of charectors i would want to maintain. that's more along the lines of what i wanted to do.

thanks again man...you're awesome

gibbon
Obsessive-Compulsive (I) Inmate

From: Oakland, CA USA
Insane since: Jul 2002

posted posted 07-17-2002 08:47

hmm... not sure I have enough information here. what are you writing that is more concerned w/the number of characters than the content of the text?

if I'm getting it right, is it that you have say a 300 character limit, and e.g. you already have 300 chars in updates.txt, and you post a 200 char blog to the updates.txt file, then what you see is the first or last 100 chars from the old version of updates.txt plus the new 200 chars? if so, do you prefer first or last set of chars from the old file to be retained? do you have any other elements separating the different updates you sent to updates.txt?

btw thx for the props

there is neither Being nor non-Being, only Nothing, which is neither Being nor Non-Being

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-17-2002 22:27

quote:
___________________________________________

is it that you have say a 300 character limit, and e.g. you already have 300 chars in updates.txt, and you post a 200 char blog to the updates.txt file, then what you see is the first or last 100 chars from the old version of updates.txt plus the new 200 chars?
___________________________________________

that is EXACTLY what i want to do. and i'd want to see the first 100 set of chars.

thank you

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-18-2002 09:16

Right but what he's asking (and what it sounds like) is that if you have 300 lines in the file, and then 200 lines in the new one. Do you want to wipe out all 300 lines of the old file? Or do you want to write over the file from the begninng, but since the 200 line post is shorter, at the end of that post you'd get the last 100 characters from the old post.

Or do you want to keep the last 100 lines from the old post and print the new one after that?






.:[ Never resist a perfect moment ]:.

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-18-2002 09:42

exactly...because i'll have a nice lil divider that will clearly seperate the post. like a link to my e-mail with the time and stuff, ie: .LiKwiD_@_12:00pm__//


understand? i'd keep the first 100 chars. of the older post and then the 200 new chars would overwrite it. but i'd want to array_reverse it so the new post would be at the top, and since it's reversed wouldn't the top 100 chars from the old post remain? i hope i'm not confusing the hell out of everyone

but thanks again, you guys fuggin own

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-18-2002 18:13

Well that's not difficult first just open the file using the r+ switch (open it for reading and writing with the pointer at the top of the document).

fopen('updates.txt','r+');

Then when you are done writing to it just use
ftruncate($open, $maxlen) // Where $maxlen is the maximum length of your file.



.:[ Never resist a perfect moment ]:.

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-20-2002 08:16

thank you bitdamaged! black pills for all

but...i think i might have a problem, since i am limiting the amount of charectors wont some of my e-mail links be broken and then i'd end up with either dave@yah, instead of my real e-mail address, or something to that effect instead of the link i want? any idea on how to fix this? ha ha...doesn't sound like i can

LiKwiD
Paranoid (IV) Inmate

From: NJ
Insane since: Mar 2002

posted posted 07-22-2002 09:15

still out there?

::bump::

« BackwardsOnwards »

Show Forum Drop Down Menu