help a noob with some inherited code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsp003
Forum Newbie
Posts: 16
Joined: Thu Apr 21, 2011 1:39 am

help a noob with some inherited code

Post by tsp003 »

i'm quite a php noob and i've inherited a site and have got quite stuck, with what i think should be something quite simple, i'm usually quite good at working it out, eventually, but this one appears to have me beat.

quite simply the site has fields of country, state and city, the database has three tables named accordingly.

Country
id
country

State
id
state
countryid

city
id
city
stateid

Now when viewing the pages the country field displays as expected, i.e. you see the country. State and City are only displaying only the id numbers of the rows and not the values.

i think i've narrowed it down to two culprit php files admin.index.php and admin.index.hml.php

admin.index.php contains the following references:-

Code: Select all

//config : country
		case 'country':	
			showCountry($option);
		break;
		
		case 'newcountry':
			editCountry(false);
		break;
		
		case 'editcountry':
			editCountry(true);
		break;	
				
		case 'savecountry':	
			saveCountry($option );
		break;
		
		case 'removecountry':
			removeCountry($cid );
		break;
		
		case 'cancelcountry':
			cancelCountry();
		break;
		
//config : state
		case 'state':	
			showCountry($option);
		break;
		
		case 'newstate':
			editCountry(false);
		break;
		
		case 'editstate':
			editCountry(true);
		break;	
				
		case 'savestate':	
			saveCountry($option );
		break;
		
		case 'removestate':
			removeCountry($cid );
		break;
		
		case 'cancelstate':
			cancelCountry();
		break;
		
		//config : city
		case 'city':	
			showCountry($option);
		break;
		
		case 'newcity':
			editCountry(false);
		break;
		
		case 'editcity':
			editCountry(true);
		break;	
				
		case 'savecity':	
			saveCountry($option );
		break;
		
		case 'removecity':
			removeCountry($cid );
		break;
		
		case 'cancelcity':
			cancelCountry();
		break;


                
              //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;
		
		//getSelectState

		function getSelectState($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_name 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 state
		$query = 'select id as value, city_name 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;
        
        ?>
and admin.index.hml.php contains


<?
// contents in html.php

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>
<tr>
  <td class="key"><label for="name"> <?php echo JText::_( 'State' ); ?>: </label></td>
  <td ><?php $list_state = getSelectState('id_state',$row->id_state,'') ;
							echo $list_state;
						?></td>
</tr>
<tr>
  <td class="key"><label for="name"> <?php echo JText::_( 'City' ); ?>: </label></td>
  <td ><?php $list_state = getSelectCity('id_city',$row->id_city,'') ;
							echo $list_city;
						?></td>
</tr>
<?

//Configuration :Country
function showCountry( $option )
{
	global $mainframe;
	$db	= & JFactory::getDBO();
	
	
	$post   = JRequest::get( 'post' );
	
	$limit		= $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
	$limitstart	= $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');
	
	
	$query = "SELECT COUNT(*)".	          
			 " FROM #__tpjobs_country a";
	
	
	$db->setQuery( $query );
	$total = $db->loadResult();
	
	jimport('joomla.html.pagination');
	$pageNav = new JPagination( $total, $limitstart, $limit );

	
	$query = "SELECT a.*  ".
			 " FROM #__tpjobs_country a";
	
	$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
	$rows = $db->loadObjectList();
	
	
	HTML_tpjobs::showCountry($rows,$pageNav,$option);
}

function editCountry($edit)
{
	$db		=& JFactory::getDBO();
	$user 	=& JFactory::getUser();

	$cid 	= JRequest::getVar('cid', array(0), '', 'array');
	$option = JRequest::getCmd('option');

	JArrayHelper::toInteger($cid, array(0));

	$row =& JTable::getInstance('country','Table');
	// load the row from the db table
	
	if($edit)
	$row->load($cid[0]);
	
	
	HTML_tpjobs::editCountry( $row, $option);
}


function saveCountry($option)
{
	global $mainframe;
	
	// Check for request forgeries
	JRequest::checkToken() or jexit( 'Invalid Token' );
	
	// Initialize variables
	$db		=& JFactory::getDBO();
	$row	=& JTable::getInstance('country', 'Table');
	$post   = JRequest::get( 'post' );
	
	if (!$row->bind( $post )) {
		JError::raiseError(500, $row->getError() );
	}
	

	// pre-save checks
	if (!$row->check()) {
		JError::raiseError(500, $row->getError() );
	}

	
	// if new item,  last in appropriate group
	if (!$row->id) {
		$where = "id = " . (int) $row->id;		
	}
	
	
	// save the changes
	if (!$row->store()) {
		JError::raiseError(500, $row->getError() );
	}
	
	
	$row->checkin();
	
	$msg	= JText::_( 'Country is saved' );
	$link	= 'index.php?option=com_tpjobs&task=country';
		
	

	$mainframe->redirect( $link, $msg );
	
}

function removeCountry(&$cid )
{
	global $mainframe;

	// Check for request forgeries
	JRequest::checkToken() or jexit( 'Invalid Token' );

	// Initialize variables
	$db =& JFactory::getDBO();
	JArrayHelper::toInteger($cid);



	if (!is_array($cid) || count($cid) < 1 || (count($cid)==1 && $cid[0] ==0 )) {		
		echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n";
		exit;				
	}	
	
	else{
			
		if (count($cid)) {
			$count_ketemu =0;
			for($i=0;$i<count($cid);$i++){
					$curr_bid =$cid[$i];
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_job ".
				 			 "\n where id_country =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_1 = $db->loadResult();	
					
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_billing ".
				 			 "\n where id_country =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_2 = $db->loadResult();	
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_jobseekeer ".
				 			 "\n where id_country =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_3 = $db->loadResult();	
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_employer ".
				 			 "\n where id_country =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_4 = $db->loadResult();
					
					
					if($find_1 > 0 || $find_2 > 0 || $find_3 > 0 || $find_4 > 0){
						$ketemu =1;
					
					}						
				
					
					if($find_1 == 0 && $find_2 == 0 && $find_3 == 0 && $find_4 == 0){
						
						$db->setQuery("DELETE FROM #__tpjobs_country WHERE id = $curr_bid");
						if (!$db->query()) {
							echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n";
						}
					}
					
					if($ketemu >0){
						$count_ketemu++;
					}
					 
				
			}
			
			if($count_ketemu >0){				
				echo "<script> alert('There some country cannot delete. Becouse it is used for others data'); window.history.go(-1); </script>\n";
				exit ();
		
			
			}		
		
		}
	}

	$msg	= JText::_( 'Country has been deleted' );
	$link	= 'index.php?option=com_tpjobs&task=country';
	
	$mainframe->redirect( $link, $msg );
}



function cancelCountry($cancel){
		global $mainframe;
		$msg ='';
		$link	= 'index.php?option=com_tpjobs&task=country';	
		$mainframe->redirect( $link,$msg );
		
}
im not getting any errors when trying to run this script, i have got so far as the selection boxes are there, but with no options available to choose from.

What I am ultimately aiming for was the creation of cascading select boxes, but seems like im stuck on just getting the values to display

If anyone can shed some light on this for me i would be very grateful. Thanks
Last edited by tsp003 on Thu Apr 21, 2011 7:07 am, edited 1 time in total.
eivind
Forum Commoner
Posts: 28
Joined: Tue Apr 19, 2011 7:57 am

Re: help a noob with some in inherited code

Post by eivind »

You say that the columns containing the names of states and cities is ("state" and "city"), while in the function getSelectState it says "state_name as text" and in getSelectCity it says "city_name as text". If you're right about the column names in the database, it should be "state as text" and "city as text", I think.
tsp003
Forum Newbie
Posts: 16
Joined: Thu Apr 21, 2011 1:39 am

Re: help a noob with some in inherited code

Post by tsp003 »

Gee I feel stoopid now :oops: Thanks alot for pointing that out, guess i've been looking at it far too hard!

I've changed all the references to the table fields and it now almost works.

Country and state are now populated as expected, but city is asking for you to select a value rather than display the value in the table.

form code

Code: Select all

<tr>
<td class="key">
<label for="name"><?php echo JText::_( 'City' ); ?>:</label>
</td>
<td >
<?php $list_city = getSelectCity('city',$row->city,'') ;
echo $list_city;?></td></tr>
functions

Code: Select all

//config : city
		]case 'city':	
			showCity($option);
		break;
		
		case 'newcity':
			editCity(false);
		break;
		
		case 'editcity':
			editCity(true);
		break;	
				
		case 'savecity':	
			saveCity($option );
		break;
		
		case 'removestate':
			removeCity($cid );
		break;
		
		case 'cancelstate':
			cancelCity();
		break;
