• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Coding, Why?!

AdamJ

When's 1999?
NLC
God, why is coding such a pain! I am coding my own contact form from scratch, done a config.php page, then a contact.php page, now done the thank_you.php page, now stuck on what to do to boost my confidence with php coding.

Anyone fancy giving me some ideas on what to code?

Thanks :)

Note: this isn't spammy, this is asking for some ideas!
 
try a simple news script - have 2 parts to it.

front end - let users view a single post or all posts (find a way to split them up over several pages ;) )

admin side - add/edit/delete news - use sessions too

this will introduce you to some interesting concepts:

sessions (a must know for later on)
mysql dbs and manipulation (also essential)
 
Yeah JohnN but I no nothing about MySQL, and already used a session on this contact script, I think it is anyway:
PHP:
<?php session_start();
$ses_id = session_id();        // DO NOT REMOVE THIS LINE!
$_SESSION['ses_id'] = $ses_id;    // OR THIS ONE!
?>
 
thats sessions more or less;)

heres some mysql stuff.

familiar with phpmyadmin? create a user/password/database then launch phpmyadmin. click your database then create a new table called news

have 3 coloumns. one called ID select INT, AUTO_INCREMENT and click the key option to make it the primary key.

one called title, make it varchar.

one called news make it text.

on your front-page code copy-past the connection code cpanel gives you.

so

PHP:
<?php

//connection code here

$mysql = mysql_query("SELECT * FROM news");

while($row = mysql_fetch_array($mysql)){

print_r($row);

}

?>

there you go! now when you insert an entry via phpmyadmin (leave the ID section blank, put whatever in title and news) it should appear on your frontpage.

tell me what you get and I'll advise from there;)
 
Why not try and have the form add CSV details to a flat file DB, so it appends on every submission, you should be able to do that - and no MySQL :D
 
Last edited:
thats sessions more or less;)

heres some mysql stuff.

familiar with phpmyadmin? create a user/password/database then launch phpmyadmin. click your database then create a new table called news

have 3 coloumns. one called ID select INT, AUTO_INCREMENT and click the key option to make it the primary key.

one called title, make it varchar.

one called news make it text.

on your front-page code copy-past the connection code cpanel gives you.

so

PHP:
<?php

//connection code here

$mysql = mysql_query("SELECT * FROM news");

while($row = mysql_fetch_array($mysql)){

print_r($row);

}

?>

there you go! now when you insert an entry via phpmyadmin (leave the ID section blank, put whatever in title and news) it should appear on your frontpage.

tell me what you get and I'll advise from there;)

JohnN, I've never used phpMyAdmin before, have no idea on it!

Can you possibly, if you don't mind, email me something more detailed explaining how to do what your on about?


Also Decker, I've always wanted to learn how to do Flat File databases, could help store information from my contact form for back-up.
 
PHP:
<?php
/**
 * An example of flat file database, using almost csv format
 * Over simplified
 *
 */
class FlatFile
{
	/**
	 * Filename of file being worked with
	 *
	 * @var string
	 */
	var $filename ;
	/**
	 * Field delimiter
	 *
	 * @var char
	 */
	var $delimiter ;
	/**
	 * Number of columns per line
	 *
	 * @var int
	 */
	var $columns ;
	/**
	 * Handle to filename
	 *
	 * @var resource
	 */
	var $handle ;
	/**
	 * Array of cached data
	 *
	 * @var array
	 */
	var $data ;
	/**
	 * Construct a FlatFile object
	 *
	 * @param string $filename
	 * @param int $columns
	 * @param char $delimiter
	 */
	function __construct( $filename, $columns, $delimiter = ',' )
	{
		$this->filename = $filename ;
		$this->delimiter = $delimiter ;
		$this->columns = $columns ;
		$this->data = array( );
		$this->handle = null ;
		
		if( file_exists( $filename ) )
		{
			foreach( file( $filename ) as $num => $line )
			{
				if( ( $data = explode( $this->delimiter, $line, $this->columns ) ) )
				{
					$this->data[ ] = array_map( 'trim', $data ) ;
				}
			}
		}
	}
	/**
	 * Add a line to the cache
	 *
	 * @param array $array
	 */
	function line( $array )
	{
		if( count( $array ) === $this->columns )
		{
			$this->data[ ] = $array;
		}
	}
	/**
	 * Write cached data to this->filename
	 *
	 * @return boolean
	 */
	function save( )
	{
		if( ( $this->handle = fopen( $this->filename, 'w+' ) ) )
		{
			/**
			 * Probably should lock the file for exclusive access while writing what could be a world of data
			 */
			flock( $this->handle, LOCK_EX );
			
			while( ( $line = array_shift( $this->data ) ) )
			{
				if( count( $line ) and !fwrite( $this->handle, sprintf( "%s\r\n", implode( sprintf( '%s ', $this->delimiter ) , $line) ) ) )
				{
					return;
				}
			}
			
			fclose( $this->handle );
			
			return true;
		}
	}
	/**
	 * Retrieve parsed cache
	 *
	 * @return array
	 */
	function data( )
	{
		if( count( $this->data ) )
		{
			return $this->data ;
		}
	}
}
/**
 * New object of type FlatFile
 */
