php - How to construct a Joomla! query for matching two field values in a table? -


i have table #__newtoys_variants having many fields of id , v_prod_id there.

now although id unique - v_prod_id product id url displays product information , price

example.com/index.php?option=com_newtoys&id=2&vid=7 

here id value extracted id , vid v_prod_id extracted db table row against corresponding

here brief of table instance

id v_prod_id v_price 1     7       200 2     7       220 3     1       250 4     1       270 5     2       300 6     10      350 7     9       220 8     7       195 

now intend 404 error / 500 error / page not exist should displayed in front end - if id , v_prod_id not matched in front end url

in case user changes url say

example.com/index.php?option=com_newtoys&id=2&vid=1 

then want 404 error / 500 error / page not exist displayed in front end here table database enter image description here can 1 on achieve same

here brief function - unsure should in sql query or function id & v_prod_id should matched in array , in case result 0 error message can displayed

function loadproduct($id ,$vid){     $mainframe =jfactory::getapplication();     $option = jrequest::getcmd('option');     $db =jfactory::getdbo();     global $itemid;      $sql = "";      $db->setquery($sql);  if ($rows = $db->loadobjectlist()) { return $rows[0];  } else {   if ($db->geterrornum()) {     jerror::raiseerror(500, "something went horribly wrong, query returned error ". $db->geterrormsg());    } else {  jerror::raiseerror(404, "404, page not exists ". $db->geterrormsg());   } }            } 

can 1 , suggest. bounty added

try this:

function loadproduct($id, $vid){   $mainframe = jfactory::getapplication();   $option = jrequest::getcmd('option');   $db = jfactory::getdbo();   $query = $db->getquery(true);   $query->select('id, v_prod_id');   $query->from($db->quotename('#__newtoys_variants'));   $query->where($db->quotename('id')." = ".$db->quote($id), 'and');   $query->where($db->quotename('v_prod_id')." = ".$db->quote($vid));   $db->setquery($sql);    if ($rows = $db->loadobjectlist()) {     return $rows[0];   } else {     if ($db->geterrornum()) {       jerror::raiseerror(500,        "something went horribly wrong, query returned error ". $db->geterrormsg());     } else {       jerror::raiseerror(404,        "404, page not exists ". $db->geterrormsg());     }   } } 

see here further details:

https://docs.joomla.org/selecting_data_using_jdatabase

update

you asked in comments joomla! translation of following query:

select *, (select prod_name #__newtoy_products id=v.id) prod_name #__newtoys_variants v v.state='1' , v.id = '".$v_prod_id."' 

...which same this:

select v.*, p.prod_name #__newtoys_variants v left join #__newtoy_products p on p.id = v.id v.state='1' , v.id = '".$v_prod_id"' 

...which should map in joomla!:

$query->select(array('v.*', 'p.prod_name'))       ->from($db->quotename('#__newtoys_variants', 'v'))       ->join('left', $db->quotename('#__newtoy_products', 'p'))       . ' on (' . $db->quotename('p.id') . ' = ' . $db->quotename('v.id') . ')')       ->where($db->quotename('v.state')." = ".$db->quote(1), 'and')       ->where($db->quotename('v.id')." = ".$db->quote($v_prod_id)); 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -