help a noob with some inherited code
Posted: Thu Apr 21, 2011 1:55 am
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:-
and admin.index.hml.php contains
<?
// contents in html.php
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
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;
?><?
// 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 );
}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