Jump to content

Change database field name to a different word during query?


hominid4

Recommended Posts

This seems the closest I've been.  Using the $d changes the database table values, using $f changes the field names.

 

Using, say: $d = str_replace('.',' ',$d); to replace the "." with a space within the values it works just fine - 2.53 becomes 2 53

echo '<table border="1">';
foreach($fieldArr as $f)
{         
    echo '<tr><td>'.$f.'</td>';      
    foreach($fieldArr2[$f] as $d)
    {     
        $d = str_replace('.',' ',$d);
        echo '<td>'.$d.'</td>';   
    }  
    echo '</tr>';   
}   
echo '</table>';

But if I use the $f = str_replace('_',' ',$f); it removes the underscore from the field names correctly but I get the below error and the values for "As Of Date" and "Nav Change" don't show up.

 

Error:  Invalid argument supplied for foreach() on line foreach($fieldArr2[$f] as $d)

 

This is the $f = str_replace('_',' ',$f); code:

echo '<table border="1">';
foreach($fieldArr as $f)
{       
    $f = str_replace('_',' ',$f);
    echo '<tr><td>'.$f.'</td>';      
    foreach($fieldArr2[$f] as $d)
    {   
        echo '<td>'.$d.'</td>';   
    }  
    echo '</tr>';   
}   
echo '</table>';

For those who may be interested, since learning the echo '<tr><td>'. str_replace('','',$f).'</td>'; I went back and worked on my original task; changing 'navchange','asofdate' to 'NAV Change','As Of Date'.  I was able to accomplish this with the below code:

echo '<table border="1">'; 
foreach($fieldArr as $f) 
{  
$find = array('asofdate','navchange'); 
$replace = array('As Of Date','NAV Change'); 

    echo '<tr><td>'. str_replace($find, $replace, $f).'</td>';       
    foreach($fieldArr2[$f] as $d) 
    {    
        echo '<td>'.$d.'</td>';    
    }   
    echo '</tr>';    
}    
echo '</table>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.