See the details here
$url = '{$_SERVER->REQUEST_URI}';
//extract texts between slashes of url
list($slsh, $page, $viewtype, $formid, $rowiddirty, $trail) = explode("/", $url);
//remove everything except numbers
$formid = preg_replace("/[^0-9]/", "", $formid);
$rowid = preg_replace("/[^0-9]/", "", $rowid);
if ($viewtype == "details") {
$mdb = JFactory::getDbo();
if ($formid == "1"){
$mdb->setQuery("SELECT needed FROM tblx WHERE (id = $rowid)");
} elseif ($formid == "2") {
$mdb->setQuery("SELECT needed FROM tbly WHERE (id = $rowid)");
} elseif ($formid == "3") {
$mdb->setQuery("SELECT needed FROM tblz WHERE (id = $rowid)");
} elseif ($formid == "4") {
$mdb->setQuery("SELECT needed FROM tblw WHERE (id = $rowid)");
}
$needed = $mdb->loadResult();
$formModel->data['___needed'] = $needed;
}
$app = JFactory::getApplication();
$formid = $app->input->get('formid', '');
$rowid = $app->input->get('rowid', '');
// rest of your stuff