Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Building Forums With vBulletin (2006).pdf
Скачиваний:
40
Добавлен:
17.08.2013
Размер:
6.3 Mб
Скачать

Chapter 9

This SQL query looks through the posts for instances of Vbulletin and replaces them with vBulletin. The WHERE clause is used to optimize the find and replace as this allows MySQL to take advantages of indexes, which speeds things up.

If you get an error running this query then check to see if you are using a table prefix in the database for the tables (such as vb_) and add this to the query. This would change the query to the following:

UPDATE vb_post SET pagetext = REPLACE (pagetext,'Vbulletin','vBulletin') WHERE pagetext LIKE '% Vbulletin%';

If you wanted to carry out a similar find and replace within PMs, you'd use the following query:

UPDATE pmtext SET message = REPLACE (message,'Vbulletin','vBulletin') WHERE message LIKE '%Vbulletin%';

By using MySQL queries you are taking total control over the data held in the database. We don't have the time or scope here to cover MySQL queries in detail, so for more information visit http://dev.mysql.com. Alternatively, you can read Mastering phpMyAdmin for Effective MySQL Management by Marc Delisle, published by Packt Publishing (ISBN 1-904811-03-5).

Programming Standards

Good programming is clear, unambiguous programming. Let's run through a few tips to help make your code more readable, easier to debug, and easier to make changes to later on.

Braces

For clarity each brace should be placed on its own line in the code:

if ($condition)

{

// code goes here

}

Don't be tempted to condense code like this:

if ($condition)

{// code goes here }

Indenting

Indent code between braces:

if ($condition)

{

// code goes here

}

Code between braces within braces should have deeper indenting:

if ($condition)

{

if ($condition2)

{

// code goes here

}

// some more code goes here

}

201

Programmer's Reference

Give Operators Space

All operators (except -- and ++) should have a space either side.

$a = $b + $c;

String Quoting

All strings should be quoted with single quotes when they don't contain variables or control characters. Otherwise always use double quotes:

$a = 'Hello, World!'; $b = "Hello,\nWorld!"; $c = "$hello,\nWorld!";

Return Values

Use only lower-case true and false for return values. Upper-case should be reserved for custom constants.

if ($condition)

{

return true;

}

else

{

return false;

}

AND and OR

Always use AND rather than and or &&, and OR rather than or or || in your code.

if ($num1 AND $num2 OR $num3)

AS

Similarly, AS in foreach statements should be capitalized.

foreach ($array AS $num => $var)

{

// code goes here

}

SQL Queries

Under all circumstances, make sure that you double quote all SQL queries.

$DB_site->query("SELECT field FROM " . TABLE_PREFIX . "table ORDER BY field");

It may be better to write long queries on more than one line.

202

Chapter 9

Naming Conventions for Functions

Custom names for functions you add should all adhere to the following naming conventions:

Prefix

Description

 

 

build_

Save data back to the database.

cache_

Read data from the database and create a temporary PHP cache variables to reduce

 

SQL database load.

can_

Return true or false based on permissions.

construct_

Return variables containing HTML.

convert_

Convert the data format of input variables.

delete_

Delete data from the database.

exec_

Perform an action.

fetch_

Return arrays, strings, integers, etc.

file_

Deal directly with the file system.

handle_

Called by the bbcode parser to deal with a specific bbcode type.

import_

Take an array of data and import it into the database.

is_, contains_

Return true or false based on conditions.

js_

JavaScript functions defined within the PHP code.

log_

Append to the vBulletin logs.

parse_

Initialize the bbcode parsing process.

print_

Print code out to browser or buffer.

process_

Prepare an array for later reference.

sanitize_

Check and clear data (such as removing illegal characters) for later processing.

sort_

Sort data.

strip_

Strip elements from strings.

undelete_

Functions that undo a soft deletion.

vb_, vb

Replace built-in vBulletin PHP functions with replacements designed to increase

 

functionality or alter the behavior of the built-in function.

verify_

Check conditions and generate an error message if particular conditions are satisfied.

xml_

Read or output XML.

 

 

203