PDA

View Full Version : Updating Website Content


neuro
04-21-2007, 04:39 PM
Hi,

Let say i wan to create a website that can be updated instanly (without opening the html file). it will be something like a forum, where i can just type something and once i submit it will automaticly update my index page. Of course only certain people have access to the page where they can update the website (which can be solve by using user log in and using database).

So the main question here is "how to do it?" is it a must to it with database?

Thx.

davidj
04-21-2007, 05:26 PM
you want a CMS

Joomla is a popular choice

http://www.joomla.org/

neuro
04-21-2007, 06:09 PM
Yeah.. something like that.

well i'm aware of joomla or mambo, but is there like another way to do it our own?

it doesnt have to be like a mambo or joomla control panel, it can be a simple form to submit content.

davidj
04-21-2007, 06:14 PM
PHP is the way to go

i will help you develop a new skill but you have to want to learn. If you just want a quick fix then let me know

neuro
04-21-2007, 06:42 PM
Hehe... i'm willing to put everything to learn it.

I did few try by creating a form with a text field and submit button, which will submitted the data to SQL and display it in my Index page with a php code. It works fine, but i was wondering what if let say i put like a very long content maybe 2 or 3 paragraph, can SQL return the result in 2 or 3 paragraph?

So yes.. if u do have some skill to teach or even little info on this matter, i'm willing to hear about that.

davidj
04-21-2007, 06:49 PM
here is some homework

http://www.dreamweaverclub.com/vtm/php-mysql-apache.php

its me! teaching stuff!

once you cover this we will build your application together

i will give you my time for free but you have to give me the same investment

neuro
04-21-2007, 06:59 PM
I've done this part.

To be honest thats how i learn how to create database, creating registration form for user, inserting the data to SQL. I have learned lots from that tutorial. From that tutorial i manage to finish this web:

http://ads.boontau.com./Index.php

davidj
04-21-2007, 07:20 PM
if you have completed the tutorials then you know how to INSERT, UPDATE and DELETE

these are the building blocks of exactly what you want

i dont know what you want other than whats in the tuts

neuro
04-21-2007, 07:26 PM
Yes..

I did ask earlier what if let say the content consist more than 1 paragraph ( 2 or 3 perhaps) can the database recgonize that input as a paragraph so when i call the data to display on my index, it will looks like the expected result?

Thx.. for answering the question.

davidj
04-21-2007, 07:45 PM
ok

first...

Thx.. for answering the question.

was that sarcasm?

anyhoo that went over my head

answer to your question..

if your keying into one text area then you need flags to identify <br>'s and <p>'s

yu can do this a number of ways. Either enter these as text along with your content or add a button that puts this stuff in for you

which one do you fancy

neuro
04-22-2007, 08:30 AM
arg..... no no no there is no "sarcasm". what i meant is just "thanks" coz look like u are the only one answering the question so far.


Either enter these as text along with your content or add a button that puts this stuff in for you

which one do you fancy


Prefer the first one, but willing to know both ways.

Again sorry if u think i was trying to show sarcasm in the earlier words.

Thx

davidj
04-22-2007, 12:50 PM
php has a function called nl2br or new line to break or \n to <br>

this will do what you need if you want to display text on a site with formatting as you wrote it in a text area

post your code with the form included and we will see if we can sort this for you

neuro
04-22-2007, 01:03 PM
Index.php

<?php
require_once("Connections/Connection.php");
session_start();
$userid = $_POST['userid'];
$passid = $_POST['passid'];
$submitted = $_POST['submitted'];
if ($userid && $passid) {
/////////////////////////////////////////////
$query = sprintf ("SELECT * FROM Admin where Username='$userid' and Password='$passid'");
$result = @mysql_query ($query);
$rowAccount = @mysql_fetch_array ($result);
/////////////////////////////////////////////
}
if ($rowAccount) {
$_SESSION['id'] = $rowAccount['Username'];
header ("location: Admin.php");
exit;
} elseif ($submitted) {
echo '<script type="text/javascript"> window.alert("You Enter Wrong Username or Password"); </script>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>MBT Central</title>
<style type="text/css">
<!--
-->
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>&nbsp;</td>
<td width="720">
<div id="banner"> </div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td bgcolor="#000000">
<div id="subcontentleft">
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
</div>
<div id="contentcenter">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

$max_results = 5;

$from = (($page * $max_results) - $max_results);
$sql = mysql_query("SELECT * FROM News LIMIT $from, $max_results");
?>

<?php do { ?>
<tr>
<td class="title"><?php echo $row['Title']; ?></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td class="content"><?php echo $row['Content']; ?></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<?php } while($row = @mysql_fetch_array($sql)); ?>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td>
<div align="center">
<?php

$total_results = @mysql_result(mysql_query("SELECT COUNT(*) as Num FROM News"),0);

$total_pages = ceil($total_results / $max_results);

echo "<center>Select a Page<br />";

if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}

if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}

echo "</center>";

?>
</div>
</td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>


Update.php

