Spartan 117 Posted December 1, 2006 Share Posted December 1, 2006 Hello, I am new to php and I would like to know how I would get the page heading, ie: Page 1 onto the page from a table in mysql. The page has 3 sections, so there are 6 fields in my table, "heading1", "text1", "heading2", "text2", "heading3", "text3". I want each section to be loaded from mysql, so the administrator (me) can make changes to the site without having to change the whole page and uploading via ftp.And also is it possible to make 1 page be able to edit all of them, but from 1 at a time, because I have an edit link next to everything that can be changed. Or maybe just one big page that can edit them all with name anchors will be fine if that isn't possible.Thanks Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/ Share on other sites More sharing options...
craygo Posted December 1, 2006 Share Posted December 1, 2006 It should be reletively easy. Give me a few minutes and I will get some code for you.Ray Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/#findComment-133453 Share on other sites More sharing options...
Spartan 117 Posted December 1, 2006 Author Share Posted December 1, 2006 [quote author=craygo link=topic=116991.msg477021#msg477021 date=1164987651]It should be reletively easy. Give me a few minutes and I will get some code for you.Ray[/quote]Thanks a lot, but I'm at BOCES right now, a technical school that I go to for 3 hours in the morning, I'll be going back to my boring home school >:( soon so I'll check back when I get home at 4pm NY time.Thanks Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/#findComment-133458 Share on other sites More sharing options...
craygo Posted December 1, 2006 Share Posted December 1, 2006 OK first things first.You should add 2 more fields to your table. If you like you can use this sql[code]CREATE TABLE `htmlpage` ( `id` int(11) unsigned NOT NULL auto_increment, `title` varchar(100) NOT NULL default '', `heading1` varchar(255) NOT NULL default '', `text1` varchar(255) NOT NULL default '', `heading2` varchar(255) NOT NULL default '', `text2` varchar(255) NOT NULL default '', `heading3` varchar(255) NOT NULL default '', `text3` varchar(255) NOT NULL default '', PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;[/code]Then here is the page to add/edit your pages[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>HTML Page Update</title></head><body><?php// Database connection hererequire('config.php');include('includes/mysql.php');// Set $self to $PHP_SELF$self = $_SERVER['PHP_SELF'];// check to see if the submit button has been pressedif(isset($_POST['update'])){// this is the id of the line you want to edit$id = $_POST['id']; // if it has loop through the POST variables foreach($_POST as $key => $val){ // we do not want the submit variable so we take it out here if($key <> "update"){ // We do not want to update the ID field if($key <> "id"){ // run your query to update the fields based on the $id $sql = "UPDATE htmlpage SET $key = '".$val."' WHERE id = '$id'"; $res = mysql_query($sql) or die (mysql_error()); // Make sure the query ran and echo out if it did or didn't if(!$res){ echo "Could not update field $key<br>"; } else { echo "Field $key Was updated<br>"; } } } }} else { if(isset($_POST['addpage'])){ // run your query to ADD a new page $sql = "INSERT into htmlpage SET title = '".$_POST['title']."', heading1 = '".$_POST['header1']."', text1 = '".$_POST['text1']."', heading2 = '".$_POST['header2']."', text2 = '".$_POST['text2']."', heading3 = '".$_POST['header3']."', text3 = '".$_POST['text3']."'"; $res = mysql_query($sql) or die (mysql_error()); // Make sure the query ran and echo out if it did or didn't if(!$res){ echo "Could not Insert Page data<br>"; } else { echo "Page data Inserted<br>"; } } else { if(isset($_POST['edit'])){ $id = $_POST['id']; $sql = "SELECT * FROM htmlpage WHERE id = '$id'"; $res = mysql_query($sql) or die (mysql_error()); $r = mysql_fetch_assoc($res); echo "<form action='' method=POST> <input type=hidden name=id value='".$_POST['id']."'> <table width=500 align=center>"; foreach($r as $key => $val){ // When data is inserted slashes and line breaks are added so you need to take them out $val = stripslashes($val); if($key <> "id"){ if($key == "title"){ echo "<tr> <td width=100>$key</td> <td width=400><input type=text name=$key size=50 value='$val'></td> </tr>"; } else { echo "<tr> <td width=100>$key</td> <td width=400><textarea name=$key cols=50 rows=5>$val</textarea> </tr>"; } } } echo "<tr> <td colspan=2 align=center><input type=submit name=update value=Submit></td> </tr> </table> </form>"; } else { if(isset($_POST['add'])){ echo "<form action='".$self."' method=POST> <table width=550 align=center> <tr> <td width=150>Page Title</td> <td width=400><input type=text name=title size=50></td> </tr> <td width=100>Header 1</td> <td width=400><textarea name=header1 cols=50 rows=5></textarea> </tr> </tr> <td width=100>Text 1</td> <td width=400><textarea name=text1 cols=50 rows=5></textarea> </tr> </tr> <td width=100>Header 2</td> <td width=400><textarea name=header2 cols=50 rows=5></textarea> </tr> </tr> <td width=100>Text 2</td> <td width=400><textarea name=text2 cols=50 rows=5></textarea> </tr> </tr> <td width=100>Header 3</td> <td width=400><textarea name=header3 cols=50 rows=5></textarea> </tr> </tr> <td width=100>Text 3</td> <td width=400><textarea name=text3 cols=50 rows=5></textarea> </tr> <tr> <td colspan=2 align=center><input type=submit name=addpage value='ADD Page'></td> </tr> </table> </form>"; } else { echo "<form action='".$self."' method=POST> <table width=400 align=center> <tr> <td colspan=2 align=center><input type=submit name=add value='ADD Page'></td> </tr> <tr> <td width=300>Page Description</td> <td width=100 align=center>Select</td> </tr>"; $sql = "SELECT * FROM htmlpage"; $res = mysql_query($sql) or die (mysql_error()); while ($r = mysql_fetch_assoc($res)){ echo "<tr> <td>".$r['title']."</td> <td align=center><input type=radio name=id value='".$r['id']."'></td> </tr>"; } echo "<tr> <td colspan=2 align=center><input type=submit name=edit value='Edit Page'></td> </tr> </table> </form>"; } } }}?></body></html>[/code]and here is a sample of how to display the data[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><?php// connect to database hererequire('config.php');include('includes/mysql.php');// Set which page you want to use here$id = '2';// Query database$sql = "SELECT * FROM htmlpage WHERE id = '$id'";$res = mysql_query($sql) or die (mysql_error());$r = mysql_fetch_assoc($res);$check = mysql_num_rows($res);// check to make sure your id existsif($check > 0){// show this if id exists// convert all fields to strip slashes and line breaks to <br>$heading1 = nl2br(stripslashes($r['heading1']));$text1 = nl2br(stripslashes($r['text1']));$heading2 = nl2br(stripslashes($r['heading2']));$text2 = nl2br(stripslashes($r['text2']));$heading3 = nl2br(stripslashes($r['heading3']));$text3 = nl2br(stripslashes($r['text3']));?> <title><?=$r['title']?></title></head><body><table width=800 align=center> <tr> <td><h1><?=$heading1 ?></h1></td> </tr></table><?php} else {// show this if ID does not exist?><p align=center>Page Number does not exist!!!</p><?php}?>[/code]:)I was bored today so did this for ya. Obviously you will have to make it look pretty like and all that good stuff. I put comments in to help you understand what it is doing.Let me know if you have problemsRay Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/#findComment-133610 Share on other sites More sharing options...
Spartan 117 Posted December 1, 2006 Author Share Posted December 1, 2006 Thanks a lot for the help ;DBut I do have a question about this line:[quote][code]require('config.php');include('includes/mysql.php');[/code][/quote]I have the config.php, but what should the mysql.php contain?The config.php contains:[code]<?php$server = "DOMAIN"; // server to connect to.$database = "DATABASE"; // the name of the database.$db_user = "LOGIN"; // mysql username to access the database with.$db_pass = "PASSWORD"; // mysql password to access the database with.$table = "content"; // database table?>[/code]Do I need mysql.php, or am I fine with just config.php? And also, what is the difference between include and require?Thanks Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/#findComment-133693 Share on other sites More sharing options...
craygo Posted December 3, 2006 Share Posted December 3, 2006 include will insert the file into the current php file. If it cannot be found or if there is an error, the main script will still run. If you require a file, if there is an error or it cannot be found the script will end.Also the config and mysql files is how I connect to mysql, you do not have to use them. just connect to mysql the way you normally would.Ray Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/#findComment-134305 Share on other sites More sharing options...
Spartan 117 Posted December 3, 2006 Author Share Posted December 3, 2006 Thanks for all of the help Ray. You have just started me on something new I want to work on - my own cms. I know that this will be difficult, especially with just one person, but sometimes I like a challenge. What I did now was a challenge - made a website for an admin to be able to change content because I have had very little experience with php.I have always wanted to make my own forums - maybe I'll do that first before trying for a cms.Thanks For All Of The Help Link to comment https://forums.phpfreaks.com/topic/29114-read-from-mysql-resolved/#findComment-134471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.