1

I am trying to rewrite below query to fetch on database value off Models But am failing on in the new code, can any one help in to rewrite in query based on joomla latest standards

Old Code

<?php $db = JFactory::getDBO(); if(isset($_GET["s"])){ $ModelId=$_GET["s"]; $sql = "SELECT * from #_toy_products where prod_status='1' and prod_cat_id='".$ModelId."' order by prod_sorder Asc"; $db->setQuery($sql); $rows = $db->loadObjectList(); $list='<select name="model" class="list-box" id="model" lang="" title="Model">'; $list.='<option value="" style="padding-left:10px;" selected>-Select Model-</option>'; foreach($rows as $row){ $list.='<option value="'.$row->prod_id .'" style="padding-left:10px;">'.$row->prod_name.'</option>'; } $list.='</select>'; die($list); 

} ?>

New Code

<?php $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select($db->quoteName(array('Model', 'state', 'prod_cat_id', 'prod_sorder'))); $query->from($db->quoteName('#__toy_products')); $query->where($db->quoteName('state=1') . ' LIKE '. $db->quote('\'custom.%\'')); $query->order('prod_sorder ASC'); $db->setQuery($query); $results = $db->loadObjectList(); ?> 

1 Answer 1

1

Try using the following:

// GET using JInput $jinput = JFactory::getApplication()->input; $ModelId = $jinput->get('s', '', 'INT'); // Database stuff $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select($db->qn(array('Model', 'state', 'prod_cat_id', 'prod_sorder'))) ->from($db->qn('#__toy_products')) ->where($db->qn('prod_status') . ' = 1') ->where($db->qn('prod_cat_id') . ' = ' . (int)$ModelId) ->order('prod_sorder ASC'); $db->setQuery($query); $rows = $db->loadObjectList(); $list = '<select name="model" class="list-box" id="model" lang="" title="Model">'; $list .= '<option value="" style="padding-left:10px;" selected>-Select Model-</option>'; foreach ($rows as $row) { $list .= '<option value="' . $row->prod_id . '" style="padding-left:10px;">' . $row->prod_name . '</option>'; } $list .= '</select>'; die($list); 

As you can see at the beginning, I've used JInput instead of $_GET.

$ModelId = $jinput->get('s', '', 'INT'); ...the ->get() function takes 3 arguments:

  • Variable name (in your case s)
  • Default value (left if as default but you can use one if you like)
  • Filter (set to INT(integer) assuming this variable will be a number)

There were also some tweaks needed to your ->where() clause in the query.

Hope this helps

4
  • Thanks, awesome - only thing is die($list); in end is necessary as the model is not popping up if this line is not added Commented Jul 29, 2016 at 11:39
  • @Ruchika - That's an odd way of populating a modal but if that's the way you're happy with, fair enough. Added it to the code in my answer as a reference. Commented Jul 29, 2016 at 11:41
  • Thanks - is there any security risk in adding that - die($list); - because withoout it the modal is not popping up Appreciate your time and expertise in replying Commented Jul 29, 2016 at 11:48
  • I wouldn't say security risks. I don't know exactly where you're adding this code but I personally wouldn't use die() when rendering a modal. Commented Jul 29, 2016 at 11:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.