<?php
require_once("Connections/Connection.php");
session_start();
$data2 = $_POST['title'];
$data3 = $_POST['update'];
$submit = $_POST['Submit'];
if ($submit && $data2 && $data3) {
/////////////////////////////////////////////
$query = sprintf ("INSERT INTO News (Title, Content) values ('$data2', '$data3')");
$result = @mysql_query ($query);
$rowAccount = @mysql_fetch_array ($result);
/////////////////////////////////////////////
}
elseif($submit) {
echo '<script type="text/javascript"> window.alert("One of your fields is empty !"); </script>';
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>MBT Central</title>
<style type="text/css">
<!--
-->

</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>&nbsp;</td>
<td width="720">
<div id="banner"> </div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td bgcolor="#000000">
<div id="subcontentleft"></div>
<div id="contentcenter">
<form id="form1" name="form1" method="post" action="">
<label>
<input name="title" type="text" id="title" size="50" />
</label>
<label> <br />
<br />
<textarea name="update" cols="60" rows="5" id="update"></textarea>
</label>
<p>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
<input name="submitted" type="hidden" id="submitted" value="1" />
</p>
</form>
</div>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>


So this is the 2 page that i currently used for testing. Any suggestion?

davidj
04-22-2007, 01:15 PM
i take it that the following is what displays your text


<td class="content"><?php echo $row['Content']; ?></td>

if so change it to this...

<td class="content"><?php echo nl2br($row['Content']); ?></td>

i think this will work although the insert statement might strip the \n out before the db insert/update takes place. If this is the case you will have to do the conversion before the SQL happens.

try this if the first option fails

where you have

$data3 = $_POST['update'];

do this

$data3 = nl2br($_POST['update']);

neuro
04-22-2007, 01:26 PM
ok.. i tested both ways and it works :)

This will solve my current problem thanks to u David.

Any other suggestion that i need to know, just as further refference for me, since i'm still learning PHP.

Thx..

davidj
04-22-2007, 01:41 PM
TIP: dont echo anything in PHP thats above the <HTML> tag

for one you dont have any control over the display position and it looks shite

always assign any output string to a variable then you can use that variable to echo anywhere you want in the HTML

TIP2:

Keep all your errors together in one location

create a blank document called errors.php (remove all html)

in the document add the following...

<?php
// add as many errors as you like...
define("ERR01","This is error message 1"); //<< error 1
define("ERR02","This is error message 2");
define("ERR03","This is error message 3");
?>

now at the top of every page where you want to display errors just include the error file...
include("errors.php");

then to display the error just assign to a var like this...

$errorMessage = ERR01;

then echo the $errorMessage where you want it displayed!

this method allows you to keep all your messeges on one area and saves you trawling through your site looking for messages you want to edit

neuro
04-22-2007, 02:01 PM
Yeah.. i guess i still have to learn on the coding structure part.

What about the first tip? can give small sample, only if you dun mind. Dun worry it's not about "how u explain" but its just because of my english, hehehe.

davidj
04-22-2007, 02:08 PM
you have this...
if ($rowAccount) {
$_SESSION['id'] = $rowAccount['Username'];
header ("location: Admin.php");
exit;
} elseif ($submitted) {
echo '<script type="text/javascript"> window.alert("You Enter Wrong Username or Password"); </script>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>MBT Central</title>
<style type="text/css">
<!--
-->
</style>
</head>
<body>.......
where you have...
} elseif ($submitted) {
echo '<script type="text/javascript"> window.alert("You Enter Wrong Username or Password"); </script>';
}
i would do it like this...

} elseif ($submitted) {
$message = "You Enter Wrong Username or Password";
}

then you can echo $message in your HTML where you want it displayed

i never use Javascript Alert popups to display errors because you dont have control over your error appearance

neuro
04-22-2007, 02:36 PM
Alrite.. i'll take note on that part.

anyway Thanks for everything, it really helps a lot.

neuro
04-25-2007, 03:56 PM
David..

about the function "nl2br" it will include <br> automaticlly right..

when i wan to edit the post i got those <br> on my textbox and if i click the submit button ti will generate another <br>, is there anyway to solve this?


Thanks

davidj
04-25-2007, 04:19 PM
about the function "nl2br" it will include <br> automaticlly right..

yes and its good if your just outputing to HTML. If, however you are wanting to edit the variable in your text area your going to have to strip out and replace <br> with \n so the format retains its structure.

so the variable that echos in your text area is called $dbcontent
$br= "<br>"; //<< look for
$n = "\n";//<< replace with
$newcontent = str_replace($br, $n, $dbcontent);//<< do the swap

then echo the new var ($newcontent) in your text area

neuro
04-25-2007, 07:17 PM
k... some question here about the code that u just wrote.

so the $dbcontent is = my textbox ID ? or i write it as it is?

the current code that i have :


<textarea name="editnewscontent" cols="50" rows="10" id="editnewscontent"><?php echo $rowUpdate['Content'];?></textarea>


i understand that i have to replace


<?php echo $rowUpdate['Content'];?>


to


<?php echo $newcontent;?>


so should i declare that $dbcontent = $rowUpdate['Content'] ?

davidj
04-25-2007, 08:13 PM
you can if you want but its not nessessary

if you do this in the loop your $rowUpdate array variable will run ok. If your not using a loop with your textbox then you can assign a variable to represent $rowUpdate if you want but its not nessessary


$br= "<br>"; //<< look for
$n = "\n";//<< replace with
$newcontent = str_replace($br, $n, $rowUpdate['Content'];);//<< do the swap

then $newcontent is the fixed variable with the right code so use this var

neuro
04-26-2007, 05:43 AM
alrite...

Thanks david.