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-2 of 2 total  RSS 

Date picker PHP + Javacript

// description of your code here

   1  
   2  <?php $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); ?>
   3  
   4  <table>
   5  <tr>
   6  <td valign="top">
   7  
   8  <select id="selMonth" name="selMonth">
   9  <?php for ($i = 0; $i < count($months); $i++) { ?>
  10  <?php   $month = $months[$i]; ?>
  11  <option value="<?php echo ($i + 1); ?>"><?php echo $month; ?></option>
  12  <?php } ?>
  13  </select>
  14  
  15  </td><td valign="top">
  16  
  17  <select name="selDay" id="selDay">
  18  <?php for ($i = 1; $i <= 31; $i++) { ?>
  19  <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
  20  <?php } ?>
  21  </select>
  22  
  23  </td><td valign="top">
  24  
  25  <select name="selYear" id="selYear">
  26  <option value="2006">2006</option>
  27  <option value="2007" selected="selected">2007</option>
  28  <option value="2008">2008</option>
  29  <option value="2009">2009</option>
  30  </select>
  31  
  32  </td><td valign="top">
  33  
  34  <div id="cal1Container"></div>
  35  
  36  </td></tr>
  37  </table>
  38  
  39  <script type="text/javascript">
  40  	YAHOO.namespace("example.calendar");
  41  
  42  	YAHOO.example.calendar.init = function() {
  43  	
  44  		function handleSelect(type,args,obj) {
  45  			var dates = args[0]; 
  46  			var date = dates[0];
  47  			var year = date[0], month = date[1], day = date[2];
  48  
  49  			var selMonth = document.getElementById("selMonth");
  50  			var selDay = document.getElementById("selDay");
  51  			var selYear = document.getElementById("selYear");
  52  			
  53  			selMonth.selectedIndex = month - 1;
  54  			selDay.selectedIndex = day - 1;
  55  
  56  			for (var y=0;y<selYear.options.length;y++) {
  57  				if (selYear.options[y].text == year) {
  58  					selYear.selectedIndex = y;
  59  					break;
  60  				}
  61  			}
  62  		}
  63  
  64  		function updateCal() {
  65  			var selMonth = document.getElementById("selMonth");
  66  			var selDay = document.getElementById("selDay");
  67  			var selYear = document.getElementById("selYear");
  68  			
  69  			var month = parseInt(selMonth.options[selMonth.selectedIndex].value);
  70  			var day = parseInt(selDay.options[selDay.selectedIndex].value);
  71  			var year = parseInt(selYear.options[selYear.selectedIndex].value);
  72  			
  73  			if (! isNaN(month) && ! isNaN(day) && ! isNaN(year)) {
  74  				var date = month + "/" + day + "/" + year;
  75  
  76  				YAHOO.example.calendar.cal1.select(date);
  77  				YAHOO.example.calendar.cal1.cfg.setProperty("pagedate", month + "/" + year);
  78  				YAHOO.example.calendar.cal1.render();
  79  			}
  80  		}
  81  
  82  		YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1Container", 
  83  																	{ mindate:"1/1/2006",
  84  																	  maxdate:"12/31/2008" });
  85  		YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal1, true);
  86  		YAHOO.example.calendar.cal1.render();
  87  
  88  		YAHOO.util.Event.addListener(["selMonth","selDay","selYear"], "change", updateCal);
  89  	}
  90  
  91  	YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);
  92  	
  93  	// Initialize pulldowns
  94  	var myDate = new Date();
  95  	var month = myDate.getMonth();
  96  	var day = myDate.getDate();
  97  	var year = myDate.getFullYear();
  98  	var selMonth = document.getElementById("selMonth");
  99  	var selDay = document.getElementById("selDay");
 100  	var selYear = document.getElementById("selYear");
 101  	selMonth.selectedIndex = month;
 102  	selDay.selectedIndex = day - 1;
 103  	selYear.selectedIndex = year - 2006;
 104  	
 105  </script>
 106  
 107  <div style="clear:both" ></div>

Color picker

I saw someone write a fileselect dialog a few days ago.
I think we should have more dialog type to use in our apps.
So, I write a color picker code below.
It can be converted easily into a dialog component.

   1  
   2  // Usual code as always
   3  import e32
   4  from appuifw import *
   5  from key_codes import *
   6  
   7  class Keyboard(object):
   8      def __init__(self):
   9          self.state = {}  # is this key pressing ?
  10          self.buffer= {}  # is it waiting to be processed ?
  11      def handle_event(self, event): # for event_callback
  12          code = event['scancode']
  13          if event['type'] == EEventKeyDown:
  14              self.buffer[code]= 1   # put into queue
  15              self.state[code] = 1
  16          elif event['type'] == EEventKeyUp:
  17              self.state[code] = 0
  18      def pressing(self, code):      # just check
  19          return self.state.get(code,0)
  20      def pressed(self, code):       # check and process the event
  21          if self.buffer.get(code,0):
  22              self.buffer[code] = 0  # take out of queue
  23              return 1
  24          return 0
  25  
  26  key = Keyboard()
  27  app.body = canvas = Canvas(event_callback=key.handle_event)
  28  
  29  def quit():
  30      global running
  31      running = 0
  32  
  33  app.exit_key_handler = quit
  34  running = 1

At first, I wanted to use the standard palette of s60.
But later decide to just use 216 web-safe colors.
The code is simpler, so the concept is easier to understand.
   1  
   2  ff00 = range(0xff, -1, -0x33)
   3  pal = [(r,g,b) for r in ff00 for g in ff00 for b in ff00]  # web-safe
   4  map_j = range(0,12,2)+range(11,0,-2)  # make better grouping
   5  for j in range(12):
   6      for i in range(18):
   7          k = 18*map_j[j] + i
   8          canvas.rectangle([(9*i+1, 9*j+1), (9*i+9, 9*j+9)], None, pal[k])
   9  
  10  def clear_box(color=0xffffff):
  11      global x,y
  12      canvas.rectangle([(9*x, 9*y), (9*x+10, 9*y+10)], color)  # cursor
  13  
  14  x, y = 0, 0
  15  black_white = 0
  16  while running:
  17      if key.pressed(EScancodeLeftArrow):
  18          clear_box()
  19          if x > 0: x -= 1
  20      if key.pressed(EScancodeRightArrow):
  21          clear_box()
  22          if x < 17: x += 1
  23      if key.pressed(EScancodeUpArrow):
  24          clear_box()
  25          if y > 0: y -= 1
  26      if key.pressed(EScancodeDownArrow):
  27          clear_box()
  28          if y < 11: y += 1
  29      if key.pressed(EScancodeSelect):
  30          color = pal[18*map_j[y] + x]
  31          canvas.rectangle([(1,109), (17*9+9,130)], None, color)  # show color
  32      black_white ^= 0xffffff  # toggle
  33      clear_box(black_white)
  34      e32.ao_sleep(0.2)

See Screenshot.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS