Help with PHP script

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 323
  • Loc: Canada

Post 3+ Months Ago

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:

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 />";
}

?>
  1. <?php
  2. $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);
  3. $firstname = "Thomas";
  4. $middlename = "John";
  5. $lastname = "Hancock";
  6. //make all names upper case. to avoid messing with small letters.
  7. $firstname = strtoupper($firstname);
  8. $middlename = strtoupper($middlename);
  9. $lastname = strtoupper($lastname);
  10. //Find the number of letters in each name
  11. $lenfn = strlen($firstname);
  12. $lenmn = strlen($middlename);
  13. $lenln = strlen($lastname);
  14. //now loop through each name one by one to get the values of each letter in the name
  15. //first name
  16. for($i=0; $i<$lenfn; $i++)
  17. {
  18. $alphafn = $firstname[$i];
  19. $letternumberfn = $ar[$alphafn];
  20. echo $alphafn." = ".$letternumberfn."<br />";
  21. }
  22. echo "<br />";
  23. //middle name
  24. for($i=0; $i<$lenmn; $i++)
  25. {
  26. $alphamn = $middlename[$i];
  27. $letternumbermn = $ar[$alphamn];
  28. echo $alphamn." = ".$letternumbermn."<br />";
  29. }
  30. echo "<br />";
  31. //last name
  32. for($i=0; $i<$lenln; $i++)
  33. {
  34. $alphaln = $lastname[$i];
  35. $letternumberln = $ar[$alphaln];
  36. echo $alphaln." = ".$letternumberln."<br />";
  37. }
  38. ?>


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
  1. Row Firstname  Middlename Lastname Total
  2. 0 ---  T=2 ---    J=1 ---    H=8    11
  3. 1 ---  T=2 ---    O=6 ---    H=8    16
  4. 2 ---  H=8 ---    0=6 ---    H=8    22
  5. 3 ---  H=8 ---    0=6 ---    H=8    22
  6. 4 ---  H=8 ---    0=6 ---    H=8    22
  7. 5 ---  H=8 ---    0=6 ---    H=8    22
  8. 6 ---  H=8 ---    0=6 ---    H=8    22
  9. 7 ---  H=8 ---    H=8 ---    H=8    24
  10. 8 ---  H=8 ---    H=8 ---    A=1    17
  11. 9 ---  H=8 ---    H=8 ---    N=5    21
  12. 10 --- O=6 ---    H=8 ---    N=5    19
  13. 11 --- O=6 ---    H=8 ---    N=5    19
  14. 12 --- O=6 ---    H=8 ---    N=5    19
  15. 13 --- O=6 ---    H=8 ---    N=5    19
  16. 14 --- O=6 ---    H=8 ---    C=3    17   
  17. 15 --- O=6 ---    N=5 ---    C=3    14
  18. 17 --- M=4 ---    N=5 ---    C=3    12
  19. 18 --- M=4 ---    N=5 ---    O=6    15
  20. 19 --- M=4 ---    N=5 ---    O=6    15
  21. 20 --- M=4 ---    N=5 ---    O=6    15
  22. 21 --- A=1 ---    J=1 ---    O=6    8
  23. 22 --- S=1 ---    O=6 ---    O=6    13
  24. 23 --- T=2 ---    O=6 ---    O=6    14
  25. 24 --- T=2 ---    O=6 ---    C=3    11
  26. 25 --- H=8 ---    O=6 ---    C=3    17
  27. 26 --- H=8 ---    O=6 ---    C=3    17
  28. 27 --- H=8 ---    O=6 ---    K=2    16
  29. 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
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Would using MySQL database make any difference. Insert your data into SQL then add your math.

even using Excel then exporting to SQL to then get your math on top of that.

If your going to be using lots of data then i think using database may be the way to go.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 323
  • Loc: Canada

Post 3+ Months Ago

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.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

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.

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);
 
