ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-03 18:27
Návrat hore
Odpovedať s citátom
|
I would like to be able to write some PHP code to be used to generate the default value for a vield when a new record is being added.
Does anyone know if there is a good trick to doing this?
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-03 21:29
Návrat hore
Odpovedať s citátom
|
| Kód: |
$col_name_default_value = 'Paris';
$opts['fdd']['col_name'] = array(
'default' => $col_name_default_value,
'input' => '',
'maxlen' => 30,
'name' => 'City',
'options' => 'ACPVDFL',
'select' => 'D',
'size' => 30,
'sqlw' => 'TRIM("$val_as")',
'sort' => true
); |
|
ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-03 21:54
Návrat hore
Odpovedať s citátom
|
Well, close. But I want to be able to determine the value at the time the insert form is displayed.
For example, to use the current time of day, or something like that...
Can I call a function?
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-03 22:02
Návrat hore
Odpovedať s citátom
|
Yes. However crystal ball was recently stolen by a roving band of Gypsies and I am unable to determine exactly what you want to do.
|
ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-03 23:46
Návrat hore
Odpovedať s citátom
|
Sheesh, one would expect to see lots of functioning crystal balls on eBay these days...
Sorry, my specific example is a little complicated, so I am trying to simplify it by focusing on the mechanism...
But I want the opportunity to calculate the default value. Ideally I would do something like:
$opts['fdd']['col_name'] = array(
'default' => generate_default_value(),
'input' => '',
'maxlen' => 30,
'name' => 'City',
'options' => 'ACPVDFL',
'select' => 'D',
'size' => 30,
'sqlw' => 'TRIM("$val_as")',
'sort' => true
);
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-04 00:33
Návrat hore
Odpovedať s citátom
|
Yes, you are on the right track. Calling a function will probably work. I can't remember an instance where I've called a function directly in the $opts['fdd'] array, but it probably would work OK.
|
ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-04 01:36
Návrat hore
Odpovedať s citátom
|
The important thing is whether the function gets evaluated at the time an insert record gets called or when the option array is populated.
A simple example would be to implement an auto-incrementing field.
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-04 01:53
Návrat hore
Odpovedať s citátom
|
Just a wild guess, if auto increment then you probably need to be looking at triggers. On the other hand the form won't work without a unique identifier, which is usually the auto_increment field defined in the table.
|
ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-04 01:58
Návrat hore
Odpovedať s citátom
|
Right, I know about auto-incrementing DB fields...
I can't actually use an auto-incrementing MySql field type in my particular case, which is why I need to do it otherwise.
Looks like triggers might be right.
I see the documentation on them, but I don't see an example. What does the inc file look like, is it just raw PHP code or a function that takes no arguments but that returns true/false?
I am guessing I would be doing a $opts[’triggers’][’insert’][’pre’] = ’categories.TIP.inc’;
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-04 02:22
Návrat hore
Odpovedať s citátom
|
If you use the forum's Search feature with "triggers" you should find plenty of examples. Basically you roll your own trigger.
| Kód: |
// Variables available within included triggers:
$this object reference
$this->dbh initialized MySQL database handle
$this->key primary key name
$this->key_type primary key type
$this->key_delim primary key deliminator
$this->rec primary key value (update and delete only)
$newvals associative array of new values (update and insert only)
$oldvals associative array of old values (update and delete only)
$changed array of keys with changed values |
|
ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-04 23:28
Návrat hore
Odpovedať s citátom
|
OK, I got my Insert Pre trigger to be called, and I am properly calculating my new default value.
And I thought the right thing was to add that value to the $newvals array, but it doesn't show the value when the form actually displays.
Here is my code:
| Kód: |
1 <?php
2 $data = $this->myquery("select MAX(MembershipNumber) from Membership;");
3 $result = mysql_fetch_array($data);
4 $next = $result[0]+1;
5 $newvals['MembershipNumber'] = $next;
6 print "<br/> TRIGGER CODE EXECUTED Next ID = $next <br/>";
7 return(true);
8 ?> |
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-04 23:49
Návrat hore
Odpovedať s citátom
|
Does it save the intended value in the database after clicking Save?
|
ScottChapman
Založený: 09.06.2007
Príspevky: 31
Zaslal: 2009-04-05 00:04
Návrat hore
Odpovedať s citátom
|
Nope.
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-05 00:09
Návrat hore
Odpovedať s citátom
|
So, if your code resides in the filename "your_code.php" then your script contains
| Kód: |
| $opts['triggers']['insert']['pre'] = 'your_code.php'; |
at a location above the call to the class file?
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2009-04-05 00:20
Návrat hore
Odpovedať s citátom
|
you might do well to build some troubleshooting / debugging methods into the trigger, e.g.
| Kód: |
<?php
$query2 = 'select MAX(MembershipNumber) from Membership';
if($this->MyQuery($query2)){
$result = mysql_fetch_array($data);
$next = $result[0]+1;
$newvals['MembershipNumber'] = $next;
print "<br/> TRIGGER CODE EXECUTED Next ID = $next <br/>";
return false;
}else{
echo "\n".'<p>'.mysql_error().'</p>';
echo "\n".'<a href="javascript:history.go(-1)" onmouseover="self.status=document.referrer;return true">Go Back</a>';
@mysql_close($this->dbh);
require_once('./inc/footer.php');
exit;
}
?> |
|
|
Odoslať novú tému
Odpovedať na tému
Choď na stránku 1, 2 Ďalší
|