DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 Sorry i wrote it wrong Should be $sql = mysql_query("SELECT ".implode(' ,',$fieldArr)." FROM dailypricing") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-745955 Share on other sites More sharing options...
hominid4 Posted January 25, 2009 Author Share Posted January 25, 2009 That was my original line I had in my first post, which outputs the table just fine but the underscores in as_of_date and nav_change are there. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-745981 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 Yeah put that replace code where you are printing the names Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-745986 Share on other sites More sharing options...
hominid4 Posted January 25, 2009 Author Share Posted January 25, 2009 I'm afraid I'm confused. Doesn't that put us back to what replace code to insert and where to put it? Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746064 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 try str_replace('_',' ',($d) Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746074 Share on other sites More sharing options...
hominid4 Posted January 25, 2009 Author Share Posted January 25, 2009 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746087 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 change $f = str_replace('_',' ',$f); echo '<tr><td>'.$f.'</td>'; to echo '<tr><td>'. str_replace('_',' ',$f).'</td>'; Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746108 Share on other sites More sharing options...
hominid4 Posted January 25, 2009 Author Share Posted January 25, 2009 Yes!! That did it!! All is working as it should now. Thanks for your help and patience, I learned a lot! Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746118 Share on other sites More sharing options...
DeanWhitehouse Posted January 26, 2009 Share Posted January 26, 2009 You should also learn how to name variables with relevant names and write code that can be understood by any programmer, then you will get help quicker Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746126 Share on other sites More sharing options...
hominid4 Posted January 26, 2009 Author Share Posted January 26, 2009 Will do. With my current knowledge level that was the only way I knew how to turn the table on it's side; but will work on learning better. So much to learn. Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746141 Share on other sites More sharing options...
hominid4 Posted January 26, 2009 Author Share Posted January 26, 2009 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/142282-change-database-field-name-to-a-different-word-during-query/page/2/#findComment-746198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.