RE: [PHP] populate form input option dropdown box from existing data

2009-06-16 Thread Dajve Green

 -Original Message-
 From: PJ [mailto:af.gour...@videotron.ca]
 Sent: 15 June 2009 23:10
 To: php-general@lists.php.net
 Subject: [PHP] populate form input option dropdown box from existing data
 
 I am having difficulties figuring out how enter retrieved data into a
 dropdown box for editing. Here's a snippet:
 ...snip
 select name=categoriesIN[] multiple size=8
 option value=1Civilization/option
 option value=2Monuments, Temples amp; Tombs/option
 option value=3Pharaohs and Queens/option... snip
 
 As I understand it, I need to put an array ( $categoriesIN[] ) somewhere
 in the above code... or do I need to insert a series of value selected
 fields for the values?
 The closest thing to what I need, seems to be in this post:
 http://www.hpfreaks.com/forums/index.php?topic=165215
 But it doesn't appear too inviting... not sure if they ever got it to
 work...
 

Hi,

Something like this should work for you

?php
  // Set up initial values
  // This could be hard-coded or brought in from DB or other source
  $categoriesIN_options = array(1 = 'Civilzation',
  2 = 'Monuments, Temples amp;
Tombs',
  3 = 'Pharaohs and Queens',
  );
  // Create a holder for values posted if you want 
  // to repopulate the form on submission
  $selected_clean   = array();
  // Check for passed values and validate
  if (array_key_exists('categoriesIN',$_REQUEST)) {
// Prevents errors with the foreach 
$passed_categoriesIN = (array)$_POST['categoriesIN'];
foreach ($passed_categoriesIN as $options_key) {
  // If the value isn't in our pre-defined array, handle the error
  if (!array_key_exists($options_key,$categoriesIN_options)) {
// Handle error
continue;
  } 
  $selected_clean[] = $options_key;
}
  }
?
[snip]
select name='categoriesIN' multiple size='8'
  ?php // Loop over the options set above
foreach ($categoriesIN_options as $key = $value) {
  echo option value='{$key}';
  // Repopulate with selected
  if (in_array($key,$selected_clean)) {
echo  selected='selected';
  }
  echo {$value}/option\n;
}
  ?
/select


--

for (thoughts, ramblings  doodles) {
  goto http://dajve.co.uk ;
}


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] difference between two times? Date_diff and DateTime::diff

2009-06-16 Thread Dajve Green

Hi Matthew,

A quick note on the DateTime::diff() method - it's only available as from
PHP 5.3, which is currently in release candidate stage, meaning unless you
have your own server running PHP, it won't be available to use (hopefully -
I would be sceptical of any webhost which rolls out RCs on production
servers).

