Archive for April, 2009

Change the css class of every second row with php

Tuesday, April 28th, 2009

If you need to find odd numbers in an array in php it’s very easy. You just use the “%” command. eg.

if ($num % 2) {
echo “$num is odd”;
} else {
echo “$num is even”;
}

I think what it basically does is divides the number by 2 and if it’s not a whole number it renders it returns it as odd.

Here’s how this code can be very tidy and useful:

The following code can be helpful if you are doing a php foreach repeater to produce a table and you want to use an alternating css class on every second row.

Step 1:
Place a $count outside your foreach loop.

$count=”0″;

Step 2:
Create your rows with the alternating css classes

echo “<tr class =’”;
$count++;
if ($count % 2) {
echo “rowclass1″; }
else {
echo “rowclass2″;
}
echo “‘>”;
echo “<td>Your row Content</td></tr>”;

That’s it, each row will have a different css class and it saves you having to do each row manually!

Table Rows ALternating

CSS fix to set a style for a specific input type

Tuesday, April 28th, 2009

You can set a css style on a specific input type without having to worry about classes etc. with this handy and not often used css fix.

input[type=radio] {

border: none; background:transparent

}

Have tested it and it works in IE7, IE8, Firefox 3.0 etc.
I haven’t tried IE6, but apparently this fix doesn’t work.

But I think these days people who are still using IE6 are fairly used to seeing pages rendered incredibly ugly, so it doesn’t really matter. :)

Forcing a scroll bar on your browser with css.

Monday, April 27th, 2009

Often when you have some long pages and some short your page may jump slightly horizontally which can be qutie annoying.
This can be caused by the scroll bar sometimes being there and sometimes not, and this will vary depending on the size of the monitor.

One way to get around this is to force a scroll bar on every page. This will make sure the page wont jump no matter the size of the monitor.

The fix for this is very simple. Just add the following to your css file:

body {

overflow-y:scroll;

{

php drop down box list of countries made by foreach array

Sunday, April 19th, 2009

This is some code I wrote to generate a drop down box with all the countries listed in it,  that will preselect the right country if you post the country to it from a previous page. Really helpful if you use it as a control. I have it permanently in my PHP includes folder and just call it any time I need it.eg.:

 <select name=”example”>

<? include ‘../inc/countries.php’ ?>

</select>

Here is countries.php

(more…)

mysql table column named ‘default’ not working!

Sunday, April 19th, 2009

I was having a hard time figuring out what the problem with my mysql query was, and so this might help someone.I had a column named in my table called ‘default’ which was just a tinyint (or boolean) 1 or 0 so that I could indicate if the row was meant to be the default row for a page. However my query that said:

“select * from table where default=1″   

Wasn’t working at all.It was giving me the same unhelpful error “you have an error in your SQL syntax. Check the manual blablabla”.Then I rememberd that the word “default” in mysql syntax is used to specify what you want the default value to be for a column when you are setting up a new one.

So if you use it as a column name it wont work and will go mental.I then changed it to be “isdefault” and it worked fine.It soon came to my attention that it’s a really dumb idea to label your column names anything that might conflict with some mysql query code.

In the same way it wouldn’t make sense to name a column “where” or “select” or “update” or any other mysql syntaxy word.But you probably aren’t idiots and already know this…. 

Round your values in PHP using Math Functions (using number_format)

Sunday, April 19th, 2009

To round any number or integer values in PHP it’s simple.number_format is useful function that you can use. It takes 2 values: the $NumberYouWantToFormat and then the number of decimal places you want it to be formatted to. 

$total = 34.128252 echo number_format($total, 2)

This will produce:

34.13 

It’s a simple thing, but really useful.