Page 1 of 1

Help pulling text values from table

Posted: Tue Apr 26, 2011 9:04 am
by tsp003
I have the following form.

Code: Select all

<tr class="tpj_row1">
			<td align="right" nowrap="nowrap" valign="top">
				 <b><?php echo JText::_('LOCATION'); ?> :</b>
			</td>
			<td align="left" width="20%" valign="top"><?php echo $job->city; ?>,<?php echo $job->state; ?>
	    </td>
			<td valign="top">&nbsp;</td>
			<td valign="top" align="right">
			<b><?php echo JText::_('COUNTRY'); ?> :</b>
			</td>
			<td align="left" valign="top">	
			<?php echo $job->country; ?> 
			</td>
		</tr>
My dilemma is that the country field is showing the country as expected, but the state and the city are only showing the record id numbers, so what I am ending up with is
Location: 1,12 Country: United Kingdom,

When what is required is
Location: London, Greater London Country: United Kingdom

I have checked the table and all references to city, state and country are numeric as they are drawn from lookup tables, so obviously the php needs to be told to pull and display the text values not the record number.

I’ve been all through the code and can find no obvious code. The only thing I can find is a GetSelectCountry, GetSelectState and GetSelectCity function, but this looks more like its forthe data entry rather than display, there doesn’t seem to be anythere there that helps me. So being a bit of noob i’m now not sure what is required to show the contents of the text field.

Can somebody point me in the right direction and give me a clue what I Should be looking for

Thanks in advance

Re: Help pulling text values from table

Posted: Tue Apr 26, 2011 9:39 am
by getmizanur
I have checked the table and all references to city, state and country are numeric as they are drawn from lookup tables, so obviously the php needs to be told to pull and display the text values not the record number.
correct me if i'm wrong however it looks like you have answered your question. what is stopping you from looking up the numeric reference from a related table. use php mysql functions to pull the data for respective field.

hmmm... rule of thumb is that all getters are for retrieving data, can you post the code for GetSelectCountry, GetSelectState and GetSelectCity?

Re: Help pulling text values from table

Posted: Tue Apr 26, 2011 9:54 am
by tsp003
Hi thanks for the reply

the GetSelectCountry, State and City code is

Code: Select all

//getSelectCountry

function getSelectCountry($var,$default,$disabled){

		global $mainframe;

		$db	= & JFactory::getDBO(); 

	

		$option ='';

		if($disabled ==1) 

		$option = 'disabled';

		

		if($default == '') $default = '0'; 

		//make selection country

		$query = 'select id as value, country as text from #__tpjobs_country order by country';		

		$db->setQuery( $query );

		$countries = $db->loadObjectList();

		

		$types[] 		= JHTML::_('select.option',  '0', '- '. JText::_('SELECT COUNTRY') .' -');

		foreach( $countries as $item )

		{

			$types[] = JHTML::_('select.option',  $item->value, JText::_( $item->text ) );

		}		

		

		$lists 	= JHTML::_('select.genericlist',   $types, $var, 'class="inputbox" size="1" '.$option.'', 'value', 'text', $default );

		

		return $lists;



}

//getSelectStates

function getSelectStates($var,$default,$disabled){

		global $mainframe;

		$db	= & JFactory::getDBO(); 

	

		$option ='';

		if($disabled ==1) 

		$option = 'disabled';

		

		if($default == '') $default = '0'; 

		//make selection state

		$query = 'select id as value, state as text from #__tpjobs_state order by state';		

		$db->setQuery( $query );

		$states = $db->loadObjectList();

		

		$types[] 		= JHTML::_('select.option',  '0', '- '. JText::_('SELECT STATE') .' -');

		foreach( $states as $item )

		{

			$types[] = JHTML::_('select.option',  $item->value, JText::_( $item->text ) );

		}		

		

		$lists 	= JHTML::_('select.genericlist',   $types, $var, 'class="inputbox" size="1" '.$option.'', 'value', 'text', $default );

		

		return $lists;



}
//getSelectCity

