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

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

Gallery: Display a list of parent categories only

The below displays a list of all parent categories for a certain Gallery. Be sure to change the gallery_id if yours doesn't have an id of 1.

{exp:query sql="SELECT cat_id, cat_name FROM exp_gallery_categories WHERE gallery_id='1' AND parent_id='0' ORDER BY cat_order"}
<li><a href="{path=gallery/subcat}{cat_id}">{cat_name}</a></li>
{/exp:query}

Change the EE date fields to dropdown pickers

You can use the code below to change Expression Engine's custom date fields to easy dropdowns, instead of the confusing text fields.

JS in the head of your document:
<script language="JavaScript" type="text/javascript">
function entrydate()
{
	var month = document.entryform.start_month.value;
	var day = document.entryform.start_day.value;
	var year = document.entryform.start_year.value;
	var time = document.entryform.start_time.value;
	document.entryform.entry_date.value = year+month+day+" "+time;
}
</script>


Code for the actual fields:

<!-- Begin Month -->
<select name="start_month" onchange="entrydate();">
<option value="01-" >January</option>
<option value="02-" >February</option>
<option value="03-" >March</option>
<option value="04-" >April</option>
<option value="05-" >May</option>

<option value="06-" >June</option>
<option value="07-" >July</option>
<option value="08-" selected>August</option>
<option value="09-" >September</option>
<option value="10-" >October</option>
<option value="11-" >November</option>
<option value="12-" >December</option>

</select>
<!-- End Month -->

<!-- Begin Day -->
<select name="start_day" onchange="entrydate();">
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
<option value="8" >8</option>

<option value="9" >9</option>
<option value="10" >10</option>
<option value="11" >11</option>
<option value="12" >12</option>
<option value="13" >13</option>
<option value="14" >14</option>
<option value="15" >15</option>
<option value="16" >16</option>
<option value="17" >17</option>

<option value="18" >18</option>
<option value="19" >19</option>
<option value="20" >20</option>
<option value="21" >21</option>
<option value="22" >22</option>
<option value="23" >23</option>
<option value="24" >24</option>
<option value="25" >25</option>
<option value="26" >26</option>

<option value="27" >27</option>
<option value="28" >28</option>
<option value="29" >29</option>
<option value="30" selected>30</option>
<option value="31" >31</option>

</select>
<!-- End Day -->
<!-- Begin Year -->
<select name="start_year" onchange="entrydate();">
<option value="2005-" >2005</option>

<option value="2006-" selected>2006</option>
<option value="2007-" >2007</option>

</select>
<!-- End Year -->
<!-- Begin Time -->
<select name="start_time" onchange="entrydate();">
<option value="6:00 AM" >6:00 AM</option>
<option value="6:30 AM" >6:30 AM</option>
<option value="7:00 AM" >7:00 AM</option>
<option value="7:30 AM" >7:30 AM</option>

<option value="8:00 AM" selected>8:00 AM</option>
<option value="8:30 AM" >8:30 AM</option>
<option value="9:00 AM" >9:00 AM</option>
<option value="9:30 AM" >9:30 AM</option>
<option value="10:00 AM" >10:00 AM</option>
<option value="10:30 AM" >10:30 AM</option>
<option value="11:00 AM" >11:00 AM</option>
<option value="11:30 AM" >11:30 AM</option>
<option value="12:00 PM" >12:00 PM</option>

<option value="12:30 PM" >12:30 PM</option>
<option value="1:00 PM" >1:00 PM</option>
<option value="1:30 PM" >1:30 PM</option>
<option value="2:00 PM" >2:00 PM</option>
<option value="2:30 PM" >2:30 PM</option>
<option value="3:00 PM" >3:00 PM</option>
<option value="3:30 PM" >3:30 PM</option>
<option value="4:00 PM" >4:00 PM</option>
<option value="4:30 PM" >4:30 PM</option>

<option value="5:00 PM" >5:00 PM</option>
<option value="5:30 PM" >5:30 PM</option>
<option value="6:00 PM" >6:00 PM</option>
<option value="6:30 PM" >6:30 PM</option>
<option value="7:00 PM" >7:00 PM</option>
<option value="7:30 PM" >7:30 PM</option>
<option value="8:00 PM" >8:00 PM</option>
<option value="8:30 PM" >8:30 PM</option>
<option value="9:00 PM" >9:00 PM</option>

<option value="9:30 PM" >9:30 PM</option>
<option value="10:00 PM" >10:00 PM</option>
<option value="10:30 PM" >10:30 PM</option>
<option value="11:00 PM" >11:00 PM</option>
<option value="11:30 PM" >11:30 PM</option>

</select>
<!-- End Time -->
<input type="hidden" name="entry_date" value="2006-08-30 8:57 AM" />


Full credit for this goes to Solspace!

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>

Restricr Search to a Member's Own Posts

Insert the below code just below the opening search form tags to restrict the search to a member's own posts if they are not a Super Admin.

{if group_id != "1"}<input type="hidden" class="input"  name="member_name" value="{screen_name}" />
{if:else}<p>You are logged in as a Super Admin, so all entries will be visible to you.</p>{/if}

Allow Admins access to member registration page

Open up mod.member_register.php, and look around line 69:

if ($SESS->userdata(’member_id’) != 1)
{
return $OUT->show_user_error(’general’, array($LANG->line(’mbr_you_are_registered’)));
}


Change the 0 to a 1 like shown above. :)

Remove www from URLs.

This is important to implement for your search engine ranking. Google, for example, thinks www.domain.com is a different site from domain.com, and this can seriously impact where you rank in Google. There are other ways of doing this, but the below is the only one that doesn't spit back a 500 error on my server, for some reason.

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

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
« Newer Snippets
Older Snippets »
Showing 1-10 of 55 total  RSS