Wednesday, December 30

The File You're Looking for is...

/templates/system/html/modules.php

This is where you can change the module titles from h3 to h2. Also from tables to not tables. I tried to copy this like normal to the template/html folder, but I get some error that makes me angry, so just forget about that.

I changed all the tables to divs too cause tables make me sad. I think pretty much the one that my site uses is like this:

function modChrome_xhtml($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="moduletable->get('moduleclass_sfx'); ?>">
<?php if ($module->showtitle != 0) : ?>
<h2><?php echo $module->title; ?></h2>
<?php endif; ?>
<?php echo $module->content; ?>
</div>
}
<?php endif;
}

Wednesday, September 9

Literal... Interpreted... Quotes Matter

Something I never before (but probably should have) read that took hours to figure out:

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters. -via php.net
This really matters!!

For example. When you are sending mail, new lines are usually \r\n. If you have your message body in single quotes (') the \r\n will show up in the message!

Even more importantly. When using Pear mime, you have to define eol. Just remember $crlf = '\r\n' WILL NOT WORK!!! it has to be $crlf = "\r\n" If I had read to the bottom of the user comments on the pear page, it would have saved me a lot of time.

And yes, this post is actually about doing Mail_Mime with Pear :D

Resources:
PEAR Mail_Mime
PHP Maniac Examples

Tuesday, September 8

getdate() Without Time

I am rewriting the front end of a site, but can't change the back end or functionality. And so I must learn how to use an MSSql db via php. My first impression... they are not friends. Okay, moving on.

Problem:
Table has a datetime field, but the backend freaks out because of some 10yr old stored procedure. We traced it down to that the datetime can't contain the time.

Solution:
Now, I could have just used php date, but the day I found out that databases can insert their own date, I swore I'd never use it again for a current timestamp. So instead, my insert statement has this completely inelegent line:

convert(varchar(8), getdate(), 112)
That inserts the date with no time. Or more specifically, the time is 12:00AM. The actual contents is something like Sep 08 2009 12:00AM.

Wednesday, September 2

Buttons Change on Hover

First time I've done this (I think). Short sweet code.

<a href="link"><img src="images/button.jpg"
onmouseover="this.src='images/buttonhover.jpg';"
onmouseout="this.src='images/button.jpg';" /></a>
It would not surprise me if there were instances where this fails. But it's also pretty nice because you don't have to worry about a no js version; it just doesn't do anything without js.

If you're using large images, you might want to preload them.

Tuesday, July 14

Remove Extension PHP

I'm using a Joomla module called JX List Files. Simple, nice little module to list files in a directory. My only complaint was that it left the file extension in the name, so I added this little function I found.

function remext($strName){
$ext = strchr($strName, '.');
if($ext !== false){
$strName = substr($strName, 0, -strlen($ext));
}
return $strName;
}

Friday, July 10

Show/Hide with Select

This isn't complicated, but I forgot how I did it before, so I'm going to write it down this time.

The Case:
I have a form with a select box, if you select Yes, you get one set of options, and No gets another. The two pieces are in divs, both with style=display:none. The dropdown is a required field and it starts as a blank box.

The JavaScript:

function showHide(show, hide){
document.getElementById(show).style.display='block';
document.getElementById(hide).style.display='none';
}
The HTML:
<select id="yesno">
<option> </option>
<option value="yes" onclick="showHide('pickYes', 'pickNo')">Yes</option>
<option value="no" onclick="showHide('pickNo', 'pickYes')">No</option>
</select>
<div id="pickYes" style="display:none">
I display if Yes is selected
</div>
<div id="pickNo" style="display:none">
I display if No is selected
</div>


:UPDATE:
Oh man, I totally lied. That only works in Firefox. onclick isn't an option javascript event. Oops. Here's the real solution

Javascript:
function showHide(){
var sel = document.getElementById('yesno').value;
if (sel == 'yes'){
document.getElementById('pickYes').style.display='block';
document.getElementById('pickNo').style.display='none';
}
else if (sel == 'no'){
document.getElementById('pickNo').style.display='block';
document.getElementById('pickYes').style.display='none';
}else{
document.getElementById('pickYes').style.display='none';
document.getElementById('pickNo').style.display='none';
}
}
HTML:
<select id="yesno" onchange="showHide()">
<option> </option>
<option value="yes" onclick="showHide('pickYes', 'pickNo')">Yes</option>
<option value="no" onclick="showHide('pickNo', 'pickYes')">No</option>
</select>

So yeah... this isn't as pretty, but it works in other browsers.

ANOTHER UPDATE (from months in the future):
This whole ugliness can be completely avoided by just using JQuery :)

Wednesday, July 1

Random CSS Background

The Case:
We have a header background image on the top of the site. The customer wants it to rotate randomly between any number of images.

The Javascript:

var images=new Array();
images[0] = "header1.jpg";
images[1] = "header2.jpg";
images[2] = "header3.jpg";
images[3] = "header4.jpg";
images[4] = "header5.jpg";
var path = "http://example.com/images/";

function changeHeader(){
var rn=Math.floor(Math.random()*images.length);
document.getElementById('header').style.background = "url('"+path+images[rn]+"')";
}
And the HTML:
<script type="text/javascript">
window.onload=function(){
changeHeader();
setInterval("changeHeader()",5000);
}</script>
Don't forget to set a default image for those who don't use javascript.