hominid4
Members-
Posts
19 -
Joined
-
Last visited
Never
Profile Information
-
Gender
Not Telling
hominid4's Achievements
Newbie (1/5)
0
Reputation
-
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
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>'; -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
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. -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
Yes!! That did it!! All is working as it should now. Thanks for your help and patience, I learned a lot! -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
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>'; -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
I'm afraid I'm confused. Doesn't that put us back to what replace code to insert and where to put it? -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
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. -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
Thanks Blade, I've been playing around with your query but keep getting the error: Wrong parameter count for implode(). You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM dailypricing' at line 1 -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
Yes, you're correct, my database field names are: 'nav','pop','nav_change','as_of_date' -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
Thanks redarrow, that seems to be on the right track but I'm receiving the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'change ,as of date FROM dailypricing' at line 1 -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
When I do that it repeats "as of date" four times. The reason I'm using the arrays as I am is to turn the database table on it's side so that the field names are in the left column; instead of the standard way where the field names are at the top of the columns. Shouldn't though I be able to take the field names and change them no matter how my insertion code is; since my insertion code is after the $sql query? -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
When I inserted the echo str_replace('_',' ',$fields); it just wrote "as of date" at the top of the table. Early I did play around with $fields = str_replace('_',' ',$fields); but didn't have any luck with that either. -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
I'm sorry, I'm really new at this. Are you speaking of putting print_r(str_replace('_',' ',$fieldArr2)); inside the for loop, and if so which one? I did try it in different places and I get this printed out above the table: Array ( [nav] => Array ( ) [pop] => Array ( ) [nav_change] => Array ( ) [as_of_date] => Array ( ) ) Also, isn't $fieldArr2 for the values of the field names? And shouldn't I be working with $fieldArr or $f so that it's only for the field names themselves? Thank you! -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
I'm printing out the table on my web page. My web page includes my database connection code and the insertion code. Below is my full html on my page that I'm testing this out on, and where I currently have the print_r(str_replace()) line. I've also attached what the table prints out as. <?php $link = mysql_connect('localhost', 'MYDATABASE-USERNAME', 'MYDATABASE-PASSWORD') mysql_select_db('MYDATABASE-NAME') ?> <html> <head> <title>Daily Pricing</title> </head> <body> <?php $fieldArr = array('nav','pop','nav_change','as_of_date'); $fieldArr2 = array(); foreach($fieldArr as $fields) $fieldArr2[$fields] = array(); $sql = mysql_query("SELECT ".implode(' ,',$fieldArr)." FROM dailypricing") or die(mysql_error()); print_r(str_replace('_',' ',$fieldArr)); while($row = mysql_fetch_array($sql)) { foreach($fieldArr as $f) { if(!empty($row[$f])) { $fieldArr2[$f][] = $row[$f]; }else{ $fieldArr2[$f][] = ' '; } } } echo '<table border="1">'; foreach($fieldArr as $f) { echo '<tr><td>'.$f.'</td>'; foreach($fieldArr2[$f] as $d) { echo '<td>'.$d.'</td>'; } echo '</tr>'; } echo '</table>'; ?> </body> </html> [attachment deleted by admin] -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
Good to hear I'm getting closer, yes that what the field names are supposed to be. I'm afraid I don't know what I'm doing wrong. Would you have a suggestion what part of the code I should focus on? I inserted the print_r(str_replace()) on every line but with no luck, would that mean I should modify the other code? Thanks for your help. -
Change database field name to a different word during query?
hominid4 replied to hominid4's topic in PHP Coding Help
I've inserted the print_r before the loop, and other places, but it just prints: Array ( [0] => nav [1] => pop [2] => nav change [3] => as of date )