Jump to content

Change database field name to a different word during query?


hominid4

Recommended Posts

On my website I have a HTML table that has 5 columns, 5 rows; within this table I have the below code. Going down the left column are the field names and their values going out to the right of each field name taking up those column cells; pulled from the MySQL database table:

$fieldArr = array('nav','pop','navchange','asofdate'); 

$fieldArr2 = array(); 
foreach($fieldArr as $fields) $fieldArr2[$fields] = array(); 

$sql = mysql_query("SELECT ".implode(' ,',$fieldArr)." FROM dailypricing") or die(mysql_error()); 
while($row = mysql_fetch_array($sql)) 

{ 
    foreach($fieldArr as $f) 
    { 
        if(!empty($row[$f])) 
        { 
            $fieldArr2[$f][] = $row[$f]; 
        }else{ 
            $fieldArr2[$f][] = ' '; 
        }   
    } 
} 
     
            echo '<tr>'; 
            foreach($fieldArr as $f) 
{ 
    echo '<td>'.$f.'</td>';    
    foreach($fieldArr2[$f] as $d) 
    { 
        echo '<td>'.$d.'</td>'; 
    } 
   echo '</tr>'; 
}

All works fine except I'm needing the field names that are pulled from the database that go down the left column to be different; such as I need 'navchange' to be "NAV Change", and 'asofdate' to be "As Of Date".

 

Could I please get some help on how I can change the field names during query? I've attached a sample of what my HTML table will look like.  The field names 'nav','pop','navchange','asofdate' changes daily, that's what part of my table is fixed and the database field names are dynamic.

 

Thank you!

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Thanks Blade.  If I used the underscore would there be a way for me to remove it when showing the results?  Maybe something like:

$fieldArr = str_replace('nav_change','NAV Change',$fieldArr); 
$fieldArr = str_replace('as_of_date','As Of Date',$fieldArr);

or

$fieldArr = str_replace('_',' ',$fieldArr);

 

Thanks again.

Link to comment
Share on other sites

Good deal.  When I insert the $fieldArr = str_replace('_',' ',$fieldArr); it removes the _ correctly but I lose the values for those field names; the nav_change and as_of_date values are blank in my HTML table.  One place where I inserted it is after:

$sql = mysql_query("SELECT ".implode(' ,',$fieldArr)." FROM dailypricing") or die(mysql_error());
$fieldArr = str_replace('_',' ',$fieldArr);

 

Below is what I'm using for the HTML table that shows in my above attachment; I've removed the formatting to simplify it.  Would you have a suggestion where I need to insert the str_replace?

 

		<table>
		<tr>
			<td>Fund Name</td>
			<td>Japan Fund</td>
			<td>Australia / New Zealand Fund</td>
			<td>Global Fund</td>
			<td>Real Estate Securities Fund</td>
		</tr>
		<tr>
			<td>Symbol</td>
			<td>CNJFX</td>
			<td>CNZLX</td>
			<td>CNGLX</td>
			<td>CNREX</td>
		</tr>
		<?php
		echo '<tr>';
		foreach($fieldArr as $f)
{
    echo '<td>'.$f.'</td>';     
    foreach($fieldArr2[$f] as $d) 
    {
        echo '<td>'.$d.'</td>';
    }
   echo '</tr>';
}
?>
	</table>

Link to comment
Share on other sites

I've removed all the extra HTML coding and am back to my basic insertion code.

 

If I insert the str_replace in the top portion it changes the field name but the values come out blank.

$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());

$fieldArr = 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>';

 

If I insert within the bottom portion I receive the error:

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

 

Such as:

$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()); 
while($row = mysql_fetch_array($sql)) 

{ 
    foreach($fieldArr as $f) 
    { 
        if(!empty($row[$f])) 
        { 
            $fieldArr2[$f][] = $row[$f]; 
        }else{ 
            $fieldArr2[$f][] = ' '; 
        }   
    } 
}

$fieldArr = str_replace('_',' ',$fieldArr);

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

or

$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()); 
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)   
{
   $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

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

No

 

try

<?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());

echo str_replace('_',' ',$fields);

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

try this

<?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(' ,',str_replace('_',' ',($fieldArr)))." FROM dailypricing") or die(mysql_error());

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>

Link to comment
Share on other sites

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

 

try this

<?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(' ,',str_replace('_',' ',($fieldArr)))." FROM dailypricing") or die(mysql_error());

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>

Link to comment
Share on other sites

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

 

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.