If you need to know what version of PHP you're running, use:
phpversion() or phpinfo()

 -Original Message-
 From: Matthew Croud [mailto:m...@obviousdigital.com]
 Sent: 16 June 2009 12:42
 To: Tom Chubb
 Cc: PHP General list
 Subject: Re: [PHP] difference between two times? Date_diff and
 DateTime::diff
 
 Hi Tom,
 
 Thanks for the reply,  I believe I have a fair understanding of
 functions, and I have followed the example on the PHP manual page (
 http://uk3.php.net/manual/en/function.date-diff.php
   ), ideally I want to know how to use the class DateTime::diff, how
 can I use the DateTime::diff to get the difference between two times/
 dates ? I suppose then I'm after the syntax
 
 would it be like this for example:
 $DIfferenceInTime  = DateTime::diff(10:00,12:32);
 
 Thanks again for helping me out.
 
 
 
 On 16 Jun 2009, at 12:33, Tom Chubb wrote:
 
  Matt,
  Do you understand how to use functions?
  A function is defined like this:
 
  function () {
  //code goes here
  }
 
  You can pass arguments to be used in a function like this:
 
  function($arg1, $arg2) {
  //code goes here
  }
 
  In the first example on the following page:
 http://uk3.php.net/manual/en/function.date-diff.php
  To call the function you need to provide two arguments: $dtTime1 
  $dtTime2
 
  To use in a script, you need to first define the function, as per
  the example:
 
  ?php
 
  function GetDeltaTime($dtTime1, $dtTime2)
  {
$nUXDate1 = strtotime($dtTime1-format(Y-m-d H:i:s));
$nUXDate2 = strtotime($dtTime2-format(Y-m-d H:i:s));
 
$nUXDelta = $nUXDate1 - $nUXDate2;
$strDeltaTime =  . $nUXDelta/60/60; // sec - hour
 
$nPos = strpos($strDeltaTime, .);
if (nPos !== false)
  $strDeltaTime = substr($strDeltaTime, 0, $nPos + 3);
 
return $strDeltaTime;
  }
 
  ?
 
  Then you need to call the function like this:
 
  ?php
  GetDeltaTime(time1-goes-here, time2-goes-here)
  ?
 
  And it should spit out the difference.
 
  Code is untested and if you didn't follow that I suggest you read up
  on functions: http://www.w3schools.com/php/php_functions.asp
 
  Hope this helps - I'm probably in a similar situation to you and
  have been dabbling with PHP for a few years just as a hobby but
  thought I'd try and help out.
  You'll learn a lot from reading this list as well.
 
  Cheers and good luck,
 
  Tom
 
 
  2009/6/16 Matthew Croud m...@obviousdigital.com
 
  Hello,
 
  My journey of learning PHP is going well, so i've decided to make a
  small program which works out the difference between two times and
  records the data in a text file.
 
  I've searched the PHP manual for functions which can help me out,
  and I discovered the function Date_diff (
 http://uk3.php.net/manual/en/function.date-diff.php
   )and the class DateTime::diff (
 http://uk3.php.net/manual/en/datetime.diff.php
   )
 
  My question is, how on earth do I use these functions ? I really
  don't understand the manual documentation.
 
  I've just moved onto the subject of classes and so I'm fairly new to
  the concept, although I am following it well.
 
  If someone could explain to me how to use ether of these ( Date_diff
  and DateTime::diff ) I would be VERY grateful.
 
  Thank you so much!
  Matt
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 
 
 
 Matthew Croud
 Studio
 
 Obvious Print Solutions Limited
 Unit 3 Abbeygate Court
 Stockett Lane
 Maidstone
 Kent
 ME15 0PP
 
 T | 0845 094 9704
 F | 0845 094 9705
 www.obviousprint.com
 
 
 
 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] populate form input option dropdown box from existing data

2009-06-16 Thread Dajve Green

Yeah, I used to write that way but found I and others in the team saved a
lot more time troubleshooting and extending the code when returning 6 months
later if it was more verbose. Horses for courses, I guess.

Also, the selected attribute should be selected='selected' to validate
correctly as xhtml. By the same token, looking back at my example, I notice
the multiple attribute of the select should be multiple='multiple' -
apologies for missing that the first time =o)
 

 -Original Message-
 From: jenai tomaka [mailto:gargari...@hotmail.com]
 Sent: 16 June 2009 15:56
 To: m...@dajve.co.uk; af.gour...@videotron.ca; php-general@lists.php.net
 Subject: RE: [PHP] populate form input option dropdown box from existing
 data
 
 
 You can try like this,
 
 $row = stored data;
 
 and write the options like this
 option value=id (id == $row ? selected : ) /option
 
 
 Yuri Yarlei.
 
 
 
 
  From: m...@dajve.co.uk
  To: af.gour...@videotron.ca; php-general@lists.php.net
  Date: Tue, 16 Jun 2009 09:33:49 +0100
  Subject: RE: [PHP] populate form input option dropdown box from existing
 data
 
 
   -Original Message-
   From: PJ [mailto:af.gour...@videotron.ca]
   Sent: 15 June 2009 23:10
   To: php-general@lists.php.net
   Subject: [PHP] populate form input option dropdown box from existing
 data
  
   I am having difficulties figuring out how enter retrieved data into a
   dropdown box for editing. Here's a snippet:
   ...snip
   select name=categoriesIN[] multiple size=8
   option value=1Civilization/option
   option value=2Monuments, Temples amp; Tombs/option
   option value=3Pharaohs and Queens/option... snip
  
   As I understand it, I need to put an array ( $categoriesIN[] )
 somewhere
   in the above code... or do I need to insert a series of value
 selected
   fields for the values?
   The closest thing to what I need, seems to be in this post:
   http://www.hpfreaks.com/forums/index.php?topic=165215
   But it doesn't appear too inviting... not sure if they ever got it to
   work...
  
 
  Hi,
 
  Something like this should work for you
 
  ?php
// Set up initial values
// This could be hard-coded or brought in from DB or other source
$categoriesIN_options = array(1 = 'Civilzation',
2 = 'Monuments, Temples amp;
  Tombs',
3 = 'Pharaohs and Queens',
);
// Create a holder for values posted if you want
// to repopulate the form on submission
$selected_clean   = array();
// Check for passed values and validate
if (array_key_exists('categoriesIN',$_REQUEST)) {
  // Prevents errors with the foreach
  $passed_categoriesIN = (array)$_POST['categoriesIN'];
  foreach ($passed_categoriesIN as $options_key) {
// If the value isn't in our pre-defined array, handle the error
if (!array_key_exists($options_key,$categoriesIN_options)) {
  // Handle error
  continue;
}
$selected_clean[] = $options_key;
  }
}
  ?
  [snip]
  select name='categoriesIN' multiple size='8'
