Jump to content

PHP forms editing .txt within site | Events 'puzzle'


iraisis

Recommended Posts

I am very new to PHP and very eager to just understand all of the stuff i have been desperately trying to get to work for the last week. I decided to give up on copy pasting and actually find help.

 

My main project right now is this: I want to have a website that I can edit from within a logged in section of the site (I've got the login part figured out - that is I have some code that at least has a pass and usrnm).

 

I have a events page with 4 events, each event consisting of date, header and text:

<div id="event">
<p class="date">
	<? include_once('date.txt')?>
</p>
<h3>
	<? include_once('header.txt')?>		
</h3>
<p class="eventinfo">
	<? include_once('text.txt')?>	
</p>
</div>

 

I use .txt includes so that I can edit the actual texts inside of a form. My first attempt was to edit each file inside of one page. Failed due to

<?php echo $_SERVER["PHP_SELF"] ?>

.

 

Second attempt was to have the page get edit.php file:

			<?php 
			$fn1 = 'date.txt'; 

			if (isset($_POST['content'])) 
			{ 
    			$content = stripslashes($_POST['content']); 
    			$fp1 = fopen($fn1,"w") or die ("Error opening file in write mode!"); 
    			fputs($fp1,$content); 
    			fclose($fp1) echo("Date has been saved") or die ("Error closing file!");
			} 
			?> 
		<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post"> 
    			<textarea rows="5" cols="40" name="content"><?php readfile($fn1); ?></textarea>
    			<input type="submit" value="Save"/>  
		</form>

 

And have the page reload and get edit header section,  then reload again for the text. But didn't see how to do this without making three pages.

 

What I want is that I can within at least tow pages do all the following: Login, choose which event to edit, then adit all three parts and save. My brains are fried from the combination of learning PHP and being over excited about having a working system. Hopefull for some answers! Thanks!!

 

PS: my events page http://naturalhorse.me/sites/testing/events.php

Thanks, Isa.

 

 

 

 

Link to comment
Share on other sites

Looks to me like you need a database design as doing flat-files (storing data in files) for this type of data can be much harder to learn than a few simple database calls.

 

Take 5 minutes and google for a simple mysql tutorial, you will soon see that on a single page you could have all 3 fields for the date/header/text for the events, you can even easily handle several events within what we call a Table (or collection), You can sort the table, choose specific "items" in the table using Comparisons etc.

 

To get you started, whilst reading the tutorials bear this in mind;

 

TableName: my_events

Field1: a number/integer (INT)

Field2: characters (VARCHAR - maxlength 255 characters)

Field3: timestamp (BIGINT)

Field4: lots of characters (TEXT - you cannot do comparisons on this field type unless it is a "LIKE" comparison, you learn this later)

 

Field1    Field 2           Field 3           Field4
  id      Snowboarding      1296255400        Going snowboarding

 

Hope this helps and good luck

Link to comment
Share on other sites

Thanks for the quick reply!

 

Your information was very helpful. I have read up in MySQL databases and created one that i have also successfully connected to in my site using PHP.

 

My continued question is:

 

What is the best way to manage my 'events' in the database? ... Is it complicated to create an small CMS (to manage my 'events' = delete, add, edit)? So that I have a table with checkmark inputs and different options.

 

Or are there pre-made systems that I can use (that aren't as advanced as Wordpress/joomla/etc since i only want to manage this small section)?

 

I realize these are many and vague questions. I am grateful for any input.

 

This is my current test: http://naturalhorse.me/sites/testing/editdb.php

And the code I am currently working around:

 

<?php
//Connect to Database
include("connect.php");

//Retreive Data from events table
$query="SELECT * FROM events";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<html>
<body>
<h2>Edit Events</h2>
<p>Add, Edit and Delete your events here.</p>
       	<table border="0">
	<tr>
		<th>[ ]</th>
		<th>Order</th>
		<th>Date</th>
		<th>Name</th>
	</tr>
	<form name="events table" method="post" action="edit.php" />
<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"date");
$f3=mysql_result($result,$i,"name");
$f4=mysql_result($result,$i,"text");
?>
	<tr>
		<td><input type="checkbox" value="check" name="select<?php echo $f1; ?>" /></td>
		<td><?php echo $f1; ?></td>
		<td><?php echo $f2; ?></td>
		<td><?php echo $f3; ?></td>
	</tr>
<?php
//
$i++;
}
?>
	<input type="submit" name="edit" value="edit"/>
	<input type="submit" name="delete" value="delete" />
	</form>	
</table>
<input type="submit" name="add new" value="new"/>
</body>
</html>

 

Thanks, Isa

Link to comment
Share on other sites

I would personally use two "views",

 

1) Would show a calendar for a given month, it looks nice and can be clickable, with events listed in each box for that day, if you have space you could put the time range as well.

 

2) Day View, this would list every event in as much detail as possible for the given date/day.

 

The admin/management part would imo be a simple admin login, which would show extra buttons normal users cannot see.

 

These buttons;

Add/Remove/Edit

 

The Add is very simple, you create the form in pure html, then use a simple insert script to sanitize (make safe) the user input and insert the new row into mysql.

 