function getSelectCity($var,$default,$disabled){

		global $mainframe;

		$db	= & JFactory::getDBO(); 

	

		$option ='';

		if($disabled ==1) 

		$option = 'disabled';

		

		if($default == '') $default = '0'; 

		//make selection city

		$query = 'select id as value, city as text from #__tpjobs_city order by city';		

		$db->setQuery( $query );

		$cities = $db->loadObjectList();

		

		$types[] 		= JHTML::_('select.option',  '0', '- '. JText::_('SELECT CITY') .' -');

		foreach( $cities as $item )

		{

			$types[] = JHTML::_('select.option',  $item->value, JText::_( $item->text ) );

		}		

		

		$lists 	= JHTML::_('select.genericlist',   $types, $var, 'class="inputbox" size="1" '.$option.'', 'value', 'text', $default );

		

		return $lists;



}
The problem is that this isn't my code, i've inherited it and been tasked with updating it because i've shown an interest in learning php. I have no clue if the code already here is supposed to do the job with the city and state or if if was left as a work in progress, and i've no way of contacting the author.

I am enjoying teasing it all out though, but this ones got me a bit stumped :)

Re: Help pulling text values from table

Posted: Tue Apr 26, 2011 12:42 pm
by getmizanur
Having a quick look at the code

getSelectCity($var, $default, $disabled)
parameters:
$var - name of the select field
$default - is the default value
$disabled - to disable the field

example: i want to create select field called city with default value 1

getSelectCity('city', 1, '');

off-course this will give you select field

however if you want just the city name for value 1 without the field

Code: Select all

$db     = & JFactory::getDBO();
$query = 'select id as value, city as text from #__tpjobs_city where id = 1';          
$db->setQuery( $query );
$cities = $db->loadObjectList();

Re: Help pulling text values from table

Posted: Tue Apr 26, 2011 3:22 pm
by tsp003
Thanks, however i've dug a little deeper and found the following bit of code

Code: Select all

<tr>
					<td class="key">
						<label for="name">
							<?php echo JText::_('COUNTRY'); ?>:
						</label>
					</td>
					<td>
						<?php $list_country = getSelectCountry('id_country',$row->id_country,'');
							echo $list_country;
						?>
					</td>
				</tr>
in this bit of code, which i think forms the drop down boxes to enable the data entry part of the site. This shows where the getSelect statement is used. If we look again at the code posted originally there is no getSelect statement used here, in fact the only php reference is

Code: Select all

<?php echo $job->city; ?>,<?php echo $job->state; ?>
So i'm now clued up on the getSelect statement, but i'm still not sure how to resolve my orginal problem on understanding how the text value is being displayed :D

Re: Help pulling text values from table

Posted: Tue Apr 26, 2011 5:22 pm
by getmizanur
To get the city and state text do this

Code: Select all

$db     = & JFactory::getDBO();
$query = 'select city from #__tpjobs_city where id = ' . $job->city;          
$db->setQuery( $query );
$cities = $db->loadObjectList();

$query = 'select state from #__tpjobs_state where id = ' . $job->state;          
$db->setQuery( $query );
$states = $db->loadObjectList();

echo $cities[0]->city;
echo $states[0]->states;
PS: i have not tested this code however give it a go

Re: Help pulling text values from table

Posted: Wed Apr 27, 2011 3:43 am
by tsp003
Thanks so much for yor help!

This bit of code pulls the city value :D . the State however, is still not displayed :(

Re: Help pulling text values from table

Posted: Wed Apr 27, 2011 5:24 am
by tsp003
Fixed!!

Changed

Code: Select all

echo $states[0]->states;
to

Code: Select all

echo $states[0]->state;
and we're now good to go

thanks so much getmizanur for taking the time to help me out :)

Re: Help pulling text values from table

Posted: Thu Apr 28, 2011 3:54 am
by getmizanur
happy to help.