?php // Loop over the options set above
  foreach ($categoriesIN_options as $key = $value) {
echo option value='{$key}';
// Repopulate with selected
if (in_array($key,$selected_clean)) {
  echo  selected='selected';
}
echo {$value}/option\n;
  }
?
  /select
 
 
  --
 
  for (thoughts, ramblings  doodles) {
goto http://dajve.co.uk ;
  }
 
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 _
 Conheça os novos produtos Windows Live! Clique aqui.
 http://www.windowslive.com.br


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] difference between two times? Date_diff and DateTime::diff

2009-06-16 Thread Dajve Green

Also, if you're returning data from MySQL, there are a number of operations
you can perform before it reaches your script:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html


 

From: Tom Chubb [mailto:tomch...@gmail.com] 
Sent: 16 June 2009 15:55
To: Matthew Croud
Cc: Dajve Green; PHP General list
Subject: Re: [PHP] difference between two times? Date_diff and
DateTime::diff

The first example here may help:
http://us3.php.net/manual/en/function.time.php




2009/6/16 Matthew Croud m...@obviousdigital.com
Hi Dajve and Tom,

Thanks again, I totally didn't realise that this function is yet to be
implemented in the mainstream PHP,

So is there no function that exists in vanilla PHP that can take two
dates/times and supply the difference ?
If there isn't I do apologise, i've been talking to my friend who does
ASP.net and he said he was sure there is for PHP.






On 16 Jun 2009, at 13:11, Dajve Green wrote:

Hi Matthew,

A quick note on the DateTime::diff() method - it's only available as from
PHP 5.3, which is currently in release candidate stage, meaning unless you
have your own server running PHP, it won't be available to use (hopefully -
I would be sceptical of any webhost which rolls out RCs on production
servers).

If you need to know what version of PHP you're running, use:
phpversion() or phpinfo()
-Original Message-
From: Matthew Croud [mailto:m...@obviousdigital.com]
Sent: 16 June 2009 12:42
To: Tom Chubb
Cc: PHP General list
Subject: Re: [PHP] difference between two times? Date_diff and
DateTime::diff

Hi Tom,

Thanks for the reply,  I believe I have a fair understanding of
functions, and I have followed the example on the PHP manual page (
http://uk3.php.net/manual/en/function.date-diff.php
 ), ideally I want to know how to use the class DateTime::diff, how
can I use the DateTime::diff to get the difference between two times/
dates ? I suppose then I'm after the syntax

would it be like this for example:
$DIfferenceInTime  = DateTime::diff(10:00,12:32);

Thanks again for helping me out.



On 16 Jun 2009, at 12:33, Tom Chubb wrote:
Matt,
Do you understand how to use functions?
A function is defined like this:

function () {
//code goes here
}

You can pass arguments to be used in a function like this:

function($arg1, $arg2) {
//code goes here
}

In the first example on the following page:
http://uk3.php.net/manual/en/function.date-diff.php
To call the function you need to provide two arguments: $dtTime1 
$dtTime2

To use in a script, you need to first define the function, as per
the example:

?php

function GetDeltaTime($dtTime1, $dtTime2)
{
 $nUXDate1 = strtotime($dtTime1-format(Y-m-d H:i:s));
 $nUXDate2 = strtotime($dtTime2-format(Y-m-d H:i:s));

 $nUXDelta = $nUXDate1 - $nUXDate2;
 $strDeltaTime =  . $nUXDelta/60/60; // sec - hour

 $nPos = strpos($strDeltaTime, .);
 if (nPos !== false)
  $strDeltaTime = substr($strDeltaTime, 0, $nPos + 3);

 return $strDeltaTime;
}

?

Then you need to call the function like this:

?php
GetDeltaTime(time1-goes-here, time2-goes-here)
?

And it should spit out the difference.

Code is untested and if you didn't follow that I suggest you read up
on functions: http://www.w3schools.com/php/php_functions.asp

Hope this helps - I'm probably in a similar situation to you and
have been dabbling with PHP for a few years just as a hobby but
thought I'd try and help out.
You'll learn a lot from reading this list as well.

Cheers and good luck,

Tom


2009/6/16 Matthew Croud m...@obviousdigital.com

Hello,

