Help with PHP script
- wpas
- Graduate


- Joined: Jul 12, 2010
- Posts: 214
- Loc: Canada
- Status: Offline
Hi All
I am working on a project and need some help to get to completion.
I have an array that assigns a number to each letter of the alphabet.
Let us say we have the name "Thomas John Hancock"
First I need to get the letter number for the firstname, middle name and last name.
I do this as follows:
This gives me the following output:
T = 2
H = 8
O = 6
M = 4
A = 1
S = 1
J = 1
O = 6
H = 8
N = 5
H = 8
A = 1
N = 5
C = 3
O = 6
C = 3
K = 2
Here is the interesting part now.
What I want to do is get a table as follows:
and so on....
If we look at the Firstname column, we see that we start with the first letter of the first name and its value.
The letter gets repeated according to its value. We see T=2, so it gets repeated 2 times.
We then take the next letter of the first name and its value.
It again gets repeated according to its value.
This holds for the rest of the letters in the first name.
After we have reached the last letter of the first name, the process gets repeated starting again with the first letter of the first name.
The same also holds for the Middlename and Lastname colums.
The Total column is simply the addition of the letter values accross the row.
I stopped at the end of the last name as we could go on forever.
What I also need to do then is to be able to limit the number of rows that will be generated.
Once this has been done, I then need to be able to select any row and see its Total.
If I select then row 20, I want to be able to see the row number and the Total value for that row which would be 20 and Total of 15.
This is because sometimes I may not want the table printed out, but want only to be able to select a row in the table and display the row and Total valule
I would appreciate any help someone can give me on this.
Thanks
I am working on a project and need some help to get to completion.
I have an array that assigns a number to each letter of the alphabet.
Let us say we have the name "Thomas John Hancock"
First I need to get the letter number for the firstname, middle name and last name.
I do this as follows:
Code: [ Select ]
<?php
$ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8);
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
//make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
//Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
//now loop through each name one by one to get the values of each letter in the name
//first name
for($i=0; $i<$lenfn; $i++)
{
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
echo $alphafn." = ".$letternumberfn."<br />";
}
echo "<br />";
//middle name
for($i=0; $i<$lenmn; $i++)
{
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
echo $alphamn." = ".$letternumbermn."<br />";
}
echo "<br />";
//last name
for($i=0; $i<$lenln; $i++)
{
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
echo $alphaln." = ".$letternumberln."<br />";
}
?>
$ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8);
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
//make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
//Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
//now loop through each name one by one to get the values of each letter in the name
//first name
for($i=0; $i<$lenfn; $i++)
{
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
echo $alphafn." = ".$letternumberfn."<br />";
}
echo "<br />";
//middle name
for($i=0; $i<$lenmn; $i++)
{
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
echo $alphamn." = ".$letternumbermn."<br />";
}
echo "<br />";
//last name
for($i=0; $i<$lenln; $i++)
{
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
echo $alphaln." = ".$letternumberln."<br />";
}
?>
- <?php
- $ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8);
- $firstname = "Thomas";
- $middlename = "John";
- $lastname = "Hancock";
- //make all names upper case. to avoid messing with small letters.
- $firstname = strtoupper($firstname);
- $middlename = strtoupper($middlename);
- $lastname = strtoupper($lastname);
- //Find the number of letters in each name
- $lenfn = strlen($firstname);
- $lenmn = strlen($middlename);
- $lenln = strlen($lastname);
- //now loop through each name one by one to get the values of each letter in the name
- //first name
- for($i=0; $i<$lenfn; $i++)
- {
- $alphafn = $firstname[$i];
- $letternumberfn = $ar[$alphafn];
- echo $alphafn." = ".$letternumberfn."<br />";
- }
- echo "<br />";
- //middle name
- for($i=0; $i<$lenmn; $i++)
- {
- $alphamn = $middlename[$i];
- $letternumbermn = $ar[$alphamn];
- echo $alphamn." = ".$letternumbermn."<br />";
- }
- echo "<br />";
- //last name
- for($i=0; $i<$lenln; $i++)
- {
- $alphaln = $lastname[$i];
- $letternumberln = $ar[$alphaln];
- echo $alphaln." = ".$letternumberln."<br />";
- }
- ?>
This gives me the following output:
T = 2
H = 8
O = 6
M = 4
A = 1
S = 1
J = 1
O = 6
H = 8
N = 5
H = 8
A = 1
N = 5
C = 3
O = 6
C = 3
K = 2
Here is the interesting part now.
What I want to do is get a table as follows:
Code: [ Select ]
Row Firstname Middlename Lastname Total
0 --- T=2 --- J=1 --- H=8 11
1 --- T=2 --- O=6 --- H=8 16
2 --- H=8 --- 0=6 --- H=8 22
3 --- H=8 --- 0=6 --- H=8 22
4 --- H=8 --- 0=6 --- H=8 22
5 --- H=8 --- 0=6 --- H=8 22
6 --- H=8 --- 0=6 --- H=8 22
7 --- H=8 --- H=8 --- H=8 24
8 --- H=8 --- H=8 --- A=1 17
9 --- H=8 --- H=8 --- N=5 21
10 --- O=6 --- H=8 --- N=5 19
11 --- O=6 --- H=8 --- N=5 19
12 --- O=6 --- H=8 --- N=5 19
13 --- O=6 --- H=8 --- N=5 19
14 --- O=6 --- H=8 --- C=3 17
15 --- O=6 --- N=5 --- C=3 14
17 --- M=4 --- N=5 --- C=3 12
18 --- M=4 --- N=5 --- O=6 15
19 --- M=4 --- N=5 --- O=6 15
20 --- M=4 --- N=5 --- O=6 15
21 --- A=1 --- J=1 --- O=6 8
22 --- S=1 --- O=6 --- O=6 13
23 --- T=2 --- O=6 --- O=6 14
24 --- T=2 --- O=6 --- C=3 11
25 --- H=8 --- O=6 --- C=3 17
26 --- H=8 --- O=6 --- C=3 17
27 --- H=8 --- O=6 --- K=2 16
28 --- H=8 --- H=8 --- K=2 18
0 --- T=2 --- J=1 --- H=8 11
1 --- T=2 --- O=6 --- H=8 16
2 --- H=8 --- 0=6 --- H=8 22
3 --- H=8 --- 0=6 --- H=8 22
4 --- H=8 --- 0=6 --- H=8 22
5 --- H=8 --- 0=6 --- H=8 22
6 --- H=8 --- 0=6 --- H=8 22
7 --- H=8 --- H=8 --- H=8 24
8 --- H=8 --- H=8 --- A=1 17
9 --- H=8 --- H=8 --- N=5 21
10 --- O=6 --- H=8 --- N=5 19
11 --- O=6 --- H=8 --- N=5 19
12 --- O=6 --- H=8 --- N=5 19
13 --- O=6 --- H=8 --- N=5 19
14 --- O=6 --- H=8 --- C=3 17
15 --- O=6 --- N=5 --- C=3 14
17 --- M=4 --- N=5 --- C=3 12
18 --- M=4 --- N=5 --- O=6 15
19 --- M=4 --- N=5 --- O=6 15
20 --- M=4 --- N=5 --- O=6 15
21 --- A=1 --- J=1 --- O=6 8
22 --- S=1 --- O=6 --- O=6 13
23 --- T=2 --- O=6 --- O=6 14
24 --- T=2 --- O=6 --- C=3 11
25 --- H=8 --- O=6 --- C=3 17
26 --- H=8 --- O=6 --- C=3 17
27 --- H=8 --- O=6 --- K=2 16
28 --- H=8 --- H=8 --- K=2 18
- Row Firstname Middlename Lastname Total
- 0 --- T=2 --- J=1 --- H=8 11
- 1 --- T=2 --- O=6 --- H=8 16
- 2 --- H=8 --- 0=6 --- H=8 22
- 3 --- H=8 --- 0=6 --- H=8 22
- 4 --- H=8 --- 0=6 --- H=8 22
- 5 --- H=8 --- 0=6 --- H=8 22
- 6 --- H=8 --- 0=6 --- H=8 22
- 7 --- H=8 --- H=8 --- H=8 24
- 8 --- H=8 --- H=8 --- A=1 17
- 9 --- H=8 --- H=8 --- N=5 21
- 10 --- O=6 --- H=8 --- N=5 19
- 11 --- O=6 --- H=8 --- N=5 19
- 12 --- O=6 --- H=8 --- N=5 19
- 13 --- O=6 --- H=8 --- N=5 19
- 14 --- O=6 --- H=8 --- C=3 17
- 15 --- O=6 --- N=5 --- C=3 14
- 17 --- M=4 --- N=5 --- C=3 12
- 18 --- M=4 --- N=5 --- O=6 15
- 19 --- M=4 --- N=5 --- O=6 15
- 20 --- M=4 --- N=5 --- O=6 15
- 21 --- A=1 --- J=1 --- O=6 8
- 22 --- S=1 --- O=6 --- O=6 13
- 23 --- T=2 --- O=6 --- O=6 14
- 24 --- T=2 --- O=6 --- C=3 11
- 25 --- H=8 --- O=6 --- C=3 17
- 26 --- H=8 --- O=6 --- C=3 17
- 27 --- H=8 --- O=6 --- K=2 16
- 28 --- H=8 --- H=8 --- K=2 18
and so on....
If we look at the Firstname column, we see that we start with the first letter of the first name and its value.
The letter gets repeated according to its value. We see T=2, so it gets repeated 2 times.
We then take the next letter of the first name and its value.
It again gets repeated according to its value.
This holds for the rest of the letters in the first name.
After we have reached the last letter of the first name, the process gets repeated starting again with the first letter of the first name.
The same also holds for the Middlename and Lastname colums.
The Total column is simply the addition of the letter values accross the row.
I stopped at the end of the last name as we could go on forever.
What I also need to do then is to be able to limit the number of rows that will be generated.
Once this has been done, I then need to be able to select any row and see its Total.
If I select then row 20, I want to be able to see the row number and the Total value for that row which would be 20 and Total of 15.
This is because sometimes I may not want the table printed out, but want only to be able to select a row in the table and display the row and Total valule
I would appreciate any help someone can give me on this.
Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
The Ultimate Solutions Center
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
December 8th, 2012, 8:11 pm
- Zealous
- Guru