?>
 
  1. <?php
  2.  
  3. $ar = array('A' => 1,
  4.          'B' => 2,
  5.          'C' => 3,
  6.          'D' => 4,
  7.          'E' => 5,
  8.          'F' => 6,
  9.          'G' => 7,
  10.          'H' => 8,
  11.          'I' => 9,
  12.          'J' => 1,
  13.          'K' => 2,
  14.          'L' => 3,
  15.          'M' => 4,
  16.          'N' => 5,
  17.          'O' => 6,
  18.          'P' => 7,
  19.          'Q' => 8,
  20.          'R' => 9,
  21.          'S' => 1,
  22.          'T' => 2,
  23.          'U' => 3,
  24.          'V' => 4,
  25.          'W' => 5,
  26.          'X' => 6,
  27.          'Y' => 7,
  28.          'Z' => 8
  29. );
  30.  
  31. $firstname = "Thomas";
  32. $middlename = "John";
  33. $lastname = "Hancock";
  34.  
  35. // Make all names upper case. to avoid messing with small letters.
  36. $firstname = strtoupper($firstname);
  37. $middlename = strtoupper($middlename);
  38. $lastname = strtoupper($lastname);
  39.  
  40. // Find the number of letters in each name
  41. $lenfn = strlen($firstname);
  42. $lenmn = strlen($middlename);
  43. $lenln = strlen($lastname);
  44.  
  45. // Setup arrays to hold the values
  46. $fname = array();
  47. $mname = array();
  48. $lname = array();
  49.  
  50. // Now loop through each name one by one to get the values of each letter in the name
  51. // First name
  52. for($i=0; $i<$lenfn; $i++) {
  53.    
  54.    $alphafn = $firstname[$i];
  55.    $letternumberfn = $ar[$alphafn];
  56.    
  57.    // Loop for letter value
  58.    for($j=0; $j<$letternumberfn; $j++) {
  59.    
  60.       // Add to the array
  61.       $fname[] = array($alphafn => $letternumberfn);
  62.    
  63.    }
  64.  
  65. }
  66.  
  67. // Middle name
  68. for($i=0; $i<$lenmn; $i++) {
  69.  
  70.    $alphamn = $middlename[$i];
  71.    $letternumbermn = $ar[$alphamn];
  72.    
  73.    // Loop for letter value
  74.    for($j=0; $j<$letternumbermn; $j++) {
  75.    
  76.       // Add to the array
  77.       $mname[] = array($alphamn => $letternumbermn);
  78.    
  79.    }
  80.  
  81. }
  82.  
  83. // Last name
  84. for($i=0; $i<$lenln; $i++) {
  85.  
  86.    $alphaln = $lastname[$i];
  87.    $letternumberln = $ar[$alphaln];
  88.    
  89.    // Loop for letter value
  90.    for($j=0; $j<$letternumberln; $j++) {
  91.    
  92.       // Add to the array
  93.       $lname[] = array($alphaln => $letternumberln);
  94.    
  95.    }
  96.  
  97. }
  98.  
  99. // Setup counters
  100. $counters = array(
  101.          "fn" => 0,
  102.          "mn" => 0,
  103.          "ln" => 0
  104.          );
  105.  
  106. // Make a table array
  107. $table = array();
  108.  
  109. // Loop again
  110. for($i=0; $i<max(array(count($fname), count($mname), count($lname))); $i++) {
  111.    
  112.    // Check the counter vs array count
  113.    if($counters['fn'] == count($fname)) {
  114.      
  115.       // Reset the counter
  116.       $counters['fn'] = 0;
  117.      
  118.    }
  119.    
  120.    // Check the counter vs array count
  121.    if($counters['mn'] == count($mname)) {
  122.      
  123.       // Reset the counter
  124.       $counters['mn'] = 0;
  125.      
  126.    }
  127.    
  128.    // Check the counter vs array count
  129.    if($counters['ln'] == count($lname)) {
  130.      
  131.       // Reset the counter
  132.       $counters['ln'] = 0;
  133.      
  134.    }
  135.    
  136.    // Add to the table array
  137.    $table[] = array(
  138.          'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
  139.          'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
  140.          'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
  141.          "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
  142.          );
  143.    
  144.    
  145.    // Add to the counters
  146.    $counters['fn']++;
  147.    $counters['mn']++;
  148.    $counters['ln']++;
  149.    
  150. }
  151.  
  152. print_r($table);
  153.  
  154. ?>
  155.  
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

However after reviewing the code the total comes out correct but some of the columns are gone in the array. Im looking into that right now
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

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>';
 
