Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sasha http://nothing-less.net

« Newer Snippets
Older Snippets »
Showing 1-10 of 16 total  RSS 

Run a simple Update Query

In PHP.

$result = mysql_query("UPDATE example SET age='22' WHERE age='21'") 
or die(mysql_error());  

Generate a dropdown with months in a year

The below code will generate a form dropdown for all 12 months, with the current month selected. Handy for those date pickers!

<select name="start_month">
<?php
global $LOC;
$current_time_m = $LOC->decode_date('%m', $LOC->now);

for ($i = 1; $i <= 12; $i++) {
   echo "<option value='$i-'";
if ($i == $current_time_m) { echo " selected='selected'"; }
$month_text = date("F", mktime(0, 0, 0, $i+1, 0, 0, 0));
   echo ">$month_text</option>
"; } ?>
</select>

Generate a dropdown with days of the month

The below code will generate a form dropdown for all the days of the month, with today's day selected. Handy for those date pickers!

<select name="start_day">
<?php
global $LOC;
$current_time_day = $LOC->decode_date('%d', $LOC->now);

for ($i = 1; $i <= 31; $i++) {
   echo "<option value='$i'";
if ($i == $current_time_day) { echo " selected='selected'"; }
   echo ">$i</option>
";
} ?>
</select>

Loop in PHP Using For (Descending)

For those times you need to do a loop in PHP, but have the outcome be in descending order (ie, start at 10 and end at 1).

$totalcode="10";
for($i=$totalcode; $i>0; $i--){

 echo"$i";
}

Loop in PHP Using For (Ascending)

If you have a set number of times you need to repeat a piece of coding, using the for loop is the easiest way to accomplish this.

$i is the starting number, 1 in this case. This loop will repeat itself until $i reaches 10 - you can change this to any desired number. The bit inbetween the { and } is simply what is repeated - replace this with anything you want. :)

for ($i = 1; $i <= 10; $i++) {
   echo $i;
}

Pad Numbers in PHP

Sometimes I need numbers to always be two digits, even if they're less than 10. The below snippet will format all numbers to be two digits, inserting a 0 if they're under 10 - creating, for example, 09 instead of 9.

$raw = 1;
$formatted = sprintf("%02d", $raw);
echo $formatted;

//outputs 01

Random Number

Replace 1 and 10 with the range you want to pick a number between. This example will pick a random number between 1 and 10.

$randomnumber= rand(1,10);

Display Current Year

Useful for example for automatic copyright notices:

&copy; Copyright 2004 - <?php echo date("Y") ?>

Date In The Future

How to display a date in the future - in this case, next month.

<?php
 $nextmonth = mktime(0, 0, 0, date("m")+1, 1, date("Y"));
 echo date("F", $nextmonth);
 ?>

Display In 2-Column Table

The below can be applied to MT, EE, etc, and will display the content in a 2 column table. The EE tags below are just an example, it will work equally well if you replace it with MT, Wordpress, etc, tags.

<? $set_table="0"; ?>

<table cellpadding="5" border="0">
{exp:gallery:categories gallery="{gallery_name}"}
<? 
$set_table = $set_table +1;
if ($set_table == "1") {echo"<tr>";} ?>


Insert other tags here.


<? if ($set_table == "2") {echo"</tr>";  $set_table="0";} ?>
{/exp:gallery:categories}
</table>
« Newer Snippets
Older Snippets »
Showing 1-10 of 16 total  RSS