Welcome Guest, Not a member yet? Register   Sign In
There's a dollar(sign) in variable name
#1

Is that possible to get the data when their is a dollar(sign) in variable.

Example:

my column name is price_in_u$
** this is the column name in my database

then i use model and controller to query and get data ( i have no problem here)

this is my code when i display the data
**
PHP Code:
echo 'Price in US: ' $comp_prod->prince_in_u

the error i got is:
syntax error, unexpected '$', expecting ',' or ';'

is there a way i can use the dollar(sign) ?

even escaping the string is not working.
PHP Code:
echo 'Price in US: ' $comp_prod->prince_in_u/$ 
Reply
#2

You could try:

PHP Code:
$tmpVar 'prince_in_u$';
echo 
'Price in US: ' $comp_prod->$tmpVar

Or

PHP Code:
echo 'Price in US: ' $comp_prod->{'prince_in_u$'}; 

Another idea would be to fetch the result as an array instead of an object.

PHP Code:
foreach ($query->result_array() as $row)
{
 
  echo 'Price in US: '$row['prince_in_u$'];



Or as a last resort you could change the column name in your database.

PS, in your code you forgot the dot sign to concatenate the string and the object.
Reply
#3

PHP Code:
$temp_var 'Price_IN_U$';

$comp-prod->$temp_var

this one works! hooray! haha i already come up with this idea but i also include the
comp-prod that makes my code echo it.

thanks for this! new learning.
Reply
#4

Something else to consider is that you can alias the column name in your select statement, like this:
PHP Code:
// Although I don't recommend adding '*' in the array, 
// I've done so here because I don't know what else you 
// may be selecting in this query.
$this->db->select(array('price_in_u$ as price_in_us''*'));
// ...
echo "Price in US: {$comp_prod->price_in_us}"
Reply




Theme © iAndrew 2016 - Forum software by © MyBB