sql_create_view

The sql_create_view() function creates a view for the selection query provided. The view can then be used by SPIP loops or by other selection commands.

It accepts 4 parameters:

  1. $nom is the name of the view created,
  2. $select_query is the selection query,
  3. $serveur,
  4. $option.

It can be used in conjunction with the sql_get_select function to retrieve the desired selection:

$selection = sql_get_select('column', 'table');
sql_create_view('myview', $selection);
// utilisation
$result = sql_select('column', 'myview');

Note: Whenever a selection column uses the 'name.column' notation, you absolutely must declare an alias for the column, otherwise certain database ports (SQLite in particular) will not create the expected view, e.g. 'name.column AS column'.

Example

This small example demonstrates this function by creating a (rather useless) table from 2 columns in a section:

$select = sql_get_select(array(
        'r.titre AS t',
        'r.id_rubrique AS id'
    ), array(
        'spip_rubriques AS r'
    ));
// create the view
sql_create_view('spip_short_rub', $select);
// use it:
$titre = sql_getfetsel('t', 'spip_short_rub', 'id=8');

The view could also be used within a SPIP template file, as in:

<BOUCLE_view(spip_short_rub) {id=8}>
    <h3>#T</h3>
</BOUCLE_view>

Author Mark Baber Published : Updated : 02/06/10

Translations : English, français