?>
 
  1. <?php
  2.  
  3. function name_counter($firstname, $middlename, $lastname, $limit = false) {
  4.    
  5.    $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
  6. );
  7.    
  8.    // Make all names upper case. to avoid messing with small letters.
  9.    $firstname = strtoupper($firstname);
  10.    $middlename = strtoupper($middlename);
  11.    $lastname = strtoupper($lastname);
  12.    
  13.    // Find the number of letters in each name
  14.    $lenfn = strlen($firstname);
  15.    $lenmn = strlen($middlename);
  16.    $lenln = strlen($lastname);
  17.    
  18.    // Setup arrays to hold the values
  19.    $fname = array();
  20.    $mname = array();
  21.    $lname = array();
  22.    
  23.    // Now loop through each name one by one to get the values of each letter in the name
  24.    // First name
  25.    for($i=0; $i<$lenfn; $i++) {
  26.      
  27.       $alphafn = $firstname[$i];
  28.       $letternumberfn = $ar[$alphafn];
  29.      
  30.       // Loop for letter value
  31.       for($j=0; $j<$letternumberfn; $j++) {
  32.      
  33.          // Add to the array
  34.          $fname[] = array($alphafn => $letternumberfn);
  35.      
  36.       }
  37.    
  38.    }
  39.    
  40.    // Middle name
  41.    for($i=0; $i<$lenmn; $i++) {
  42.    
  43.       $alphamn = $middlename[$i];
  44.       $letternumbermn = $ar[$alphamn];
  45.      
  46.       // Loop for letter value
  47.       for($j=0; $j<$letternumbermn; $j++) {
  48.      
  49.          // Add to the array
  50.          $mname[] = array($alphamn => $letternumbermn);
  51.      
  52.       }
  53.    
  54.    }
  55.    
  56.    // Last name
  57.    for($i=0; $i<$lenln; $i++) {
  58.    
  59.       $alphaln = $lastname[$i];
  60.       $letternumberln = $ar[$alphaln];
  61.      
  62.       // Loop for letter value
  63.       for($j=0; $j<$letternumberln; $j++) {
  64.      
  65.          // Add to the array
  66.          $lname[] = array($alphaln => $letternumberln);
  67.      
  68.       }
  69.    
  70.    }
  71.    
  72.    // Setup counters
  73.    $counters = array(
  74.             "fn" => 0,
  75.             "mn" => 0,
  76.             "ln" => 0
  77.             );
  78.    
  79.    // Make a table array
  80.    $table = array();
  81.    
  82.    // Setup the limit
  83.    $limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
  84.    
  85.    // Loop again
  86.    for($i=0; $i<$limit; $i++) {
  87.      
  88.       // Check the counter vs array count
  89.       if($counters['fn'] == count($fname)) {
  90.          
  91.          // Reset the counter
  92.          $counters['fn'] = 0;
  93.          
  94.       }
  95.      
  96.       // Check the counter vs array count
  97.       if($counters['mn'] == count($mname)) {
  98.          
  99.          // Reset the counter
  100.          $counters['mn'] = 0;
  101.          
  102.       }
  103.      
  104.       // Check the counter vs array count
  105.       if($counters['ln'] == count($lname)) {
  106.          
  107.          // Reset the counter
  108.          $counters['ln'] = 0;
  109.          
  110.       }
  111.      
  112.       // Add to the table array
  113.       $table[] = array(
  114.             'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
  115.             'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
  116.             'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
  117.             "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
  118.             );
  119.      
  120.      
  121.       // Add to the counters
  122.       $counters['fn']++;
  123.       $counters['mn']++;
  124.       $counters['ln']++;
  125.      
  126.    }
  127.    
  128.    // Return the table
  129.    return $table;
  130.    
  131. }
  132.  
  133. $firstname = "Thomas";
  134. $middlename = "John";
  135. $lastname = "Hancock";
  136.  
  137. //$table1 = name_counter($firstname, $middlename, $lastname);
  138. $table2 = name_counter($firstname, $middlename, $lastname, 20);
  139.  
  140. // Record 1
  141. echo $table2[0]['total'] . '<br />';
  142.  
  143. // Record 20
  144. echo $table2[19]['total'] . '<br />';
  145.  
  146. echo '<pre>';
  147. print_r($table2);
  148. echo '</pre>';
  149.  
  150. ?>
  151.  
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 323
  • Loc: Canada

