joomla2.5joomla-component

Fetch category and article list joomla


I am using joomla 2.5 and I want to fetch a list of distinct catagories and article list.

I'm using the following code to fetch all categories but I'm unable to get the category id to use it on another insert.

How can I fetch these distinct field ids also in select list given below.

$db = &JFactory::getDBO();
$query = "SELECT DISTINCT title FROM #__categories WHERE published = '1' ORDER BY level";
$db->setQuery($query);
$rows = $db->loadObjectList();
?>
<?php $catagories = (isset($this->settings['catagories']) ? $this->settings['catagories'] : "");?>
<select id="catagories" name="settings[catagories]">
<option value="default" selected="selected">---Default---</option>
<?php foreach ($rows as $row) {?>
<option <?php //if ($row->id == $catagories) { echo " selected=\"selected\""; } ?>value="<?php //echo $row->id;?>" >
<?php echo $row->title;?>
  </option>
<?php }?>
</select>

Solution

  • <?php 
    $db = &JFactory::getDBO();
    $query = "SELECT DISTINCT title FROM #__categories WHERE published = '1' ORDER BY level";
    $db->setQuery($query);
    $rows = $db->loadObjectList();
    ?>
    <?php foreach ($rows as $row) {
    $query = "SELECT id FROM #__categories WHERE title = " . $row->title . " AND published = '1'  LIMIT 1 ";
    $db->setQuery($query);
    $rowid = $db->loadResult();
    ?>
    <option value="<?php echo $rowid;?>" >
    <?php echo $row->title;?>
      </option>
    <?php }?>