0

I am fairly new to PHP-MYsql and HTML programming. So Please bear with me,

I'm trying to create a function which will produce an array of 'Fields in a Table' and combine that array with an array of 'Column Headings' to produce an associative array like this;

$labels= array('IDno' => 'Id No', 
                  'VendorID' => 'Vendor Id No',
                    etc, 
                    ect.
             );

so $labels('VendorID') would show 'Vendor Id No', etc.
the code I will use to produce function getTableFields() is as follows;

 // ============ getTableFields =========== 
  $query= sprintf("SHOW COLUMNS FROM %s", $table); 
   $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
  $row = mysqli_fetch_assoc($result);
            $fields[]=$row['Field'];         
  for($i=1; $i<mysqli_num_rows($result); $i++)
  { 
                  $row = mysqli_fetch_assoc($result);
                  $fields[]=$row['Field'];

  } 

This works and produces the array of fields.
Hand written array of column headings for above fields.

 $colhead = array(
        "Id No:",
        "Vendor Id No:",
        "Account No:",
        "Company:",
        "Category:",
        "Email:",
        "Phone:",
        "Internet Id:",
        "Password:", 
        "License:",
        "Email of Record:",
        "Notes:"        
   );

I have tried this and variations of this;

$labels=array('$fields[0]' =>'$colhead[0]',    
          '$fields[1]' => $colhead[1],
           etc,
            etc,
 ); 

which does not work.
I have tried all kinds of combinations of { } ( ) [ ] ' " \" to enclose the variables to make it work. I've tried echo, print, printf, sprintf etc.

Can you help?
It just seems that this should be trivial.

Thanks in advance.

Frustrated, Rick_P

Edited by Reverend Jim: Fixed formatting

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by pizzipie
0

Thanks robothy, I knew it was simple and!!! array_combine() works like a champ. I think this will help me solve some other similar problems. If not I'll post again.

Again a big THANKS!!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.