Post 3+ Months Ago

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
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

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
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']])])
            );
 
  1.  
  2.       // Add to the table array
  3.       $table[] = array(
  4.             'f-' . key($fname[$counters['fn']]) => $fname[$counters['fn']][key($fname[$counters['fn']])],
  5.             'm-' . key($mname[$counters['mn']]) => $mname[$counters['mn']][key($mname[$counters['mn']])],
  6.             'l-' .key($lname[$counters['ln']]) => $lname[$counters['ln']][key($lname[$counters['ln']])],
  7.             "total" => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
  8.             );
  9.  


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']])])
            );
 
  1. // Add to the table array
  2.       $table[] = array(
  3.             'first_name'   => array("letter" => key($fname[$counters['fn']]), "value" => $fname[$counters['fn']][key($fname[$counters['fn']])]),
  4.             'middle_name'  => array("letter" => key($mname[$counters['mn']]), "value" => $mname[$counters['mn']][key($mname[$counters['mn']])]),
  5.             'last_name'    => array("letter" => key($lname[$counters['ln']]), "value" => $lname[$counters['ln']][key($lname[$counters['ln']])]),
  6.             "total"     => ($fname[$counters['fn']][key($fname[$counters['fn']])] + $mname[$counters['mn']][key($mname[$counters['mn']])] + $lname[$counters['ln']][key($lname[$counters['ln']])])
  7.             );
  8.  


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 />';
 
  1. // Record 1
  2. echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
  3.  
  4. // Record 1
  5. echo $table2[0]['first_name']['value'] .','. $table2[0]['middle_name']['value'] .','. $table2[0]['last_name']['value'] .','. $table2[0]['total']. '<br />';
  6.  
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

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>';
 
