Set up an array that checks a large list of numbers

Hi,
I’m pretty much of a beginner, and that may be why I’m having difficulty phrasing my question in such a way that search results for answers turn up anything useful!

Anyway, here’s the question: I have a Wordpress site that has a section that is only available to “Certified Trainers”. These Certified Trainers have to enter their Certification Number when they register for the site, and I need code that checks to see if the number they enter is valid, i.e., it’s a number that my client has actually assigned to a real Certified Trainer.

I’ve managed to write php code that does this nicely as a test, in which I simply entered example numbers (1,2,3,4) into an array of “valid numbers”. However, there are hundreds of real Certification Numbers, and they’ve been more or less randomly assigned (i.e., I can’t set it up to say “any number between 100 and 200 is okay”); I actually need to have the array refer to a list of several hundred numbers.

It seems very unwieldy and inelegant, to say the least, to make an array which actually includes all those numbers, and to which I’ll have to manually add a number every time a new Trainer is Certified. What I’m imagining as a solution is a separate list of numbers that the array will be able to check upon execution of the code… that way as long as the list is kept up to date, the code will work.

Here’s what I have so far:
[php]

<?php $valid_certs = array(1,2,3,4,); $cert = get_user_field ("cert_number"); if (isset($_GET['first']) && !empty($cert) && in_array($cert, $valid_certs)) { $user = new WP_User(S2MEMBER_CURRENT_USER_ID); $user->set_role('s2member_level1'); } echo $cert; ?>[/php]

FYI, the variable ‘first’ is referring to the fact that this code only needs to be run the first time the user sees this page, i.e., right after they register for the site; and I’m using the S2member plugin for Wordpress for registering users, which is what all those references are about, for anyone who’s not familiar with it.

If anyone can help me figure this out, I’d appreciate it!

Does the client have a database with those cert numbers in it?

They have the numbers in an offline ACT database…

I would have to agree, you may want to get read access to this database, or have them send you a report and import it into a local database.
if it’s a real simple database, like you mention just a number, than MySQL may be a bit of overkill.
You may opt for SQLite or a simple flatfile

with SQL you most likely will want to build an admin interface
a flatfile you can just update the file as needed

Don’t even need a database. If they can export the user data to a file and then upload it, then all you need to do is open the file, read the contents into an array, then just use in_array() to see if the cert number exists.

Thats what a flatfile is :slight_smile:
and depending on the size of the file, importing it into an array wud be costly in php.
so just a lookup routine will suffice

They can export the data; and I could put all the numbers into the array. But I was hoping there would be a way to check a separate list rather than have several hundred numbers in the code itself. That would also have the advantage that as new numbers are added (or subtracted), the list can be replaced or edited by someone who doesn’t know anything about php, rather than having to get someone who has some knowledge of how to modify the array manually.

Laffin, a “lookup routine” sounds promising… can you give me a little more detail? Thanks!

This really depends on the datafile format
if’ it’s just a number per line, it can be as simple as
[php]

<?php $fh=fopen('datafile.txt','r'); $query=151; $found=FALSE; while(!found && !feof($fh)) { $line=fgets($fh); if(trim($line)==$query)) $found=TRUE; } fclose($fh); if($found) { echo "Cert # {$query} found in database"; } else { echo "Unknown Cert #"; } [/php]

You don’t want limit the number lines, i would first count the number of entries, then use that as the max and i think you have an extra ) in that first if statement.

You are correct there is an extra ) in the if

but there is no limit to lines in the routine, it terminates early when a match has been found, as there is no need to continue processing the rest once a match has been found.

That looks like it should work, to my inexperienced brain, but it’s returning “Unknown Cert#”, even though the number is in the list (datafile.txt).

I think I understand all your code; in the line [php]$query=151[/php] you’re using 151 as a number which should be replaced by an actual Certification Number, correct? The number I replaced it with in my code is a real Certification Number, 10-0001. Is it possible I don’t have datafile.txt formatted correctly? You can see it, if you like… this is how that line reads in my code: [php] $fh=fopen(“http://ecbiz119.inmotionhosting.com/~nappit5/wp-content/uploads/2012/04/datafile.txt","r”);[/php]

Correct I checked this code against the datafile, it works with no issues
[php]<?php
$fh=fopen(‘datafile.txt’,‘r’);
$query=‘11-10111x’; // added x to do a failure check, removed x to do a success check
$found=FALSE;
while(!$found && $line=fgets($fh))
{
if(trim($line)==$query)
$found=TRUE;
}
fclose($fh);
echo "Certification {$query} was ". ($found?’’:'not ') .‘found’;
?>
[/php]

added note:
Some webhosts don’t allow fopen url’s, if these are in the same server use the pathname :slight_smile:

Excellent! It works (even with the absolute url); you’ve been extremely helpful. Thanks so much for your time on this!

Sponsor our Newsletter | Privacy Policy | Terms of Service