Startnet - Web Design in Wimbledon

Startnet - Web Design in Wimbledon

Joomla, Magento and MySQL Coding Tips

Here are some Joomla and MySQL Coding Tips and Tricks. You may find these useful for a variety of common tasks when programming Joomla templates and modules / components.

 


Joomla Base URL in index.php


echo $this->baseurl ?>/includes/js/captify.tiny.js"
<img src="/images/stories/spinach_logo.png">


Joomla default menu (homepage), view, option variables in index.php

// if a section set background body type for background image
$menu = &JSite::getMenu();

// echo the menu item name
echo $menu->getActive()->name;
echo $menu->getActive()->id; // id number


if ( JRequest::getCmd('view') === "section" ) {
$db = &JFactory::getDBO();
$temp   = JRequest::getString('id');
$temp   = explode(':', $temp);           
$id   = $temp[0];

$db->setQuery('SELECT title FROM #__sections WHERE id='.$id);  
$sname=$db->loadResult();
$sname=str_replace(" ","_",$sname);
$sname=str_replace("'","",$sname);
echo "_".strtolower($sname);
}

elseif ( ($menu->getActive() == $menu->getDefault()) || ( JRequest::getCmd('view') === "frontpage" ) ) {
echo "_homepage";
}



Joomla countModules function

<?php if ($this->countModules( 'user1' )) : ?>

<div id="imageFlick">
<jdoc:include type="modules" name="slider_image" style="xhtml" />
</div>


Joomla index.php PHP copyright

echo "© Startnet Ltd ".date("Y");


<jdoc:include type="modules" name="footer_menu" style="xhtml" />


Joomla index.php component (main text)

<jdoc:include type="component" />


Joomla error reporting FULL in configuration.php

var $error_reporting = '-1'; // NONE

var $error_reporting = '2048'; // FULL


Joomla component/module XML parameter element types eg. calendar, textfield, categories path

/libraries/joomla/html/parameter/element

http://docs.joomla.org/Tutorial:Template_parameters


Joomla MYSQL loadResult

global $database;
 
$sql = 'SELECT title FROM #__categories';
$database->setQuery( $sql );
echo $database->loadResult();
$sql = "SELECT * FROM #__categories WHERE title=$title";
$database->setQuery( $sql );
$category = $database->loadRow();
print_r( $category );


$sql = 'SELECT * FROM #__users';
$database->setQuery( $sql );
$user = NULL;
$database->loadObject( $user );
echo "Name: $user->name\n";
echo "Username: $user->username\n";

$sql = 'SELECT * FROM #__categories';
$database->setQuery( $sql );
$rows = $database->loadObjectList();
foreach ( $rows as $row ) {
echo "$row->title: $row->description\n";
}


LINK TO MORE DATABASE FUNCTIONS

PHP mysql_connect for SELECT

<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM jos_menu");

while($row = mysql_fetch_array($result))
{
//  echo $row['id'] . " " . $row['menutype']. " " . $row['name'];
//  echo "<br />";

print "<pre>";

print_r($row);

print "</pre>";

}



mysql_close($con);
?>


PHP mysql_connect for UPDATE

mysql_select_db("database", $con);

$result = mysql_query("ALTER TABLE jos_acajoom_subscribers ADD interested_in varchar(200); ");

if (!$result) {
die('Invalid query: ' . mysql_error());
}

mysql_close($con)


Illegal characters in MYSQLdump solution:

use php or command line to export mysql database as latin1 :
<?php
exec("mysqldump --default-character-set=latin1 -uUSERNAME -pPASSWORD DATABASE > DATABASE.sql");
?>

open in editor and Find/Replace latin1 to utf8

then import using web interface or command line.

 


Fix the Magento Database import error : Can't create table 'api_session.frm' (errno: 150).

the problem is caused by having ENGINE=InnoDB in lines for foreign keys.

edit the file and do a find/replace for "ENGINE=InnoDB" to remove them.

Make sure to drop / create the database, so you start afresh and re-import the data. Should all work then.

 


Magento admin page xxx.com/index.php/admin gives 404 error.

SET FOREIGN_KEY_CHECKS=0;
UPDATE `core_store` SET store_id = 0 WHERE code='admin';
UPDATE `core_store_group` SET group_id = 0 WHERE name='Default';
UPDATE `core_website` SET website_id = 0 WHERE code='admin';
UPDATE `customer_group` SET customer_group_id = 0 WHERE customer_group_code='NOT LOGGED IN';
SET FOREIGN_KEY_CHECKS=1;

 


Magento - display product attributes in category list page

Say you've added 'brand' as an attribute and added it to the Default attribute set (make sure the attribute is visible in the frontend). Then add this:

From http://www.magentocommerce.com/boards/viewthread/19215/

<?php
echo $this->htmlEscape($_product->getData('brand'));        
?>