?>
 
  1. <?php
  2.  
  3. function name_counter($firstname, $middlename, $lastname, $limit = false) {
  4.    
  5.    $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
  6. );
  7.    
  8.    // Make all names upper case. to avoid messing with small letters.
  9.    $firstname = strtoupper($firstname);
  10.    $middlename = strtoupper($middlename);
  11.    $lastname = strtoupper($lastname);
  12.    
  13.    // Find the number of letters in each name
  14.    $lenfn = strlen($firstname);
  15.    $lenmn = strlen($middlename);
  16.    $lenln = strlen($lastname);
  17.    
  18.    // Setup arrays to hold the values
  19.    $fname = array();
  20.    $mname = array();
  21.    $lname = array();
  22.    
  23.    // Now loop through each name one by one to get the values of each letter in the name
  24.    // First name
  25.    for($i=0; $i<$lenfn; $i++) {
  26.      
  27.       $alphafn = $firstname[$i];
  28.       $letternumberfn = $ar[$alphafn];
  29.      
  30.       // Loop for letter value
  31.       for($j=0; $j<$letternumberfn; $j++) {
  32.      
  33.          // Add to the array
  34.          $fname[] = array(
  35.                "letter" => $alphafn,
  36.                "value" => $letternumberfn
  37.                );
  38.      
  39.       }
  40.    
  41.    }
  42.    
  43.    // Middle name
  44.    for($i=0; $i<$lenmn; $i++) {
  45.    
  46.       $alphamn = $middlename[$i];
  47.       $letternumbermn = $ar[$alphamn];
  48.      
  49.       // Loop for letter value
  50.       for($j=0; $j<$letternumbermn; $j++) {
  51.      
  52.          // Add to the array
  53.          $mname[] = array(
  54.                "letter" => $alphamn,
  55.                "value" => $letternumbermn
  56.                );
  57.      
  58.       }
  59.    
  60.    }
  61.    
  62.    // Last name
  63.    for($i=0; $i<$lenln; $i++) {
  64.    
  65.       $alphaln = $lastname[$i];
  66.       $letternumberln = $ar[$alphaln];
  67.      
  68.       // Loop for letter value
  69.       for($j=0; $j<$letternumberln; $j++) {
  70.      
  71.          // Add to the array
  72.          $lname[] = array(
  73.                "letter" => $alphaln,
  74.                "value" => $letternumberln
  75.                );
  76.      
  77.       }
  78.    
  79.    }
  80.    
  81.    // Setup counters
  82.    $counters = array(
  83.             "fn" => 0,
  84.             "mn" => 0,
  85.             "ln" => 0
  86.             );
  87.    
  88.    // Make a table array
  89.    $table = array();
  90.    
  91.    // Setup the limit
  92.    $limit = ($limit) ? $limit : max(array(count($fname), count($mname), count($lname)));
  93.    
  94.    // Loop again
  95.    for($i=0; $i<$limit; $i++) {
  96.      
  97.       // Check the counter vs array count
  98.       if($counters['fn'] == count($fname)) {
  99.          
  100.          // Reset the counter
  101.          $counters['fn'] = 0;
  102.          
  103.       }
  104.      
  105.       // Check the counter vs array count
  106.       if($counters['mn'] == count($mname)) {
  107.          
  108.          // Reset the counter
  109.          $counters['mn'] = 0;
  110.          
  111.       }
  112.      
  113.       // Check the counter vs array count
  114.       if($counters['ln'] == count($lname)) {
  115.          
  116.          // Reset the counter
  117.          $counters['ln'] = 0;
  118.          
  119.       }
  120.      
  121.       // Add to the table array
  122.       $table[] = array(
  123.             'first_name'   => $fname[$counters['fn']],
  124.             'middle_name'  => $mname[$counters['mn']],
  125.             'last_name'    => $lname[$counters['ln']],
  126.             "total"     => ($fname[$counters['fn']]['value'] + $mname[$counters['mn']]['value'] + $lname[$counters['ln']]['value'])
  127.             );
  128.      
  129.      
  130.       // Add to the counters
  131.       $counters['fn']++;
  132.       $counters['mn']++;
  133.       $counters['ln']++;
  134.      
  135.    }
  136.    
  137.    // Return the table
  138.    return $table;
  139.    
  140. }
  141.  
  142. $firstname = "Thomas";
  143. $middlename = "John";
  144. $lastname = "Hancock";
  145.  
  146. //$table1 = name_counter($firstname, $middlename, $lastname);
  147. $table2 = name_counter($firstname, $middlename, $lastname, 20);
  148.  
  149. // Record 1
  150. echo $table2[0]['first_name']['letter'] .','. $table2[0]['middle_name']['letter'] .','. $table2[0]['last_name']['letter'] .','. $table2[0]['total']. '<br />';
  151.  
  152.  
  153. // Record 20
  154. echo $table2[19]['total'] . '<br />';
  155.  
  156. echo '<pre>';
  157. print_r($table2);
  158. echo '</pre>';
  159.  
  160. ?>
  161.  
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 323
  • Loc: Canada

Post 3+ Months Ago

Again, great stuff

You gave me what I wanted and read my mind.

Being able to get the value of each letter was actually something I was also thinking about.

Thanks for your great help.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

No problem
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 323
  • Loc: Canada

Post 3+ Months Ago

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
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

With the last post that I had posted the function I removed the use of key(). I tested with

PHP Code: [ Select ]
error_reporting(E_ALL);
 
$firstname = "Thomas";
$middlename = "";
$lastname = "Hancock";
 
$table2 = name_counter($firstname, $middlename, $lastname, 20);
 
  1. error_reporting(E_ALL);
  2.  
  3. $firstname = "Thomas";
  4. $middlename = "";
  5. $lastname = "Hancock";
  6.  
  7. $table2 = name_counter($firstname, $middlename, $lastname, 20);
  8.  


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();
 
  1. // Check for arrays
  2. $fname[0] = (is_array($fname[0])) ? $fname[0] : array();
  3. $mname[0] = (is_array($mname[0])) ? $mname[0] : array();
  4. $lname[0] = (is_array($lname[0])) ? $lname[0] : array();
  5.  


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
  • Proficient
  • Proficient
  • User avatar
  • Posts: 323
  • Loc: Canada

Post 3+ Months Ago

Hey, you are right

I went back and redid the code again.

I must have somehow got your first version and second version screwed up when I did the changes.

Works perfectly without middle name.

Thanks and sorry
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

No problem

Post Information

  • Total Posts in this topic: 15 posts
  • Users browsing this forum: No registered users and 123 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.