Jueves, Marzo 18, 2010
   
Text Size

Google AdSearch

 

Joomla! - Ejemplo de código - JFactory

Ejemplo de escritura y lectura de la base de datos de Joomla! con JFactory::getDBO();

nombre: apellido:

 include('testinclude.php');

En este ejemplo hacemos un include de un fichero php externo a Joomla!, aunque también lo podríamos ejecutar en un wrapper.

En el fichero php, primero mostramos un formulario con nombre y apellido,
y al hacer submit guardamos los datos en la tabla testinclude y luego mostramos los valores guardados.

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__) ));
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

if (isset($_POST['nombre']) && isset($_POST['apellido']))
{


//guardo en la base de datos
    $db =& JFactory::getDBO();
    $nombre=$_POST['nombre'];
    $apellido=$_POST['apellido'];
    $result = $db->Execute("INSERT INTO testcodigo (nombre,apellido) VALUES ('$nombre','$apellido');");
    echo "$result - <a href="/.$PHP_SELF.">Guardado '$nombre','$apellido'</a><br><br>";   

//leo los registros guardados
$db->setQuery("select nombre,apellido from testcodigo");
$results=$db->loadObjectList();
foreach ($results as $result) {
echo $result->nombre." - ".$result->apellido."<br>";
}


}
else
{
?>

Formulario:
<div align="center">
<table align="center">
<tr>
    <form name="form" action="<?php echo $PHP_SELF; ?>" method="post">
    <td>nombre:</td><td> <input type="text" name="nombre"></td>
    <td>apellido:</td><td> <input type="text" name="apellido"></td>
    <td colspan="2"><input type="submit" name="submit" value="enviar"></td>
    </form>
</tr>
</table>
</div>
<?php
}
?>

 

 

[volver al índice de la Guia Joomla!]

 


Compartir este artículo en...
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! TwitThis Joomla Free PHP


Login