PDA

View Full Version : php data validation


jackam1
08-10-2006, 11:23 AM
Hi

I have small php program that gets data from server and displays it in table.
Want I need to add is a sort of data validation if statements that examines the content of eact data, in this case 'part_number' ' and ignores the data if it has two (-) sings in it or if it has any (+) sings in it.
ie.
123-456-1-78 is ok to dispaly
123-456-789 not ok
1-2-4
123+456-24-9 not ok ignore it and don't diaply it

here is part of teh php loop that I use to display the data. I am gessing ti goes there:

$row = mysql_fetch_array($result);


echo "<table border='1' align='center'> ";
echo "<tr> <th>Part Number</th> <th>Price</th> </tr>";
// keeps getting the next row until there are no more to get


echo "<tr><td>";
echo $row['part_number'];
echo "</td><td>";
echo $row['price_each'];
echo "</td></tr>";

while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

echo "<tr><td>";
echo $row['part_number'];
echo "</td><td>";
echo $row['price_each'];
echo "</td></tr>";
}

echo "</table>";

thanks
jack

NancyJ
08-10-2006, 11:35 AM
$row = mysql_fetch_array($result);


echo "<table border='1' align='center'> ";
echo "<tr> <th>Part Number</th> <th>Price</th> </tr>";
// keeps getting the next row until there are no more to get


echo "<tr><td>";
echo $row['part_number'];
echo "</td><td>";
echo $row['price_each'];
echo "</td></tr>";

while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
if((substr_count($row['part_number'], "-") !=2) && (substr_count($row['part_number'], "+") ==0))
{
echo "<tr><td>";
echo $row['part_number'];
echo "</td><td>";
echo $row['price_each'];
echo "</td></tr>";
}
}

echo "</table>";

jackam1
08-10-2006, 11:47 AM
Hi
Thanks for the reply.
I have tested your code, but it does printer part numbers with more than one (-) in it.
sorry.
thanks
jack

NancyJ
08-10-2006, 11:52 AM
you only said to not print ones with 2 "-" in, not any thats more than one.
Is it printing ones with 2 "-" in?

jackam1
08-10-2006, 11:57 AM
Hi Nancy

You are ofcourse 100% right.

No, it does not print part numbers with 2 (-), only printes with 3 (-).
Many applogies for putting to wrongly.

As we only use max of 4 (-), is it easy to alter it so no part numbers get printed for more than one (-)?
Many thanks for the quick reply, and much appreciated.
thanks
jack

NancyJ
08-10-2006, 12:36 PM
yes of course,
just change
(substr_count($row['part_number'], "-") !=2
to
(substr_count($row['part_number'], "-") <2

jackam1
08-10-2006, 01:56 PM
Hi Nancy

That code worked like a dream. many thanks.

I have a limited knowledge of php but shouldn't your code for testing (-) or (+) contain OR statement instead of && (and) ???
The reason is this. Due to our large number of part numbers I have found that I have to put many more conditions to be tested such as if it includes a space ( ) don't print the data.
And i was hoping to understand the code you have written which seems to be quite straight forward,but one thing i can't understand is that you have included && instead of OR condition and it still works.
Or am I reading it wrong? which I am sure I have.
i want to test for alot of different conditions, surely I will need an OR statement there,,,no?

thanks jack

NancyJ
08-10-2006, 02:32 PM
AND vs OR can be confusing...
The code says.
Only show IF our product number contains less than 1 '-' AND it contains no '+'
If it were an OR it would say
Show IF product number contains less than 1 '-' or it doesnt contain a '+'

So if either of the conditions (less than 1 -) and (no +) returned true then it would show.
Which isnt what we want, we want both conditions to return true.

Hope that makes sense

jackam1
08-10-2006, 02:58 PM
Hi Nancy

It does make perfect sense, now that you have explained it that way.
I can now add other conditions easily but I have just one difficult one that I know it will take me days to figure out.
If you could help me I will for ever be grateful and let you consider this case closed.lol.

I want a statement that says if a part number has 7 characters before a (-)starts , do not print that part number. i.e. if there is part number that does not include a (-) in the first 7 characters, then do not print it.
Many thanks
Jack

p.s. your wet site is awsome and very different from all the usual sites, which makes a nice change.
:thumbsup:

NancyJ
08-10-2006, 03:16 PM
you need to add
&& ((strpos($row['part_number'], "-") <7) && (strpos($row['part_number'] !== false))

...I think I got the logic right there

jackam1
08-10-2006, 03:39 PM
Hi
Nice try but it doesn't work.
I think you got a bracket missing at the end and also, when I try to search a data of 24, only one shows with value of C100, ..
Any clues?
Thanks
jack

NancyJ
08-10-2006, 03:44 PM
OK, if the logic is right then this should work - all the brackets line up

if((substr_count($row['part_number'], "-") !=2) && (substr_count($row['part_number'], "+"))&& ((strpos($row['part_number'], "-") <7) && (strpos($row['part_number'] !== false))))

not sure what you mean about 24 and c100?

jackam1
08-10-2006, 03:55 PM
Hi Nancy

Still the same, it only displays the first record when it should display 24 records.

I can wait if you are busy now nancy, cos you have helped me alot and I am very grateful.
thnaks
jack

NancyJ
08-10-2006, 04:42 PM
apologies - just spotted a big mistke

if((substr_count($row['part_number'], "-") !=2) && (substr_count($row['part_number'], "+"))&& ((strpos($row['part_number'], "-") <7) && (strpos($row['part_number'], "-") !== false)))

marek_mar
08-10-2006, 07:22 PM
if(preg_match('/^[0-9]+(?:-[0-9]+){3,}$/', trim($row['part_number'])))
{

jackam1
08-11-2006, 09:52 AM
Hi

Sorry Marek mar, your code didn't work properly. It only showed the first record as well, when it should have displayed all the 20 records.

Nancy, am I correct to assume that teh last part of the code (strpos($row['part_number'], "-") !== false))) means , don't dipaly part numbers that have no (-) in them?
If thats the case then there are some part numbers that have no (-) in them and I want them displayed.
there fore I just added the first part of the code ..ie. ((strpos($row['part_number'], "-") <7) which should stop parts displaying that they do not have a (-) in the first 7 letters, ( and parts that are more than 7 letters long and have no (-) at all), but it still displays parts longer than 7 letters without a dash.
Where am I going wrong?

so just to some it all up:
I want parts NOT displayed that:
1) have spaces
2) have more than 1 (-) , dash
3) have (+) plus sign in them
4) have no dashes (-) in the first seven letters
note: But I want parts to be dispalyed that have no dashes (-) but are less than 7 letters wrong
5)NEW: parts that has no dashes with the exception of parts that start with letters T or AT or BT.

I am sorry to keep adding exceptions to the list but this should be all.
its just that we a have a mssive amount of data and afew exceptions that are not strictly part numbers that need to route out.
Hope I am not being too demanding.
Please take your time and think of it as a puzzle.lol
many thanks
jack

marek_mar
08-11-2006, 06:08 PM
Yes well my code was supposed to be a replacement fot the if statement inside the while loop.
It would match any sequence of number dash number where each number is at least one character and there are at least 4 numbers.
You have changed all requirements now though.