$csv = new FlatFile( 'file.db', 4 );
/**
 * Write a couple of lines to cached data
 */
$csv->line( array(
	"John",
	"Watkins",
	"23",
	"Male"
) );
$csv->line( array(
	"The",
	"Other Guy",
	34,
	"Male"
) );
/**
 * Write out to csv
 */
$csv->save( );
/**
 * Reset
 */
$csv = null ;
/**
 * Reopen
 */
$csv = new FlatFile( 'file.db', 4 );
/**
 * Output nice data
 */
echo "<table width=\"100%\">\n";
echo "\t<tr>\n";
echo "\t\t<th>First</th>\n";
echo "\t\t<th>Last</th>\n";
echo "\t\t<th>Age</th>\n";
echo "\t\t<th>Sex</th>\n";
echo "\t</tr>\n";
foreach( $csv->data as $id => $line )
{
	echo "\t<tr>\n";
	printf( "\t\t<td>%s</td\n", $line[0] );
	printf( "\t\t<td>%s</td>\n", $line[1] );
	printf( "\t\t<td>%s</td>\n", $line[2] );
	printf( "\t\t<td>%s</td>\n", $line[3] );
	echo "\t</tr>\n";
}
echo "</table>\n";
?>

Something like that, I just wanted a two minute break from what I'm doing. I urge you to learn mysql, if you can understand arrays you already know the php part. SQL is quite easy to learn and applicable in a lot of commonly used languages, which you may or may not be interested in ...
 
Last edited:
Thanks for that Joe, bit confusing but will learn from it. I'm trying to learn MySQL but its blooming difficult, can't find sufficient tutorials/ebooks (free) to help me.
 
I've got your e-mail somewhere Adam and I have a few free e-books (yes seriously free ones) I'll look them out - give me a prod after a couple of days
 
MySQL isn't hard at all

Lets say you have a database all ready created and the tables are all created with contents aswell.

PHP:
<?
mysql_connect("localhost", "MySQL_USER", "MySQL_PASS"); //This connects to server

mysql_db_select("news");

$sql = "SELECT * FROM news_content"; //SQL query that gets the data
$get = mysql_query($sql); //Executes the query


while($data = mysql_fetch_array($get)) {
echo "The title is ".$data["Title"]."<br>";
echo "The content is <br>".$data['content']."<br><br>";
}
//That will echo all data in the news_content table eg.
//The title is Test
//The content is
//I like testing stuff
//
//The title is Test123
//The content is 
//Yes I can test stuff
//
//It'll do this for EVERY entry inside the table.
?>

If you need any more help with some simple PHP and MySQL script just PM me ;)

ALSO - OMG 99th post
 
Last edited:
Guys stop confusing me!

Thanks nwhstaff! +rep awarded for your help :) *when the system lets me :( *
 
How is that contributing to the topic serverorigin??

Just to tell everyone (so I'm on topic), I'm currently going through an extensive tutorial I found online about flat file databases :)
 
Back
Top