This topic is locked

Prevent Entry of Compound Fields

2/6/2009 8:58:57 PM
PHPRunner General questions
F
fryon author

Good day,
How can i prevent entry of values whose 3 characters are already in the database. Say, i entered 456, therefore the combinitions of these characters should be prevented thus if one enters any of these values :465,564, 654, 645, and 564 it will prompt the user: "values are already stored". How can i do that in PHPRUNNER?
Regards master and more power

J
Jane 2/9/2009

Hi,
check entered value in the Before record added/Before record updated events on the Events tab.

Here is a sample:

if ($values["FieldName"])

{

//check this value here

//and show error message if needed

$message = "values are already stored";

return false;

}

return true;

F
fryon author 2/9/2009

Hi,

check entered value in the Before record added/Before record updated events on the Events tab.

Here is a sample:


Thanks for reply Jane, but i think this would only check for single values, as i mentioned the cases above. I mean if i enter "456" its ok, but in my next entry say "456", "564", "465" etc and other characters containing combinition of 456, it will be prevented. Can we do it in phprunner?
More power master.

F
fryon author 2/10/2009

Anybody out there? can you give a slight idea for this?

T
thesofa 2/11/2009

get the entered value into a string

explode the string

set each part of the string into a variable, so now you have three variables, $variable1, $variable2 and $variable3

do a select query where the filter option is 'contains $variable1 AND $variable2 AND $variable3.

If the result of the query contains a record, reject entry.

F
fryon author 2/11/2009

get the entered value into a string

explode the string

set each part of the string into a variable, so now you have three variables, $variable1, $variable2 and $variable3

do a select query where the filter option is 'contains $variable1 AND $variable2 AND $variable3.

If the result of the query contains a record, reject entry.


Thank you, but i am new in php...hope you can provide a slight code for this.
More power, and thank you.

T
thesofa 2/13/2009

send me the table structure and I will play with it to try to give you a working example, this could be useful for many applications.

J
Jane 2/13/2009

Hi,
I recommend you to check preg_match() PHP function here:

http://php.net/manual/en/function.preg-match.php
Also here is a good "How to use regular exressions in PHP" article:

http://www.webcheatsheet.com/php/regular_expressions.php

T
thesofa 2/13/2009

Hi

I have looked at preg_match and preg_match_all and they would seem to give an answer
Preg_match only returns 0 or 1 depending if a match occurs or not, also it does not find a match if the $subject is in a different order to the $pattern. so it looks as if preg_match_all will be the one, tried this code

<?php

$pattern = '/[456]/';

$subject = "465";

$hh=preg_match_all($pattern, $subject, $matches);

//print_r($matches);

echo "<br>";

echo $hh;

?>



If I run this from my web server, I get the value of 3 returned, this shows that all 3 characters in $subject are matched by all 3 characters in $pattern

BUT, if I change $subject to "666", I still get an answer of 3, so I need to dig deeper

A
alang 2/15/2009

Suspect the following may give better result for the regular expression:
'/(?=.[4])(?=.[5])(?=.[6])/'
But wouldn't it be better to do it in the SQL, ie:
select
from tablename where

fieldname like '%4%'

and fieldname like '%5%'

and fieldname like '%6%'
Assuming fieldname is string type.

T
thesofa 2/15/2009

that is what I suggested in my first answer, but Jane suggested the preg_match function, so I explored that route.

Surely I could use Contains instead of like?

A
alang 2/16/2009

that is what I suggested in my first answer, but Jane suggested the preg_match function, so I explored that route.

Surely I could use Contains instead of like?


I agree with you. Seems like MSSQL has a "contains" function and MySQL uses the "LIKE" function as above to do the same thing.

T
thesofa 2/17/2009

OK, lets have another look at this one

I have assumed that the code you have put in is in a field called code in a table called ttable and it is a varchar of 3 characters long
in the

function BeforeAdd(&$values,&$message,$inline)

Global $conn;

$test=$values["code"];

$bits=str_split($test,1);

$strSQLExists="select * from `ttable` where ((`code` like '%'".$bits[0]."'%') and (`code` like '%'".$bits[1]."'%') and (`code` like '%'".$bits[2]."'%'));";

$rs=db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data){

$message= "You have chosen a number set that has already been used, please try again.";

return false;

}

Return true;



try that, I may have some syntax wrong with the ", ' and ` but I cannot check it as my web server is dead.
Also, assuming that you need to check for all the numbers and nothing but the numbers, you will have to add in some more checks

ie, lets say you have 456 in the table as a valid entry
you want to repel 465 and any combinations of the numbers, but what if you had a new number of 466, will that have to be rejected?

using the search string above it would be.
HTH

jpedwardspost 3/9/2009

Hi Fryon,
Curious problem you posted, don't know whether you ever solved it but you could try this approach:
Add a column to your table - maybe call it 'SortedCompound'

Index this field so that it can accept unique values only.
Use a pre-update event to sort the elements of the compound field that if the user enters e.g.

If user enters 731 it sorts to 137,

If user enters 173 it sorts to 137,

If user enters 713 it sorts to 137 - in each case any combination of 1 and 3 and 7 will sort to 137.
Store the sorted value (137) in a variable and execute sql to test whether it already exists in the column 'SortedCompound'
If it already exists then throw it out (tell the user) , if it doesn't exist store the both the user value and it's corresponding sorted value.
JP

F
fryon author 5/18/2009

Hi Fryon,

Curious problem you posted, don't know whether you ever solved it but you could try this approach:
Add a column to your table - maybe call it 'SortedCompound'

Index this field so that it can accept unique values only.
Use a pre-update event to sort the elements of the compound field that if the user enters e.g.

If user enters 731 it sorts to 137,

If user enters 173 it sorts to 137,

If user enters 713 it sorts to 137 - in each case any combination of 1 and 3 and 7 will sort to 137.
Store the sorted value (137) in a variable and execute sql to test whether it already exists in the column 'SortedCompound'
If it already exists then throw it out (tell the user) , if it doesn't exist store the both the user value and it's corresponding sorted value.
JP


SO LATE I DID make a reply... This is what i did :
I tried to insert values in a separate table

INSERT INTO separate_table (firstNo) VALUES ('$string123')"; ..etc..etc..
//concatenate

$string1 = $compoundOne;

$string2 = $compoundTwo;

$string3 = $compoundThree;
Where i used query to determin $compoundOne.

$compoundOne= SELECT SUBSTR('$numbr', 1, 2) FROM pick_numbers

$compoundTwo= SELECT SUBSTR('$numbr', 3, 2) FROM pick_numbers

$compoundThree= SELECT SUBSTR('$numbr', 5, 2) FROM pick_numbers
Then checking the combinition:

//first combinition

$NewString12 = $string1 . $string2;

$string123 = $NewString12 . $string3;
//second combinition

$NewString132 = $string1 . $string3;

$string132 = $NewString132 . $string2;
//third combinition

$NewString213 = $string2 . $string1;

$string213 = $NewString213 . $string3;

E
e61 5/18/2009

Maybe this regex will do it?:

if (preg_match('/[4,5,6]/', $value)) {



} else {



}


I have tested it on $value like:

456

654

645

444

556

464jkfk

aSDF654
It marks/find all the numbers in the above examples...