Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP & MySQL Introduction.

Similar presentations


Presentation on theme: "PHP & MySQL Introduction."— Presentation transcript:

1 PHP & MySQL Introduction

2 A session keeps variables ‘in mind’ after you load a new page
It does this in the $_SESSION array You need two extra lines in THE TOP of your pages session_name( “oneword_no_spaces” ); session_start(); This of it as a fridge You put something in de box before you go Take something out after you return

3 $_SESSION[‘valid_user’]
Three steps of login… $_SESSION[‘valid_user’] $_POST[‘submit’] mysql_query()

4 The proper order

5 Database > link $query = “SELECT * FROM books”; //execute & loop
$row = mysql_fetch_assoc($result); $id = $row[‘id’]; $title = $row[‘title’]; print "<p>".$title." ["; print "<a href='?r=".$id."'>delete print "</a>]<p>\n";

6 Q: What’s the proper order?
Link > Database <?php $remove_id = $_GET['r'] + 0; if ($remove_id > 0 ){ $query = “DELETE FROM books WHERE id=”.$remove_id.” LIMIT 1”; mysql_query( $query ); } ?> Q: What’s the proper order?

7 Tasks $_GET/$_POST exercise Delete test

8 Goals You know what a CMS is You’ll create a website from pages stored in a database You’ll create a form to put pages into a database We’ll use the UPDATE statement

9 Content Management System
CMS Content Management System Users Permissions Content Examples UvA DWS Joomla! ( ) Drupal ( )

10 Many people can’t write HTML Conforms to company style
Why a CMS? Many people can’t write HTML Conforms to company style Makes managing a website easier Who made a page? When? Still relevant? Who can change what? Can be handled from a website Anytime, anyplace, anywhere

11 Not really, for a small site
Handy for us? Not really, for a small site One user Not a lot of content However No more FileZilla for every change No need to copy/paste header & footer Menu makes itself with a bit of programming Limitations We’ll use HTML code Images still need to be uploaded

12 Table Primary key = id (int, primary key, a.i.) Content is your html code Menu_name is displayed in the menu Level is the horizontal hierarchy Position is the vertical hierarchy / order website.php?page_id=4

13 Create table if you haven’t done so yet
Exercise Create table if you haven’t done so yet Initials + _ + website Insert a few pages in phpMyAdmin Just add some rubbish but pay attention to the level and order so you can check if your menu is properly generated Make a new folder in your website Example: cindy/cms/ Make a new file index.php Example: cindy/cms/index.php

14 Creating your main page
We’ll only use one file to show every page: index.php For now we’ll put header & footer etc all in that file We need a php script with the following Database connection SELECT id, menu_name, position, level Use the results to make the menu SELECT content WHERE id=x Show the current page content

15 Menu script Animals Welcome Birds level position Bees
if ( $row[‘level’] > 1 ){ print “<p class=‘submenu’>”; } else{ print “<p class=‘menu’>”;

16 Menu script print “<p class=‘menu_” $row[‘level’].”’>”; print “<a href= ‘index.php?page_id=“ $row[‘id’].”’>”; print $row[‘menu_name’]; print “</a></p>\n”;

17 Exercise Create table if you haven’t done so yet Insert a few pages in phpMyAdmin Make a new folder in your website Make a new file index.php Make a table for banner, menu, content Add menu and content php

18 What goes out must go in Now for the input side Form input types Names
id will be dealt with by database content > textarea Menu_name > text input Position > text input (for now) Level > radio input or select Names Same as database fields

19 Add form html, use print for each line
Exercise, admin pages Make a new file cms/admin/index.php Add form html, use print for each line

20 You want to see a list of current pages Below or above comes the form
What goes out must go in You want to see a list of current pages Think of book pages Below or above comes the form On submit we add a new page INSERT INTO xyz_website VALUES ( NULL, ‘”.$content.”’, ‘”.$menu_name.”’, “.$level.”, “.$position.” )” Strip & check incoming variables! A delete option would also be nice

21 Add form html, use print for each line Add insert and delete code
Exercise, admin pages Make a new file cms/admin/index.php Add form html, use print for each line Add insert and delete code

22 You can only add things at the end…
Drawbacks You can only add things at the end… Use large numbers for position Recalculate position Last one is better but you’ll need a timestamp Last page that edited and shares a position is the proper one, the stuff below it moves up one position So…

23 Add form html, use print for each line Add insert and delete code
Exercise, admin pages Make a new file cms/admin/index.php Add form html, use print for each line Add insert and delete code Add a new column at the end of your table Name: changed Type: timestamp

24 On submit we add a new page
Drawbacks On submit we add a new page INSERT INTO xyz_website VALUES ( NULL, ‘”.$content.”’, ‘”.$menu_name.”’, “.$level.”, “.$position.”, NOW())” After you insert or delete a record you run UpdatePosition( $row[‘page_id’]; See next page for this function I nicked it from some website and copy/pasted it ever after

25 UpdatePosition function UpdatePosition( $id ){ //set counter
$query = mysql_query( $query ); //recalculate positions $query = "UPDATE xyz_website SET position = ) WHERE id=".$id." ORDER BY position ASC, changed ASC"; }

26 Add form html, use print for each line Add insert and delete code
Exercise, admin pages Make a new file cms/admin/index.php Add form html, use print for each line Add insert and delete code Add a new column at the end of your table Add UpdatePosition()

27 Goals You know what a CMS is You’ll create a website from pages stored in a database You’ll create a form to put pages into a database We’ll use the UPDATE statement

28 Think about your CMS and make a list
Tasks Think about your CMS and make a list What is really missing and needed? What else do you like to add? Are there any bugs or vulnerabilities? Add a few pages to your CMS Make a nice CSS layout Add a banner The two above could be taken from your site of the previous course

29 Done


Download ppt "PHP & MySQL Introduction."

Similar presentations


Ads by Google