Jump to content

Php and MySQL help?


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 kinda 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!

Link to comment
https://forums.phpfreaks.com/topic/219130-php-and-mysql-help/
Share on other sites

The exact warning when i search in Joomla with the plugin enabled is this:  ( Replaced my root path with *)

 

Warning: Invalid argument supplied for foreach() in /*/*/*/*/*/*/html/plugins/search/hdflvsearch.php on line 110

 

the foreach is the foreach after the query.

Link to comment
https://forums.phpfreaks.com/topic/219130-php-and-mysql-help/#findComment-1136363
Share on other sites

Warning: Invalid argument supplied for foreach() in /*/*/*/*/*/*/html/plugins/search/hdflvsearch.php on line 110

 

This warning means that $rows is NOT an array.  Which probably means that your query is failing ($db->setQuery).  Double check that it is correct and echo out your variables to ensure the values used in the query are what you expect.

Link to comment
https://forums.phpfreaks.com/topic/219130-php-and-mysql-help/#findComment-1136900
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.