My journey of learning PHP is going well, so i've decided to make a
small program which works out the difference between two times and
records the data in a text file.

I've searched the PHP manual for functions which can help me out,
and I discovered the function Date_diff (
http://uk3.php.net/manual/en/function.date-diff.php
)and the class DateTime::diff (
http://uk3.php.net/manual/en/datetime.diff.php
)

My question is, how on earth do I use these functions ? I really
don't understand the manual documentation.

I've just moved onto the subject of classes and so I'm fairly new to
the concept, although I am following it well.

If someone could explain to me how to use ether of these ( Date_diff
and DateTime::diff ) I would be VERY grateful.

Thank you so much!
Matt

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Matthew Croud
Studio

Obvious Print Solutions Limited
Unit 3 Abbeygate Court
Stockett Lane
Maidstone
Kent
ME15 0PP

T | 0845 094 9704
F | 0845 094 9705
www.obviousprint.com






-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Matthew Croud
Studio

Obvious Print Solutions Limited
Unit 3 Abbeygate Court
Stockett Lane
Maidstone
Kent
ME15 0PP

T | 0845 094 9704
F | 0845 094 9705
www.obviousprint.com


-- 
Tom Chubb
t...@tomchubb.com | tomch

RE: [PHP] populate form input option dropdown box from existing data

2009-06-16 Thread Dajve Green
A *multiple* select control, as in Phil's initial request will return an
array, however.


 -Original Message-
 From: tedd [mailto:tedd.sperl...@gmail.com]
 Sent: 16 June 2009 15:58
 To: PJ; php-general@lists.php.net
 Subject: Re: [PHP] populate form input option dropdown box from existing
 data
 
 At 6:09 PM -0400 6/15/09, PJ wrote:
 I am having difficulties figuring out how enter retrieved data into a
 dropdown box for editing. Here's a snippet:
 ...snip
 select name=categoriesIN[] multiple size=8
  option value=1Civilization/option
  option value=2Monuments, Temples amp; Tombs/option
  option value=3Pharaohs and Queens/option... snip
 
 As I understand it, I need to put an array ( $categoriesIN[] ) somewhere
 in the above code... or do I need to insert a series of value selected
 fields for the values?
 The closest thing to what I need, seems to be in this post:
 http://www.hpfreaks.com/forums/index.php?topic=165215
 But it doesn't appear too inviting... not sure if they ever got it to
 work...
 
 Think about it. A Select control returns *one* value and not an
 array. As such you don't need to provide an array to collect a single
 value.
 
 Here's the solution:
 
 HTML
 
 select name=categories 
 option value=1Civilization/option
 option value=2Monuments, Temples amp; Tombs/option
 option value=3Pharaohs and Queens/option
 /select
 
 PHP
 
 $category = $_POST['categories'];
 
 The variable $category will contain 1, 2, or 3 after a post form submit.
 
 Try it.
 
 The reference/link you cite above is for a dynamic select control
 that incorporates javascript to populate the second select control.
 It's not a simple select.
 
 If you are looking to understand what basic html does, try this:
 
 http://www.htmlcodetutorial.com/
 
 I often use this site to refresh my failing memory about html stuff.
 
 Cheers,
 
 tedd
 
 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] populate form input option dropdown box from existingdata

2009-06-16 Thread Dajve Green
Ack, yes - my bad. Missed the select name square brackets when I sent my
example.

And yeah, the only real difference between a multiple select and checkbox is
which works best with the UI (and, more often than not, how confused the
concept of multiple selects makes your end-user)

--

for (thoughts, ramblings  doodles) {
  goto http://dajve.co.uk ;
}
 

 -Original Message-
 From: tedd [mailto:tedd.sperl...@gmail.com]
 Sent: 16 June 2009 19:05
 To: php-general@lists.php.net
 Subject: Re: [PHP] populate form input option dropdown box from
 existingdata
 
 At 10:42 AM -0500 6/16/09, Shawn McKenzie wrote:
 He has a multiple select, so the select has to be an array or you just
 get one value even if more than one was selected (you just get the last
 one).
 
 Okay, it's not that much different than using check-boxes -- here's
 the solution for that:
 
 http://www.webbytedd.com/bbb/check-box-form/index.php
 
 and here's the select solution:
 
 http://www.webbytedd.com/bbb/select-box-form/index.php
 
 The point is to use name=option[] (i.e., name the array) and allow
 the browser to populate the POST array. Then on the php side of
 things, simply pull out the data from the POST array via the array's
 name.
 
 You can see how this is done via the above links.
 
 Cheers,
 
 tedd
 
 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Re: Form handling

