PHP for HTML Gurus - J and Beyond 2012

Post on 19-Jun-2015

8.227 views 0 download

Tags:

description

If you are tired of copy/pasting bits of PHP in your layouts by trial and error, here is your chance to learn what those bits of PHP mean. This talk explains the PHP code found in layouts and templates: what it does, how you can change it, and what to watch out for. Intended for those who know HTML but don’t know what all those dollar signs and arrows are doing. And if you are a code guru, it's your chance to learn about layouts. We’ll start by briefly going over the basics of PHP, then move on to looking at the actual code in various layouts to illustrate what is being done. We’ll end by making changes and additions to the coding and seeing how that changes what you see on the sceen. If you want to learn more about PHP, see the book "PHP and MySQL, 24-Hour Trainer".

Transcript of PHP for HTML Gurus - J and Beyond 2012

PHP for HTML GurusAndrea TarrTarr ConsultingJ and Beyond 2012

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Outline• PHP Basics• Explanation of

actual code in templates & layouts• Changing code

and seeing it in action• Book give away

2

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

PHP Basics• Get in and out of PHP with <?php ?>• End each statement with a semi-colon<?php

$first_name = 'Andrea';$last_name = 'Tarr';

?>

• Whitespace is irrelevant<?php $first_name='Andrea';$last_name='Tarr';?>

• Omit any final ?> at the end of a file3

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Comments• Single line comments// This is a single line comment$language = 'PHP'; // End of line comment// Multiple lines of single comments// can be done by repeating the slashes

• Multiple line comments/* With this type of comment you canstart a comment on one line and endit on an other line */

4

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Variables• Used to store information that can change• Start with a $• Assign a value with =• Use single ('') or double ("") quotes for text• You need to assign a value before you can use the

variable• Use echo to display a value• Use a dot (.) to join items together

$first_name = 'Andrea';$last_name = 'Tarr';echo $first_name . ' ' . $last_name;

5

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

$Variable Types• String Text: Andrea• Numbers: 42• Logical: True/False, 1/0• Array: An array is a list, either indexed or named• [0]=>Sun, [1]=>Mon, [2]=>Tue, [3]=>Wed• Name=>Joomla, Version=>2.5, • You can nest arrays

• Object: We'll come back to Object shortly

6

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Functions()• Functions perform an action• You recognize a function by the ()• You can pass information to the function in the ()$full_name = ' Andrea Tarr ';echo trim($full_name);

• This results in Andrea Tarr

• You can pass a function to a functionecho strtoupper(trim($full_name);

• This results in ANDREA TARR7

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

CONSTANTS• Constants are assigned once and don't change• No $• Usually in ALL CAPSdefine('SITENAME', 'My Site');echo SITENAME;

8

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Objects• Think of an object as a named array plus functions• Properties are variables• Functions are also called methods

Examples:• $item->title contains the title• $item->introtext contains the intro text• $params->get('show_modify_date')

9

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Classes• Classes are the blueprints that are used to create

objects• You can use the classes directly without creating

an object

Examples:• JText::_('COM_CONTENT_READ_MORE');• JHtml::_('string.truncate', ($this->item->title),

$params->get('readmore_limit'))

10

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Making Decisions: if/else• Example 1if ($sum > 10) :

// do something when greater than 10else :

// do something when 10 or lessendif;

• Example 2if ($errors) :

// do something when there are errorsendif;

• You can also nest if statements

11

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Comparison Operators

12Don't use = to compare!

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

One Line If Statement• Normal wayif ($gender == 'M') :

echo 'Man';else :

echo 'Woman';endif;

• One line versionecho ($gender == 'M') ? 'Man' : 'Woman';

13

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Alternative Syntax• Alternative Syntax used when mixing with HTMLif ($sum > 10) :

// do something when greater than 10else :

// do something when 10 or lessendif;

• Normal syntax with curly bracesif ($sum > 10) {

// do something when greater than 10} else {

// do something when 10 or less}

14

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Looping• While• Repeats while a condition is true

• Do/While• Does it at least once then repeats while true

• For• Loops a given number of times

• Foreach• Repeats the code for each element in an array or

object

• Jumping out early• Continue

• Jump to the next iteration

• Break• Jump out of the loop

15

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Template index.php file• Template Parameters• Conditional Stylesheets• Conditional Positions• One Line If Statement

16

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

$color = $this->params->get('templatecolor');

<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/<?php echo htmlspecialchars($color); ?>.css" type="text/css" />

<link rel="stylesheet" href="/localhost/jc/templates/beez_20/css/personal.css" type="text/css" />

Template Parameters

17

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Conditional Stylesheets<?php if ($this->direction == 'rtl') : ?> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/template_rtl.css" type="text/css" /><?php endif; ?>

18

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Conditional Positions<?php if ($this->countModules('position-12')): ?><div id="top"> <jdoc:include type="modules" name="position-12" /></div><?php endif; ?>

19

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

$showRightColumn = ($this->countModules('position-3') OR$this->countModules('position-6') OR$this->countModules('position-8'));

<div id="<?php echo $showRightColumn ? 'contentarea2' : 'contentarea';?>">

If there are right modules: <div id="contentarea2">If there aren't: <div id="contentarea">

One Line If Statement

20

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Module Layout • Latest Articles• mod_articles_latest/tmpl/default.php

21

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Latest Articles

22

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Latest News default.php

23

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

var_dump()

24

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item

25

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item

26

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 2

27

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 3

28

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 4

29

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 5

30

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Object $item – Page 6

31

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Add information

32

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

List with intro text

33

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Questions?

34

PH

P f

or

HTM

L G

uru

s • J a

nd

Beyond 2

01

2 •

Andre

a T

arr

Book Give Away

35