....

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

}

.....

//Configuration :City
function showCity( $option )
{
	global $mainframe;
	$db	= & JFactory::getDBO();
	
	
	$post   = JRequest::get( 'post' );
	
	$limit		= $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
	$limitstart	= $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');
	
	
	$query = "SELECT COUNT(*)".	          
			 " FROM #__tpjobs_city a";
	
	
	$db->setQuery( $query );
	$total = $db->loadResult();
	
	jimport('joomla.html.pagination');
	$pageNav = new JPagination( $total, $limitstart, $limit );

	
	$query = "SELECT a.*  ".
			 " FROM #__tpjobs_city a";
	
	$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
	$rows = $db->loadObjectList();
	
	
	HTML_tpjobs::showCity($rows,$pageNav,$option);
}

function editCity($edit)
{
	$db		=& JFactory::getDBO();
	$user 	=& JFactory::getUser();

	$cid 	= JRequest::getVar('cid', array(0), '', 'array');
	$option = JRequest::getCmd('option');

	JArrayHelper::toInteger($cid, array(0));

	$row =& JTable::getInstance('city','Table');
	// load the row from the db table
	
	if($edit)
	$row->load($cid[0]);
	
	
	HTML_tpjobs::editCity( $row, $option);
}


function saveCity($option)
{
	global $mainframe;
	
	// Check for request forgeries
	JRequest::checkToken() or jexit( 'Invalid Token' );
	
	// Initialize variables
	$db		=& JFactory::getDBO();
	$row	=& JTable::getInstance('city', 'Table');
	$post   = JRequest::get( 'post' );
	
	if (!$row->bind( $post )) {
		JError::raiseError(500, $row->getError() );
	}
	

	// pre-save checks
	if (!$row->check()) {
		JError::raiseError(500, $row->getError() );
	}

	
	// if new item,  last in appropriate group
	if (!$row->id) {
		$where = "id = " . (int) $row->id;		
	}
	
	
	// save the changes
	if (!$row->store()) {
		JError::raiseError(500, $row->getError() );
	}
	
	
	$row->checkin();
	
	$msg	= JText::_( 'City is saved' );
	$link	= 'index.php?option=com_tpjobs&task=city';
		
	

	$mainframe->redirect( $link, $msg );
	
}