2009-06-12 Thread Dajve Green

 -Original Message-
 From: Manuel Lemos [mailto:mle...@acm.org]
 Sent: 11 June 2009 07:21
 To: Eddie Drapkin
 Cc: PHP General Mailing List
 Subject: [PHP] Re: Form handling
 
 Hello,
 
 on 06/10/2009 03:10 PM Eddie Drapkin said the following:
  I've been charged with writing a class that handles forms, once they've
 been
  POSTed to.  The idea of the class is to handle the most common use-cases
 of
  POST forms, and any special functionality can be handled with a child
 class
  at a later date, but for our uses, we're going to have mostly pretty
 typical
  POST forms.  Follows is the list of cases I've determined that are the
 most
  common, can anyone think of any that are omitted or that are never going
 to
  be used?
 
  class form_handler {
  public /* bool */ function setRequiredFields(array $fields); //takes
 a
  simple array that corresponds to a $_POST key, verifying that there is
 data
  on required fields but not for optional fields, returns true or false on
  error
  public /* bool */ function setRequiredFieldTypes(array $fieldTypes);
  //array of field names = type a la ('username' = array(regex,
  '/a-zA-Z0-9\-_/'))
  //or 'phone_number' = (array('int', 'min_len' = 7,
  'max_len' = 10)) etc, the exact spec is obviously nowhere near done but
  will probably just wrap a lot of filter_ functions, returns true or
 false on
  error
  public /* string */ function validateAndCaptureError(); //returns
 error
  or empty string
  public /* void */ function validateAndForwardTo($page); //forwards
 to
  page on error, or not
  }
 
  each of the globule setters will have a corresponding appendRequired...
  method, so as not to require handling enormous data structures for
  conditional form building.
  ♦
  As you can see, the class looks pretty barren, but I can't think of any
 more
  functionality than would be required, although I am kickign the idea
 around
  of having very specific validation type methods ie.
  form_handler::requireInt($field, array $options) or
  form_handler::requireRegex($field, $regex), etc.
 
  Thoughts?
 
 You may want consider not reinventing the wheel.
 
 I use this popular forms generation and validation class since about 10
 years now. It can deal with pretty much all you need now and probably
 later.
 
 http://www.phpclasses.org/formsgeneration
 
 Here are some live examples of the class and its plug-ins.
 
 http://www.meta-language.net/forms-examples.html
 
 --
 
 Regards,
 Manuel Lemos

Whilst Manuel's Forms Generation class / Zend Form et al are certainly quick 
and easy to integrate, if this is an in-house project, I don't see a problem 
with rolling your own Forms class designed to simply automate some of the 
common functionality your developers use. In a lot of cases, out-of-the-box 
classes come with bloat while being all things to all men, which is not a bad 
thing, per se, but needs to be weighed up when selecting a methodology.

To answer your original question, though, our in-house framework has a base 
Form class, which is extended through child classes, as well as a base input 
class, which is extended with child classes such as TextInput, SelectInput etc. 
This allows each input to have its individual validate() methods, as well as 
creating a wrapper Form::validate() method which loops through the 
Input::validate() methods.

Example

$Form   = new Form;
$Form-fields['text']   = new TextInput;
$Form-fields['select'] = new SelectInput;
$Form-fields['select']-options= array('foo' = 'bar', 'wom' = 'bat');

With regard your setRequiredFields(), we use an Input::$mandatory property, 
which the Input::validate() method checks.
The setRequiredFieldTypes() is dealt with by the actual File class.
I would certainly separate out the required validation as mentioned with 
requireInt/Regex methods, which would allow combining of required conditions. 
We use a $validation bitwise property in the Input class combined with class 
constants, as well as minlength / maxlength properties for text / password 
fields.

Other things to consider

- Some method of storing user messages during validation which can be output 
later.

- Creating an output method for the fields. Repopulating the fields with 
submitted values, using CSS to highlight missing or invalid fields or creating 
custom fields (such as a date input with a calendar / made up of Y/M/D select 
inputs) can lead to a mess if you are hardcoding the output. 
For example:

$Form-fields['text']-add_css_class('missing_mandatory');
$Form-fields['text']-set_value('Foo');
$Form-fields['text']-output();

Something further to consider would be to have a $value property and use 
__set() overloading 
(http://us3.php.net/manual/en/language.oop5.overloading.php) to validate
Eg 
$Form-fields['select']-value = 'foo'; // Sets value to foo
$Form-fields['select']-value = 'No such option'; // Does not set

- A method / property to check whether the form has been submitted.
If you