- Joined: Apr 15, 2011
- Posts: 1195
- Loc: Sydney
- Status: Offline
- wpas
- Graduate


- Joined: Jul 12, 2010
- Posts: 214
- Loc: Canada
- Status: Offline
I do not think using a database would help.
The data is strictly dependant on the person's name which is going to change all the time so putting it in a database will do nothing but take up room.
The rows will be completely different when the name changes.
I do not want to store any information.
Also, there could be more than one user using the script at the same time which could create issues if a database is used.
The data is strictly dependant on the person's name which is going to change all the time so putting it in a database will do nothing but take up room.
The rows will be completely different when the name changes.
I do not want to store any information.
Also, there could be more than one user using the script at the same time which could create issues if a database is used.
http://www.schembrionics.com
The Ultimate Solutions Center
The Ultimate Solutions Center
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
OK ... Not sure what you ment by a table but I threw this together real quick to get the info into an array. It was the first stab I took at this it can be slow if the numbers in the name are large but the concept matches your table. It will at least set you in one direction.
EDIT:: Ok the code was correct but the table array would overwrite it self in the letters for the first/middle/last name were the same so I added a f-/m-/l- to the table array and this solved this issue.
EDIT:: Ok the code was correct but the table array would overwrite it self in the letters for the first/middle/last name were the same so I added a f-/m-/l- to the table array and this solved this issue.
PHP Code: [ Select ]
<?php
$ar = array('A' => 1,
'B' => 2,
'C' => 3,
'D' => 4,
'E' => 5,
'F' => 6,
'G' => 7,
'H' => 8,
'I' => 9,
'J' => 1,
'K' => 2,
'L' => 3,
'M' => 4,
'N' => 5,
'O' => 6,
'P' => 7,
'Q' => 8,
'R' => 9,
'S' => 1,
'T' => 2,
'U' => 3,
'V' => 4,
'W' => 5,
'X' => 6,
'Y' => 7,
'Z' => 8
);
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
// Make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
// Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
// Setup arrays to hold the values
$fname = array();
$mname = array();
$lname = array();
// Now loop through each name one by one to get the values of each letter in the name
// First name
for($i=0; $i<$lenfn; $i++) {
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
// Loop for letter value
for($j=0; $j<$letternumberfn; $j++) {
// Add to the array
$fname[] = array($alphafn => $letternumberfn);
}
}
// Middle name
for($i=0; $i<$lenmn; $i++) {
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
// Loop for letter value
for($j=0; $j<$letternumbermn; $j++) {
// Add to the array
$mname[] = array($alphamn => $letternumbermn);
}
}
// Last name
for($i=0; $i<$lenln; $i++) {
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
// Loop for letter value
for($j=0; $j<$letternumberln; $j++) {
// Add to the array
$lname[] = array($alphaln => $letternumberln);
}
}
// Setup counters
$counters = array(
"fn" => 0,
"mn" => 0,
"ln" => 0
);
// Make a table array
$table = array();
// Loop again
for($i=0; $i<max(array(count($fname), count($mname), count($lname))); $i++) {
// Check the counter vs array count
if($counters['fn'] == count($fname)) {
// Reset the counter
$counters['fn'] = 0;
}
// Check the counter vs array count
if($counters['mn'] == count($mname)) {
// Reset the counter
$counters['mn'] = 0;
}
// Check the counter vs array count
if($counters['ln'] == count($lname)) {
// Reset the counter
$counters['ln'] = 0;
}
// Add to the table array
$table[] = array(
'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
// Add to the counters
$counters['fn']++;
$counters['mn']++;
$counters['ln']++;
}
print_r($table);
?>
$ar = array('A' => 1,
'B' => 2,
'C' => 3,
'D' => 4,
'E' => 5,
'F' => 6,
'G' => 7,
'H' => 8,
'I' => 9,
'J' => 1,
'K' => 2,
'L' => 3,
'M' => 4,
'N' => 5,
'O' => 6,
'P' => 7,
'Q' => 8,
'R' => 9,
'S' => 1,
'T' => 2,
'U' => 3,
'V' => 4,
'W' => 5,
'X' => 6,
'Y' => 7,
'Z' => 8
);
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
// Make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
// Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
// Setup arrays to hold the values
$fname = array();
$mname = array();
$lname = array();
// Now loop through each name one by one to get the values of each letter in the name
// First name
for($i=0; $i<$lenfn; $i++) {
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
// Loop for letter value
for($j=0; $j<$letternumberfn; $j++) {
// Add to the array
$fname[] = array($alphafn => $letternumberfn);
}
}
// Middle name
for($i=0; $i<$lenmn; $i++) {
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
// Loop for letter value
for($j=0; $j<$letternumbermn; $j++) {
// Add to the array
$mname[] = array($alphamn => $letternumbermn);
}
}
// Last name
for($i=0; $i<$lenln; $i++) {
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
// Loop for letter value
for($j=0; $j<$letternumberln; $j++) {
// Add to the array
$lname[] = array($alphaln => $letternumberln);
}
}
// Setup counters
$counters = array(
"fn" => 0,
"mn" => 0,
"ln" => 0
);
// Make a table array
$table = array();
// Loop again
for($i=0; $i<max(array(count($fname), count($mname), count($lname))); $i++) {
// Check the counter vs array count
if($counters['fn'] == count($fname)) {
// Reset the counter
$counters['fn'] = 0;
}
// Check the counter vs array count
if($counters['mn'] == count($mname)) {
// Reset the counter
$counters['mn'] = 0;
}
// Check the counter vs array count
if($counters['ln'] == count($lname)) {
// Reset the counter
$counters['ln'] = 0;
}
// Add to the table array
$table[] = array(
'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
// Add to the counters
$counters['fn']++;
$counters['mn']++;
$counters['ln']++;
}
print_r($table);
?>
- <?php
- $ar = array('A' => 1,
- 'B' => 2,
- 'C' => 3,
- 'D' => 4,
- 'E' => 5,
- 'F' => 6,
- 'G' => 7,
- 'H' => 8,
- 'I' => 9,
- 'J' => 1,
- 'K' => 2,
- 'L' => 3,
- 'M' => 4,
- 'N' => 5,
- 'O' => 6,
- 'P' => 7,
- 'Q' => 8,
- 'R' => 9,
- 'S' => 1,
- 'T' => 2,
- 'U' => 3,
- 'V' => 4,
- 'W' => 5,
- 'X' => 6,
- 'Y' => 7,
- 'Z' => 8
- );
- $firstname = "Thomas";
- $middlename = "John";
- $lastname = "Hancock";
- // Make all names upper case. to avoid messing with small letters.
- $firstname = strtoupper($firstname);
- $middlename = strtoupper($middlename);
- $lastname = strtoupper($lastname);
- // Find the number of letters in each name
- $lenfn = strlen($firstname);
- $lenmn = strlen($middlename);
- $lenln = strlen($lastname);
- // Setup arrays to hold the values
- $fname = array();
- $mname = array();
- $lname = array();
- // Now loop through each name one by one to get the values of each letter in the name
- // First name
- for($i=0; $i<$lenfn; $i++) {
- $alphafn = $firstname[$i];
- $letternumberfn = $ar[$alphafn];
- // Loop for letter value
- for($j=0; $j<$letternumberfn; $j++) {
- // Add to the array
- $fname[] = array($alphafn => $letternumberfn);
- }
- }
- // Middle name
- for($i=0; $i<$lenmn; $i++) {
- $alphamn = $middlename[$i];
- $letternumbermn = $ar[$alphamn];
- // Loop for letter value
- for($j=0; $j<$letternumbermn; $j++) {
- // Add to the array
- $mname[] = array($alphamn => $letternumbermn);
- }
- }
- // Last name
- for($i=0; $i<$lenln; $i++) {
- $alphaln = $lastname[$i];
- $letternumberln = $ar[$alphaln];
- // Loop for letter value
- for($j=0; $j<$letternumberln; $j++) {
- // Add to the array
- $lname[] = array($alphaln => $letternumberln);
- }
- }
- // Setup counters
- $counters = array(
- "fn" => 0,
- "mn" => 0,
- "ln" => 0
- );
- // Make a table array
- $table = array();
- // Loop again
- for($i=0; $i<max(array(count($fname), count($mname), count($lname))); $i++) {
- // Check the counter vs array count
- if($counters['fn'] == count($fname)) {
- // Reset the counter
- $counters['fn'] = 0;
- }
- // Check the counter vs array count
- if($counters['mn'] == count($mname)) {
- // Reset the counter
- $counters['mn'] = 0;
- }
- // Check the counter vs array count
- if($counters['ln'] == count($lname)) {
- // Reset the counter
- $counters['ln'] = 0;
- }
- // Add to the table array
- $table[] = array(
- 'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
- 'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
- 'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
- "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
- );
- // Add to the counters
- $counters['fn']++;
- $counters['mn']++;
- $counters['ln']++;
- }
- print_r($table);
- ?>
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
I read your post through again and I turn it into a function with a limiter so you can limit how many records are returned from the function.
PHP Code: [ Select ]
<?php
function name_counter($firstname, $middlename, $lastname, $limit = false) {
$ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8
);
// Make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
// Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
// Setup arrays to hold the values
$fname = array();
$mname = array();
$lname = array();
// Now loop through each name one by one to get the values of each letter in the name
// First name
for($i=0; $i<$lenfn; $i++) {
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
// Loop for letter value
for($j=0; $j<$letternumberfn; $j++) {
// Add to the array
$fname[] = array($alphafn => $letternumberfn);
}
}
// Middle name
for($i=0; $i<$lenmn; $i++) {
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
// Loop for letter value
for($j=0; $j<$letternumbermn; $j++) {
// Add to the array
$mname[] = array($alphamn => $letternumbermn);
}
}
// Last name
for($i=0; $i<$lenln; $i++) {
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
// Loop for letter value
for($j=0; $j<$letternumberln; $j++) {
// Add to the array
$lname[] = array($alphaln => $letternumberln);
}
}
// Setup counters
$counters = array(
"fn" => 0,
"mn" => 0,
"ln" => 0
);
// Make a table array
$table = array();
// Setup the limit
$limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
// Loop again
for($i=0; $i<$limit; $i++) {
// Check the counter vs array count
if($counters['fn'] == count($fname)) {
// Reset the counter
$counters['fn'] = 0;
}
// Check the counter vs array count
if($counters['mn'] == count($mname)) {
// Reset the counter
$counters['mn'] = 0;
}
// Check the counter vs array count
if($counters['ln'] == count($lname)) {
// Reset the counter
$counters['ln'] = 0;
}
// Add to the table array
$table[] = array(
'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
// Add to the counters
$counters['fn']++;
$counters['mn']++;
$counters['ln']++;
}
// Return the table
return $table;
}
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
//$table1 = name_counter($firstname, $middlename, $lastname);
$table2 = name_counter($firstname, $middlename, $lastname, 20);
// Record 1
echo $table2[0]['total'] . '<br />';
// Record 20
echo $table2[19]['total'] . '<br />';
echo '<pre>';
print_r($table2);
echo '</pre>';
?>
function name_counter($firstname, $middlename, $lastname, $limit = false) {
$ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8
);
// Make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
// Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
// Setup arrays to hold the values
$fname = array();
$mname = array();
$lname = array();
// Now loop through each name one by one to get the values of each letter in the name
// First name
for($i=0; $i<$lenfn; $i++) {
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
// Loop for letter value
for($j=0; $j<$letternumberfn; $j++) {
// Add to the array
$fname[] = array($alphafn => $letternumberfn);
}
}
// Middle name
for($i=0; $i<$lenmn; $i++) {
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
// Loop for letter value
for($j=0; $j<$letternumbermn; $j++) {
// Add to the array
$mname[] = array($alphamn => $letternumbermn);
}
}
// Last name
for($i=0; $i<$lenln; $i++) {
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
// Loop for letter value
for($j=0; $j<$letternumberln; $j++) {
// Add to the array
$lname[] = array($alphaln => $letternumberln);
}
}
// Setup counters
$counters = array(
"fn" => 0,
"mn" => 0,
"ln" => 0
);
// Make a table array
$table = array();
// Setup the limit
$limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
// Loop again
for($i=0; $i<$limit; $i++) {
// Check the counter vs array count
if($counters['fn'] == count($fname)) {
// Reset the counter
$counters['fn'] = 0;
}
// Check the counter vs array count
if($counters['mn'] == count($mname)) {
// Reset the counter
$counters['mn'] = 0;
}
// Check the counter vs array count
if($counters['ln'] == count($lname)) {
// Reset the counter
$counters['ln'] = 0;
}
// Add to the table array
$table[] = array(
'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
// Add to the counters
$counters['fn']++;
$counters['mn']++;
$counters['ln']++;
}
// Return the table
return $table;
}
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
//$table1 = name_counter($firstname, $middlename, $lastname);
$table2 = name_counter($firstname, $middlename, $lastname, 20);
// Record 1
echo $table2[0]['total'] . '<br />';
// Record 20
echo $table2[19]['total'] . '<br />';
echo '<pre>';
print_r($table2);
echo '</pre>';
?>
- <?php
- function name_counter($firstname, $middlename, $lastname, $limit = false) {
- $ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8
- );
- // Make all names upper case. to avoid messing with small letters.
- $firstname = strtoupper($firstname);
- $middlename = strtoupper($middlename);
- $lastname = strtoupper($lastname);
- // Find the number of letters in each name
- $lenfn = strlen($firstname);
- $lenmn = strlen($middlename);
- $lenln = strlen($lastname);
- // Setup arrays to hold the values
- $fname = array();
- $mname = array();
- $lname = array();
- // Now loop through each name one by one to get the values of each letter in the name
- // First name
- for($i=0; $i<$lenfn; $i++) {
- $alphafn = $firstname[$i];
- $letternumberfn = $ar[$alphafn];
- // Loop for letter value
- for($j=0; $j<$letternumberfn; $j++) {
- // Add to the array
- $fname[] = array($alphafn => $letternumberfn);
- }
- }
- // Middle name
- for($i=0; $i<$lenmn; $i++) {
- $alphamn = $middlename[$i];
- $letternumbermn = $ar[$alphamn];
- // Loop for letter value
- for($j=0; $j<$letternumbermn; $j++) {
- // Add to the array
- $mname[] = array($alphamn => $letternumbermn);
- }
- }
- // Last name
- for($i=0; $i<$lenln; $i++) {
- $alphaln = $lastname[$i];
- $letternumberln = $ar[$alphaln];
- // Loop for letter value
- for($j=0; $j<$letternumberln; $j++) {
- // Add to the array
- $lname[] = array($alphaln => $letternumberln);
- }
- }
- // Setup counters
- $counters = array(
- "fn" => 0,
- "mn" => 0,
- "ln" => 0
- );
- // Make a table array
- $table = array();
- // Setup the limit
- $limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
- // Loop again
- for($i=0; $i<$limit; $i++) {
- // Check the counter vs array count
- if($counters['fn'] == count($fname)) {
- // Reset the counter
- $counters['fn'] = 0;
- }
- // Check the counter vs array count
- if($counters['mn'] == count($mname)) {
- // Reset the counter
- $counters['mn'] = 0;
- }
- // Check the counter vs array count
- if($counters['ln'] == count($lname)) {
- // Reset the counter
- $counters['ln'] = 0;
- }
- // Add to the table array
- $table[] = array(
- 'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
- 'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
- 'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
- "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
- );
- // Add to the counters
- $counters['fn']++;
- $counters['mn']++;
- $counters['ln']++;
- }
- // Return the table
- return $table;
- }
- $firstname = "Thomas";
- $middlename = "John";
- $lastname = "Hancock";
- //$table1 = name_counter($firstname, $middlename, $lastname);
- $table2 = name_counter($firstname, $middlename, $lastname, 20);
- // Record 1
- echo $table2[0]['total'] . '<br />';
- // Record 20
- echo $table2[19]['total'] . '<br />';
- echo '<pre>';
- print_r($table2);
- echo '</pre>';
- ?>
- wpas
- Graduate


- Joined: Jul 12, 2010
- Posts: 214
- Loc: Canada
- Status: Offline
Hi ScottG
Great Work!
If I use
echo $table2[0]['total']
I get Total of 11 which is good
How would I also show the letters in the row
The key seems dependant on the letters of the first, middle and lastname which I would not know for the other rows
What I would like then is to be able to get the following for each row, using row 0 as example:
T, J, H, 11
Thanks
Great Work!
If I use
echo $table2[0]['total']
I get Total of 11 which is good
How would I also show the letters in the row
The key seems dependant on the letters of the first, middle and lastname which I would not know for the other rows
What I would like then is to be able to get the following for each row, using row 0 as example:
T, J, H, 11
Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
The Ultimate Solutions Center
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
With the current way the function is written the solution to that wouldn't be easy However making a quick change to the for loop in the function that builds the out put array would make it easier
Change
// Add to the table array
$table[] = array(
'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
To
Then you will be able to use it like
Change
PHP Code: [ Select ]
// Add to the table array
$table[] = array(
'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
- // Add to the table array
- $table[] = array(
- 'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
- 'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
- 'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
- "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
- );
To
PHP Code: [ Select ]
// Add to the table array
$table[] = array(
'first_name' => array("letter" => key($fname[$counters['fn']]), "value" => $fname[$counters['fn']][key($fname[$counters['fn']])]),
'middle_name' => array("letter" => key($mname[$counters['mn']]), "value" => $mname[$counters['mn']][key($mname[$counters['mn']])]),
'last_name' => array("letter" => key($lname[$counters['ln']]), "value" => $lname[$counters['ln']][key($lname[$counters['ln']])]),
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
$table[] = array(
'first_name' => array("letter" => key($fname[$counters['fn']]), "value" => $fname[$counters['fn']][key($fname[$counters['fn']])]),
'middle_name' => array("letter" => key($mname[$counters['mn']]), "value" => $mname[$counters['mn']][key($mname[$counters['mn']])]),
'last_name' => array("letter" => key($lname[$counters['ln']]), "value" => $lname[$counters['ln']][key($lname[$counters['ln']])]),
"total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
);
- // Add to the table array
- $table[] = array(
- 'first_name' => array("letter" => key($fname[$counters['fn']]), "value" => $fname[$counters['fn']][key($fname[$counters['fn']])]),
- 'middle_name' => array("letter" => key($mname[$counters['mn']]), "value" => $mname[$counters['mn']][key($mname[$counters['mn']])]),
- 'last_name' => array("letter" => key($lname[$counters['ln']]), "value" => $lname[$counters['ln']][key($lname[$counters['ln']])]),
- "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
- );
Then you will be able to use it like
PHP Code: [ Select ]
// Record 1
echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
// Record 1
echo $table2[0]['first_name']['value'] .','. $table2[0]['middle_name']['value'] .','. $table2[0]['last_name']['value'] .','. $table2[0]['total']. '<br />';
echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
// Record 1
echo $table2[0]['first_name']['value'] .','. $table2[0]['middle_name']['value'] .','. $table2[0]['last_name']['value'] .','. $table2[0]['total']. '<br />';
- // Record 1
- echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
- // Record 1
- echo $table2[0]['first_name']['value'] .','. $table2[0]['middle_name']['value'] .','. $table2[0]['last_name']['value'] .','. $table2[0]['total']. '<br />';
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
If going that direction it could be simplified more by changing the fname, mname and lname arrays as well as the table array
PHP Code: [ Select ]
<?php
function name_counter($firstname, $middlename, $lastname, $limit = false) {
$ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8
);
// Make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
// Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
// Setup arrays to hold the values
$fname = array();
$mname = array();
$lname = array();
// Now loop through each name one by one to get the values of each letter in the name
// First name
for($i=0; $i<$lenfn; $i++) {
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
// Loop for letter value
for($j=0; $j<$letternumberfn; $j++) {
// Add to the array
$fname[] = array(
"letter" => $alphafn,
"value" => $letternumberfn
);
}
}
// Middle name
for($i=0; $i<$lenmn; $i++) {
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
// Loop for letter value
for($j=0; $j<$letternumbermn; $j++) {
// Add to the array
$mname[] = array(
"letter" => $alphamn,
"value" => $letternumbermn
);
}
}
// Last name
for($i=0; $i<$lenln; $i++) {
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
// Loop for letter value
for($j=0; $j<$letternumberln; $j++) {
// Add to the array
$lname[] = array(
"letter" => $alphaln,
"value" => $letternumberln
);
}
}
// Setup counters
$counters = array(
"fn" => 0,
"mn" => 0,
"ln" => 0
);
// Make a table array
$table = array();
// Setup the limit
$limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
// Loop again
for($i=0; $i<$limit; $i++) {
// Check the counter vs array count
if($counters['fn'] == count($fname)) {
// Reset the counter
$counters['fn'] = 0;
}
// Check the counter vs array count
if($counters['mn'] == count($mname)) {
// Reset the counter
$counters['mn'] = 0;
}
// Check the counter vs array count
if($counters['ln'] == count($lname)) {
// Reset the counter
$counters['ln'] = 0;
}
// Add to the table array
$table[] = array(
'first_name' => $fname[$counters['fn']],
'middle_name' => $mname[$counters['mn']],
'last_name' => $lname[$counters['ln']],
"total" => ($fname[$counters['fn']]['value'] + $mname[$counters['mn']]['value'] + $lname[$counters['ln']]['value'])
);
// Add to the counters
$counters['fn']++;
$counters['mn']++;
$counters['ln']++;
}
// Return the table
return $table;
}
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
//$table1 = name_counter($firstname, $middlename, $lastname);
$table2 = name_counter($firstname, $middlename, $lastname, 20);
// Record 1
echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
// Record 20
echo $table2[19]['total'] . '<br />';
echo '<pre>';
print_r($table2);
echo '</pre>';
?>
function name_counter($firstname, $middlename, $lastname, $limit = false) {
$ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8
);
// Make all names upper case. to avoid messing with small letters.
$firstname = strtoupper($firstname);
$middlename = strtoupper($middlename);
$lastname = strtoupper($lastname);
// Find the number of letters in each name
$lenfn = strlen($firstname);
$lenmn = strlen($middlename);
$lenln = strlen($lastname);
// Setup arrays to hold the values
$fname = array();
$mname = array();
$lname = array();
// Now loop through each name one by one to get the values of each letter in the name
// First name
for($i=0; $i<$lenfn; $i++) {
$alphafn = $firstname[$i];
$letternumberfn = $ar[$alphafn];
// Loop for letter value
for($j=0; $j<$letternumberfn; $j++) {
// Add to the array
$fname[] = array(
"letter" => $alphafn,
"value" => $letternumberfn
);
}
}
// Middle name
for($i=0; $i<$lenmn; $i++) {
$alphamn = $middlename[$i];
$letternumbermn = $ar[$alphamn];
// Loop for letter value
for($j=0; $j<$letternumbermn; $j++) {
// Add to the array
$mname[] = array(
"letter" => $alphamn,
"value" => $letternumbermn
);
}
}
// Last name
for($i=0; $i<$lenln; $i++) {
$alphaln = $lastname[$i];
$letternumberln = $ar[$alphaln];
// Loop for letter value
for($j=0; $j<$letternumberln; $j++) {
// Add to the array
$lname[] = array(
"letter" => $alphaln,
"value" => $letternumberln
);
}
}
// Setup counters
$counters = array(
"fn" => 0,
"mn" => 0,
"ln" => 0
);
// Make a table array
$table = array();
// Setup the limit
$limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
// Loop again
for($i=0; $i<$limit; $i++) {
// Check the counter vs array count
if($counters['fn'] == count($fname)) {
// Reset the counter
$counters['fn'] = 0;
}
// Check the counter vs array count
if($counters['mn'] == count($mname)) {
// Reset the counter
$counters['mn'] = 0;
}
// Check the counter vs array count
if($counters['ln'] == count($lname)) {
// Reset the counter
$counters['ln'] = 0;
}
// Add to the table array
$table[] = array(
'first_name' => $fname[$counters['fn']],
'middle_name' => $mname[$counters['mn']],
'last_name' => $lname[$counters['ln']],
"total" => ($fname[$counters['fn']]['value'] + $mname[$counters['mn']]['value'] + $lname[$counters['ln']]['value'])
);
// Add to the counters
$counters['fn']++;
$counters['mn']++;
$counters['ln']++;
}
// Return the table
return $table;
}
$firstname = "Thomas";
$middlename = "John";
$lastname = "Hancock";
//$table1 = name_counter($firstname, $middlename, $lastname);
$table2 = name_counter($firstname, $middlename, $lastname, 20);
// Record 1
echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
// Record 20
echo $table2[19]['total'] . '<br />';
echo '<pre>';
print_r($table2);
echo '</pre>';
?>
- <?php
- function name_counter($firstname, $middlename, $lastname, $limit = false) {
- $ar = array('A' => 1,'B' => 2,'C' => 3,'D' => 4,'E' => 5,'F' => 6,'G' => 7,'H' => 8,'I' => 9,'J' => 1,'K' => 2,'L' => 3,'M' => 4,'N' => 5,'O' => 6,'P' => 7,'Q' => 8,'R' => 9,'S' => 1,'T' => 2,'U' => 3,'V' => 4,'W' => 5,'X' => 6,'Y' => 7,'Z' => 8
- );
- // Make all names upper case. to avoid messing with small letters.
- $firstname = strtoupper($firstname);
- $middlename = strtoupper($middlename);
- $lastname = strtoupper($lastname);
- // Find the number of letters in each name
- $lenfn = strlen($firstname);
- $lenmn = strlen($middlename);
- $lenln = strlen($lastname);
- // Setup arrays to hold the values
- $fname = array();
- $mname = array();
- $lname = array();
- // Now loop through each name one by one to get the values of each letter in the name
- // First name
- for($i=0; $i<$lenfn; $i++) {
- $alphafn = $firstname[$i];
- $letternumberfn = $ar[$alphafn];
- // Loop for letter value
- for($j=0; $j<$letternumberfn; $j++) {
- // Add to the array
- $fname[] = array(
- "letter" => $alphafn,
- "value" => $letternumberfn
- );
- }
- }
- // Middle name
- for($i=0; $i<$lenmn; $i++) {
- $alphamn = $middlename[$i];
- $letternumbermn = $ar[$alphamn];
- // Loop for letter value
- for($j=0; $j<$letternumbermn; $j++) {
- // Add to the array
- $mname[] = array(
- "letter" => $alphamn,
- "value" => $letternumbermn
- );
- }
- }
- // Last name
- for($i=0; $i<$lenln; $i++) {
- $alphaln = $lastname[$i];
- $letternumberln = $ar[$alphaln];
- // Loop for letter value
- for($j=0; $j<$letternumberln; $j++) {
- // Add to the array
- $lname[] = array(
- "letter" => $alphaln,
- "value" => $letternumberln
- );
- }
- }
- // Setup counters
- $counters = array(
- "fn" => 0,
- "mn" => 0,
- "ln" => 0
- );
- // Make a table array
- $table = array();
- // Setup the limit
- $limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
- // Loop again
- for($i=0; $i<$limit; $i++) {
- // Check the counter vs array count
- if($counters['fn'] == count($fname)) {
- // Reset the counter
- $counters['fn'] = 0;
- }
- // Check the counter vs array count
- if($counters['mn'] == count($mname)) {
- // Reset the counter
- $counters['mn'] = 0;
- }
- // Check the counter vs array count
- if($counters['ln'] == count($lname)) {
- // Reset the counter
- $counters['ln'] = 0;
- }
- // Add to the table array
- $table[] = array(
- 'first_name' => $fname[$counters['fn']],
- 'middle_name' => $mname[$counters['mn']],
- 'last_name' => $lname[$counters['ln']],
- "total" => ($fname[$counters['fn']]['value'] + $mname[$counters['mn']]['value'] + $lname[$counters['ln']]['value'])
- );
- // Add to the counters
- $counters['fn']++;
- $counters['mn']++;
- $counters['ln']++;
- }
- // Return the table
- return $table;
- }
- $firstname = "Thomas";
- $middlename = "John";
- $lastname = "Hancock";
- //$table1 = name_counter($firstname, $middlename, $lastname);
- $table2 = name_counter($firstname, $middlename, $lastname, 20);
- // Record 1
- echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
- // Record 20
- echo $table2[19]['total'] . '<br />';
- echo '<pre>';
- print_r($table2);
- echo '</pre>';
- ?>
- wpas
- Graduate


- Joined: Jul 12, 2010
- Posts: 214
- Loc: Canada
- Status: Offline
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
- wpas
- Graduate


- Joined: Jul 12, 2010
- Posts: 214
- Loc: Canada
- Status: Offline
Hi ScottG
I have been testing using firstname, middlename and lastname and it works perfectly.
I just noticed that if I use only firstname and lastname, no middlename, I get the following warning:
Warning: key() expects parameter 1 to be array, null given in /home/...
Looks like you must put in a middle name.
Can this be fixed so that if a middle name is not used, I do not get the warning.
Thanks
I have been testing using firstname, middlename and lastname and it works perfectly.
I just noticed that if I use only firstname and lastname, no middlename, I get the following warning:
Warning: key() expects parameter 1 to be array, null given in /home/...
Looks like you must put in a middle name.
Can this be fixed so that if a middle name is not used, I do not get the warning.
Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
The Ultimate Solutions Center
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
With the last post that I had posted the function I removed the use of key(). I tested with
And got no warning just notices. As far as the old code you could add
after the for loops the build those arrays this will make sure that them arrays have arrays in the this will stop the warnings but give you notices like the newer version of the function
PHP Code: [ Select ]
error_reporting(E_ALL);
$firstname = "Thomas";
$middlename = "";
$lastname = "Hancock";
$table2 = name_counter($firstname, $middlename, $lastname, 20);
$firstname = "Thomas";
$middlename = "";
$lastname = "Hancock";
$table2 = name_counter($firstname, $middlename, $lastname, 20);
- error_reporting(E_ALL);
- $firstname = "Thomas";
- $middlename = "";
- $lastname = "Hancock";
- $table2 = name_counter($firstname, $middlename, $lastname, 20);
And got no warning just notices. As far as the old code you could add
PHP Code: [ Select ]
// Check for arrays
$fname[0] = (is_array($fname[0])) ? $fname[0] : array();
$mname[0] = (is_array($mname[0])) ? $mname[0] : array();
$lname[0] = (is_array($lname[0])) ? $lname[0] : array();
$fname[0] = (is_array($fname[0])) ? $fname[0] : array();
$mname[0] = (is_array($mname[0])) ? $mname[0] : array();
$lname[0] = (is_array($lname[0])) ? $lname[0] : array();
- // Check for arrays
- $fname[0] = (is_array($fname[0])) ? $fname[0] : array();
- $mname[0] = (is_array($mname[0])) ? $mname[0] : array();
- $lname[0] = (is_array($lname[0])) ? $lname[0] : array();
after the for loops the build those arrays this will make sure that them arrays have arrays in the this will stop the warnings but give you notices like the newer version of the function
- wpas
- Graduate


- Joined: Jul 12, 2010
- Posts: 214
- Loc: Canada
- Status: Offline
- ScottG
- Proficient


- Joined: Jul 06, 2010
- Posts: 266
- Status: Offline
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
December 11th, 2012, 3:50 pm
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 15 posts
- Users browsing this forum: Kurthead+1 and 128 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