function removeCity(&$cid )
{
	global $mainframe;

	// Check for request forgeries
	JRequest::checkToken() or jexit( 'Invalid Token' );

	// Initialize variables
	$db =& JFactory::getDBO();
	JArrayHelper::toInteger($cid);



	if (!is_array($cid) || count($cid) < 1 || (count($cid)==1 && $cid[0] ==0 )) {		
		echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n";
		exit;				
	}	
	
	else{
			
		if (count($cid)) {
			$count_ketemu =0;
			for($i=0;$i<count($cid);$i++){
					$curr_bid =$cid[$i];
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_job ".
				 			 "\n where city =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_1 = $db->loadResult();	
					
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_billing ".
				 			 "\n where city =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_2 = $db->loadResult();	
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_jobseekeer ".
				 			 "\n where city =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_3 = $db->loadResult();	
					
					$query ="select COUNT(*)".		
			    			 "\n FROM  #__tpjobs_employer ".
				 			 "\n where city =$curr_bid".						
				 			 "\n ";
					
					$db->setQuery($query);	
					$find_4 = $db->loadResult();
					
					
					if($find_1 > 0 || $find_2 > 0 || $find_3 > 0 || $find_4 > 0){
						$ketemu =1;
					
					}						
				
					
					if($find_1 == 0 && $find_2 == 0 && $find_3 == 0 && $find_4 == 0){
						
						$db->setQuery("DELETE FROM #__tpjobs_city WHERE id = $curr_bid");
						if (!$db->query()) {
							echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n";
						}
					}
					
					if($ketemu >0){
						$count_ketemu++;
					}
					 
				
			}
			
			if($count_ketemu >0){				
				echo "<script> alert('There some city cannot delete. Becouse it is used for others data'); window.history.go(-1); </script>\n";
				exit ();
		
			
			}		
		
		}
	}

	$msg	= JText::_( 'City has been deleted' );
	$link	= 'index.php?option=com_tpjobs&task=city';
	
	$mainframe->redirect( $link, $msg );
}



function cancelCity($cancel){
		global $mainframe;
		$msg ='';
		$link	= 'index.php?option=com_tpjobs&task=city';	
		$mainframe->redirect( $link,$msg );
		
}
Can anyone see whats wrong with this?

Thanks
eivind
Forum Commoner
Posts: 28
Joined: Tue Apr 19, 2011 7:57 am

Re: help a noob with some inherited code

Post by eivind »

Maybe it should be

Code: Select all

$list_city = getSelectCity('id_city',$row->city,'') ;
echo $list_city;
?

At least I think the first argument will be the name of the form, and it might be important that it is id_city. I'm not sure, but it might be worth a try.
tsp003
Forum Newbie
Posts: 16
Joined: Thu Apr 21, 2011 1:39 am

[RESOLVED]: help a noob with some inherited code

Post by tsp003 »

Thank you.

the moral of this story is always check the consistency between table names and what you are putting into your code.

I must've had table name blindness :drunk:

Thanks for replying
Post Reply