Jump to content

Trying to code a Joomla Search plugin.


lukejd83

Recommended Posts

Hi all!  I have racked my brain trying to figure this one out.  I'm typically a smart cookie, but since I am new to php and mysql, I'm stumped.  I'm just trying to program a Joomla search plugin that queries my HDFLV player database, so videos are searchable, but I keep getting an invalid "foreach(" command.  It's supposed to be there to interpret the database query, right?

 

I'll post my code here, can someone give me some insight?

 


<?php
/**
* @version		$Id: contacts.php 10381 2008-06-01 03:35:53Z pasamio $
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$mainframe->registerEvent( 'onSearch', 'plgSearchhdflvplayer' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchhdflvplayerAreas' );

JPlugin::loadLanguage( 'plg_search_hdflvplayer' );

/**
* @return array An array of search areas
*/
function &plgSearchhdflvplayerAreas()
{
static $areas = array(
	'hdflvplayer' => 'Videos'
);
return $areas;
}

/**
* Contacts Search method
*
* The sql must return the following fields that are used in a common display
* routine: href, title, section, created, text, browsernav
* @param string Target search string
* @param string mathcing option, exact|any|all
* @param string ordering option, newest|oldest|popular|alpha|category
*/
function plgSearchhdflvplayer( $text, $phrase='', $ordering='', $areas=null )
{
$db		=& JFactory::getDBO();
$user	=& JFactory::getUser();

if (is_array( $areas )) {
	if (!array_intersect( $areas, array_keys( plgSearchhdflvplayerAreas() ) )) {
		return array();
	}
}

// load plugin params info
	$plugin =& JPluginHelper::getPlugin('search', 'hdflvplayer');
	$pluginParams = new JParameter( $plugin->params );

$limit = $pluginParams->def( 'search_limit', 50 );

$text = trim( $text );
if ($text == '') {
	return array();
}

$section = JText::_( 'hdflvplayer' );

switch ( $ordering )
{
	case 'oldest':
		$order = 'a.id ASC';
		break;

	case 'popular':
		$order = 'a.times_viewed DESC';
		break;

	case 'alpha':
		$order = 'a.title ASC';
		break;

	case 'category':
		$order = 'b.title ASC, a.title ASC';
		break;

	case 'newest':
	default:
		$order = 'a.id DESC';
}

$text	= $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
$query	= 'SELECT a.title AS title, "" AS created,'
. ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, '
. ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(\':\', b.id, b.alias) ELSE b.id END AS catslug, '
. ' CONCAT_WS( ", ", a.title, a.description ) AS text,'
. ' CONCAT_WS( " / ", '.$db->Quote($section).', b.title ) AS section,'
. ' "2" AS browsernav'
. ' FROM #__hdflvplayerupload AS a'
. ' INNER JOIN #__hdflvplayername AS b ON b.id = a.catid'
. ' WHERE ( a.title LIKE '.$text
. ' OR a.description LIKE '.$text.' )'
. ' AND a.published = 1'
. ' AND b.published = 1'
. ' AND a.access <= '.(int) $user->get( 'aid' )
. ' AND b.access <= '.(int) $user->get( 'aid' )
. ' GROUP BY a.id'
. ' ORDER BY '. $order
;
$db->setQuery( $query, 0, $limit );
$rows = $db->loadObjectList();

foreach($rows as $key => $row) {
	$rows[$key]->href = '?pid='.$row->slug;
}

return $rows;
}


 

 

ANY help is more than what I got from HDFLV Player support.

 

Thank you so much in advance!

 

[attachment deleted by admin]

Link to comment
Share on other sites

what's the exact error? If it's related to the foreach, most likely it's because $rows isn't an array. You'll get an array if you try to pass something other than an array to a foreach loop. I would print your query to the browser, then try to run it directly in mySQL to see if the query is failing (which is one reason why $rows wouldn't be an array)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.