The Remove is relatively easy but slightly more complex; it will be a simpel script that will take a number (id) of a row and check if it exists. If it exists then you can display a "confirmation page" to the user, are you sure you want to delete ..... When/if the user clicks yes then it will simply DELETE FROM the row.

 

The Edit is the most complex, first it selects the data for the given row you want to edit. Then it shows a form with all the values already filled in. Then when you click submit it will go to a small script that will "update" the mysql row given.

 

The hardest part to code will be the actual calendar, being dynamic, i would suggest trying to follow a tutorial found from google and then modify it to your needs. I would _not_ just copy/paste and modify because you wont have a clue how it works. IMO first step is to build the calendar view. (a "View" is a term used to describe a page or object the user sees in respect to the program/project).

 

hope this helps

Link to comment
Share on other sites

Thanks again for you help. I have pretty much finished my little CMS, although I am encountering a frustrating problem. The script I'm using is ignoring the HTML in the bottom firsthand which means my footer is missing. As you can see I have tried to echo it with a function but have not succeeded: How can I get my footer back?

 

Here is my reduced code:

 

<?
include('connect.php');

//Injection preventation and global variables to easier names.

$id = mysql_escape_string($_POST["id"]);
$date = mysql_escape_string($_POST["date"]);
$name = mysql_escape_string($_POST["name"]);
$text =mysql_escape_string($_POST["text"]);

?>

<html>
<head>
    <link rel="stylesheet" href="#.css" type="text/css"></link>
</head>
<body>
<div id="content">
	<h2>Manage events</h2>
	<div class="edit_section">
	<h4>Click on an event to edit or delete it or <a href="new.php"><input type="button" name="new" value="Add an Event"/></a> here.</h4>

<?php

// Script EDITS and DELETES events

if ($_GET["action"] == "edit") {
$query=mysql_query("SELECT * FROM events WHERE id='".$_GET["id"]."'");
if ($query) {
	$row=mysql_fetch_array($query);
	echo "You are editing event \"".$row["name"]." \". <br><br>";
}
else
	editNews();
	footer();
}

elseif ($_POST["action"] == "delete") {
$query=mysql_query("DELETE FROM events WHERE id='".$id."'");
if ($query)
	echo "The record was successfully deleted. <br><br>";
else	
	echo "Failed to delete the record <br><br>";
	editNews();
	footer();
}

elseif ($_POST["action"] == "doedit") {
$query=mysql_query("UPDATE events SET date='".$date."', name='".$name."', text='".$text."' WHERE id='".$id."' ");
if ($query)
	echo "\"$name\" was successfully updated.";
else
	echo "Failed to update $name.";

	editNews();
	footer();
}

else {
editNews();
footer();
}

//My failed attempt to include the footer at the bottom of the page.

function footer() {
include_once('inc/three.inc.php')/*This script ends the HTML page.*/;
}


// Echos out table for managing events:

function editNews() {
?>
<table width="600px">
	<tr>
		<td width="5%"> # </td>
		<td width="45%">Event date info</td>
		<td width="50%">Event Titles:</td>
	</tr>
<?php
$query=mysql_query("SELECT * FROM events ORDER BY id ASC");
while($row = mysql_fetch_array($query)){
	echo("
	<tr>
		<td>".$row['id']."</td>
		<td>".$row['date']."</td>
		<td><a href=editdb.php?id=".$row['id']."&action=edit>".$row['name']."</a></td>
	</tr>
	");
}

exit(0);
?>
</table>
<?php
}
?>
		<form action="editdb.php" method="post">
			<p>Date:</p>
			<input type="text" name="date" value="<? echo $row['date'] ?>" />
			<p>Event Name:</p>
			<input type="text" name="name" value="<? echo $row['name'] ?>" />
			<input type="submit" name="Submit1" value="Edit Existing" />
			<p>Information:</p>
			<textarea name="text" rows="6" cols="50"><? echo $row['text'] ?></textarea>
			<input type="hidden" name="action" value="doedit" />
			<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
		</form>
		<form action="editdb.php" method="post">
			<input type="submit" name="Submit2" value="Delete Event" />
			<input type="hidden" name="action" value="delete" />
			<input type="hidden" name="id" value="<? echo $row['id'] ?>" />
		</form>

<?
footer()
?>

 

I'm very happy to have gotten this far.. but I need my footer. I also tried putting the edit delete script and html within a if statemant but coudn't get it to work either. Thanks again.

 

Link to comment
Share on other sites

put footer(); at the end of editNews function, put and exit(); at the end of footer function, this should simplify things.

 

Also, functions should return something and be used as tools rather than segmenting or hiding code. And try to use variables instead of echos, it will help in the long run.

 

hope this helps

Link to comment
Share on other sites

Yes! All works now!

 

I ended up putting an include in the function as well as at the bottom of the page. All i was missing before was putting the exit() in the right place. I couldn't seem to get this working with variables though. Tried making a variable and then echoing it out instead of using two includes:

 

$footer = include_once('inc/footer.inc.php');

//and then in the right places:

echo $footer;

 

But alas, my problems are solved.

 

Thanks a bunch.

Link to comment
Share on other sites

Glad it's sorted.

 

Just to clear up; when you use include(), your basically inserting the file into the exact position you called include().

Your footer.php should only contain one variable (however large). After you include, you then use the variable as if it was where you called the Include.

 

Hope that makes sense

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.