Hi All ...

I don't know if this is way too simple or just impossible. 😕 I haven't found any examples.

Is there a way to match row fields to a second array's keys and then output the second array's values?

A mysql db row was opened as an associative array (array1). Key/value examples: ("mon1b"/7.5, "mon1e"/9.45 ... etc.)

A second associative array (array2) has keys that reference the array1 values. Key/value examples: (7.0/"7:00", 7.25/"7:15" ... etc.)

I'd like to be able to open a $key in array1 and output the related value from array2. Example: (input "mon1b", output "7:30"). I hope this is clear. Thanks in advance for any help. 🙂

    I don't fully understand what your after but I think you could do something like this:

    //Get all field names for table 1
    $select = "select * from table_1";
    $result = mysql_query($select);
    $row = mysql_fetch_array($result);
    
    $field_names_table_1 = array();
    
    for($i = 0; $i < sizeof($row); $i++)
    {
    
     $field_names_table_1[$i] = mysql_field_name($result, $i);
    
    }
    
    //Get all field names for table 2
    $select = "select * from table_2";
    $result = mysql_query($select);
    $row = mysql_fetch_array($result);
    
    $field_names_table_2 = array();
    
    for($i = 0; $i < sizeof($row); $i++)
    {
    
     $field_names_table_2[$i] = mysql_field_name($result, $i);
    
    }
    
    //Do some code to compare of manipulate the fieldname arrays
    
    

    or you could:

    //Initialize an array to hold key=value pairs from row
    $table_data_1 = array();
    
    $select = "select * from table_1";
    $result = mysql_query($result);
    
    while($row = mysql_fetch_array($result))
    {
         for($i = 0; $i < sizeof($row); $i++)
         {
              $fieldname = mysql_field_name($row, $i);
              $table_data_1 = array_merge($table_data_1, array($fieldname => $row[$i]));
         }
    }
    

    Now $table_data_1 is an associative array with key = fieldname and value = to the actual field data

    Now Just repeat the same process for table 2 and change the variable names of course accordingly so that you end up with another associative array called $table_data_2

    Now you have should have 2 associative arrays with the keys = to fieldnames and values = to field data

    simply create a new associative array with the keys = to $table_data_1's keys and the values = to $table_data_2's values

    //create a pipe delimited string and an array to hold $table_data_1's keys
    $table_1_keys = array();
    $keys = "";
    
    //create a pipe delimited string and an array to hold $table_data_2's values 
    $table_2_values = array();
    $vals = "";
    
    foreach($table_data_1 as $key => $value)
    {
         $keys .= "|" . $key;
    }
    foreach($table_data_2 as $value)
    {
         $vals .= "|" . $value;
    }
    
    $table_1_keys = explode("|", $keys);
    $table_2_values = explode("|", $vals);
    
    //Initialize the new associative array
    $my_data = array();
    
    for($i = 0; $i < sizeof($table_1_$keys); $i++)
    {
         $the_key = $table_1_keys[$i];
         $the_value = $table_2_values[$i];
    
     if($i == 0)
     {
          $my_data = array($the_key => $the_value);
     }
     else
     {
          $my_data = array_merge($my_data, array($the_key => $the_value));
     }
    }
    

    ...and that should do it. Now you should end up with an associative array called $my_data that contains what you
    need. Keep in mind this is untested code. Hope there are no
    errors, but i have written similar code to this for doing nearly
    the same thing several times before and it worked for what
    I needed at the time.

    Robogenus

      Thank you Robogenus! I'll work with your suggestions and let you know. It'll probably take me a day or two since I'm very slow at all this. Thanks again. 🙂

        Your Welcome...I'm constantly learning too...

        Let me know if it works you might have to carefully step through
        the loops and arrays and watch what goes on by echoing the array.

        Robogenus

          Robogenus ...

          Thanks for your valiant efforts in trying to help me. Unfortunately, being a virtual novice, I couldn't understand how much of the code you wrote actually works. The (hopefully) attached file page demonstrates the very basic level of PHP at which I'm currently working without a lot of handholding.

          On other pages I use the "confhours" table data to calculate the sums and differences of entered times. I was hoping to use the same data to complete this page's information. (I'll probably use "IF" statements to display only the rows that are used.) The "$times" array was going to be the start of an attempt to help convert the times from float to char.

          Thanks again for your assistance. 🙂

            I looked at your php code and wrote in some new code around where you define the $time array. Check it out. Hope it works! You might need to change the fieldname in the select statement.

            I have included the newcode.txt file for you to see the changes.

            Robogenus

              Thanks again for all your help Robogenus. 🙂

              I'll need to review and work with your new code for another day ... or more ... . Sorry for being so slow with this.

                Robogenus ...

                Thanks for the assistance and all the handholding comments! They really help. 🙂 I tried to echo every variable and array to discover how your script works line by line.

                I got a little loopy (148 increments) with $i and learned how the incrementing 'FOR' operates.

                These are some of the results so far ... I'm still reviewing/testing the script.

                    '$my_time_data[x]' produces the same time value (7:00) for for every inserted key.
                
                    '$row' accurately returned all the keys.
                
                    '$keys' returned only the pipe but no keys.
                
                    '$vals' accurately returned all the pipe delimited times.
                
                    So far I'm unable to get '$fieldname', '$time_data' or '$the_key' to return a value.

                Thanks again for the help.

                
                $time_data = array();
                
                $sql = "SELECT mon1b,mon1e,mon1t,tue1b,tue1e,tue1t,wed1b,wed1e,wed1t,thu1b,thu1e,thu1t,fri1b,fri1e,fri1t,sat1b,sat1e,sat1t,mon2b,mon2e,mon2t,tue2b,tue2e,tue2t,wed2b,wed2e,wed2t,thu2b,thu2e,thu2t,fri2b,fri2e,fri2t,sat2b,sat2e,sat2t,mon3b,mon3e,mon3t,tue3b,tue3e,tue3t,wed3b,wed3e,wed3t,thu3b,thu3e,thu3t,fri3b,fri3e,fri3t,sat3b,sat3e,sat3t,mon4b,mon4e,mon4t,tue4b,tue4e,tue4t,wed4b,wed4e,wed4t,thu4b,thu4e,thu4t,fri4b,fri4e,fri4t,sat4b,sat4e,sat4t FROM confhours WHERE last_name = '$last' AND first_name = '$first'";
                
                $result = mysql_query($sql);
                
                while($row = mysql_fetch_array($result))
                  {
                
                        for($i = 0; $i < sizeof($row); $i++)
                
                        $fieldname = mysql_field_name($row, $i);
                        {
                
                                    if($i == 0)
                                    {
                                            $time_data = array($fieldname => $row[$i]);
                                    }
                                    if($fieldname != "first_name" or $fieldname != "last_name")
                                    {
                                            $time_data = array_merge($time_data, array($fieldname => $row[$i]));
                                    }
                         }
                  }
                
                $time = array ("7"=>"7:00", "7.25"=>"7:15", "7.5"=>"7:30", "7.75"=>"7:45", 
                "8"=>"8:00", "8.25"=>"8:15", "8.5"=>"8:30", "8.75"=>"8:45",
                 "9"=>"9:00", "9.25"=>"9:15", "9.5"=>"9:30", "9.75"=>"9:45", 
                "10"=>"10:00", "10.25"=>"10:15", "10.5"=>"10:30", 
                "10.75"=>"10:45", "11"=>"11:00", "11.25"=>"11:15", 
                "11.5"=>"11:30", "11.75"=>"11:45", "12"=>"12:00p", 
                "12.25"=>"12:15p", "12.5"=>"12:30p", "12.75"=>"12:45p", 
                "13"=>"1:00p", "13.25"=>"1:15p", "13.5"=>"1:30p", 
                "13.75"=>"1:45p", "14"=>"2:00p", "14.25"=>"2:15p",
                 "14.5"=>"2:30p", "14.75"=>"2:45p", "15"=>"3:00p", 
                "15.25"=>"3:15p", "15.5"=>"3:30p", "15.75"=>"3:45p", 
                "16"=>"4:00p", "16.25"=>"4:15p", "16.5"=>"4:30p",
                 "16.75"=>"4:45p", "17"=>"5:00p", "17.25"=>"5:15p", 
                "17.5"=>"5:30p", "17.75"=>"5:45p", "18"=>"6:00p", 
                "18.25"=>"6:15p", "18.5"=>"6:30p", "18.75"=>"6:45p",
                 "19"=>"7:00p", "19.25"=>"7:15p", "19.5"=>"7:30p",
                 "19.75"=>"7:45p", "20"=>"8:00p", "20.25"=>"8:15p", 
                "20.5"=>"8:30p", "20.75"=>"8:45p", "21"=>"9:00p", 
                "21.25"=>"9:15p", "21.5"=>"9:30p", "21.75"=>"9:45p",
                 "22"=>"10:00");
                
                $time_data_keys = array();
                $keys = "";
                
                $time_values = array();
                $vals = "";
                
                foreach($time_data as $key => $value)
                {
                     $keys .= "|" . $key;
                }
                
                foreach($time as $value)
                {
                     $vals .= "|" . $value;
                }
                
                $time_data_keys = explode("|", $keys);
                $time_values = explode("|", $vals);
                
                $my_time_data = array();
                
                for($i = 0; $i < sizeof($time_data_keys); $i++)
                {
                     $the_key = $time_data_keys[$i];
                     $the_value = $time_values[$i];
                
                
                 if($i == 0)
                 {
                      $my_time_data = array($the_key => $the_value);
                 }
                 else
                 {
                      $my_time_data = array_merge($my_time_data, array($the_key => $the_value));
                 }
                }
                
                extract($my_time_data);
                
                
                  6 days later

                  Try this instead:

                  
                  $time_data = array();
                  
                  $sql = "select * FROM confhours WHERE last_name = '$last' AND first_name = '$first'";
                  
                  $result = mysql_query($sql);
                  
                  while($row = mysql_fetch_array($result))
                  {
                  
                          for($i = 0; $i < sizeof($row); $i++)
                          {
                               //Notice parameter $result
                               $fieldname = mysql_field_name($result, $i);
                  
                               if($i == 0)
                               {
                                     $time_data = array($fieldname => $row[$i]);
                               }
                               else
                               {
                                     $time_data = array_merge($time_data, array($fieldname => $row[$i]));
                               }            
                  
                          }
                  }
                  
                  $time = array ("7"=>"7:00", "7.25"=>"7:15", "7.5"=>"7:30", "7.75"=>"7:45", 
                  "8"=>"8:00", "8.25"=>"8:15", "8.5"=>"8:30", "8.75"=>"8:45",
                   "9"=>"9:00", "9.25"=>"9:15", "9.5"=>"9:30", "9.75"=>"9:45", 
                  "10"=>"10:00", "10.25"=>"10:15", "10.5"=>"10:30", 
                  "10.75"=>"10:45", "11"=>"11:00", "11.25"=>"11:15", 
                  "11.5"=>"11:30", "11.75"=>"11:45", "12"=>"12:00p", 
                  "12.25"=>"12:15p", "12.5"=>"12:30p", "12.75"=>"12:45p", 
                  "13"=>"1:00p", "13.25"=>"1:15p", "13.5"=>"1:30p", 
                  "13.75"=>"1:45p", "14"=>"2:00p", "14.25"=>"2:15p",
                   "14.5"=>"2:30p", "14.75"=>"2:45p", "15"=>"3:00p", 
                  "15.25"=>"3:15p", "15.5"=>"3:30p", "15.75"=>"3:45p", 
                  "16"=>"4:00p", "16.25"=>"4:15p", "16.5"=>"4:30p",
                   "16.75"=>"4:45p", "17"=>"5:00p", "17.25"=>"5:15p", 
                  "17.5"=>"5:30p", "17.75"=>"5:45p", "18"=>"6:00p", 
                  "18.25"=>"6:15p", "18.5"=>"6:30p", "18.75"=>"6:45p",
                   "19"=>"7:00p", "19.25"=>"7:15p", "19.5"=>"7:30p",
                   "19.75"=>"7:45p", "20"=>"8:00p", "20.25"=>"8:15p", 
                  "20.5"=>"8:30p", "20.75"=>"8:45p", "21"=>"9:00p", 
                  "21.25"=>"9:15p", "21.5"=>"9:30p", "21.75"=>"9:45p",
                   "22"=>"10:00");
                  
                  foreach($time_data as $key => $value)
                  {
                       //Set time_data's key to hold time's value
                       //by matching time_data's value to time's key
                       //this should give you what you need
                  
                   //Check if $key is a number
                   //if it is skip over it, because mysql
                   //will return numbers as key also.
                   //And check if $value actually has a value
                   //if it doesn't skip over the assignment.
                  
                   if(!is_numeric($key) and $value)
                   {
                        $time_data[$key] = $time[$value];
                   }
                  }
                  
                  extract($time_data);
                  
                  //Here is where you echo or print out the new values of time_data
                  //possibly for a report record of the individual
                  
                  

                  I think this will work. Hope it helps.

                  Robogenus

                    Wow ... first evidence looks like that did it! 🙂

                    I've been scratchng my head and testing as much as I could without success.

                    I'll go through it over the next couple of days, confirm your suggestions and let you know for sure.

                    Thanks very much. 😃

                      Your Welcome OutPost!
                      Let me know how it works.

                      Robogenus

                        Thanks again Robogenus, your code works flawlessly! 🙂

                        I've compared the scripts and can see what you've changed. All in all it seems like a fairly complex bit of code ... it'll take me years to reach your skills.

                        Thanks for helping us newbies (and for helping the local school with this volunteer project)! 🙂

                          I'm just glad it works finally...lol. I really don't know what I'm doing. I just go by trial and error. I'm glad it worked for you though, ciao!

                          Robogenus

                            Write a Reply...