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>';

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.