Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Invalid argument supplied for foreach()
Forum PHP.pl > Forum > PHP
k3d4rkk
Witam
Od razu pisze że nie mam zielonego pojęcia nt. programowania w PHP problem polega na tym że wgrałem templatkę do zainstalowanego CMSa i w poglądzie wywala mi błąd na stronie a mianowicie:

Warning: Invalid argument supplied for foreach() in /homepages/.........../Base.class.php on line 152

Poniżej kod PHP pliku Base.class.php
  1. <?php
  2. /*.
  3. .Tu jest tekst ale zastapiłem go .
  4. .Liczba kropek zgadza się z ilością linijek
  5. .
  6. .
  7. .
  8. .
  9. .
  10. .*/
  11.  
  12. defined( '_VALID_MOS' ) or defined('_JEXEC') or die('Restricted access');
  13.  
  14. if (!defined ('_JA_BASE_MENU_CLASS')) {
  15. define ('_JA_BASE_MENU_CLASS', 1);
  16.  
  17. class JA_Base{
  18. var $_params = null;
  19. var $children = null;
  20. var $open = null;
  21. var $items = null;
  22. var $Itemid = 0;
  23. var $_db = null;
  24. var $showSeparatedSub = false;
  25.  
  26. function JA_Base( &$params ){
  27. global $Itemid;
  28. $this->_params = $params;
  29. $this->Itemid = $Itemid;
  30. $this->loadMenu();
  31. }
  32.  
  33. function loadMenu(){
  34. $this->loadMenu_15();
  35. }
  36.  
  37. function createParameterObject($param, $path='', $type='menu') {
  38. return new JParameter($param, $path);
  39. }
  40.  
  41. function loadMenu_10x(){
  42. global $my, $mosConfig_shownoauth, $mainframe, $database;
  43.  
  44. if ($mosConfig_shownoauth) {
  45. $sql = "SELECT m.* FROM #__menu AS m"
  46. . "\nWHERE menutype='". $this->getParam( 'menutype' ) ."' AND published='1'"
  47. . "\nORDER BY parent,ordering";
  48. } else {
  49. $sql = "SELECT m.* FROM #__menu AS m"
  50. . "\nWHERE menutype='". $this->getParam( 'menutype' ) ."' AND published='1' AND access <= '$my->gid'"
  51. . "\nORDER BY parent,ordering";
  52. }
  53. $database->setQuery( $sql );
  54. $rows = $database->loadObjectList( 'id' );
  55.  
  56. // first pass - collect children
  57. $cacheIndex = array();
  58. $this->items = array();
  59. foreach ($rows as $index => $row) {
  60. if ($row->access <= $my->gid) {
  61. $pt = $row->parent;
  62. $list = @ $children[$pt] ? $children[$pt] : array ();
  63.  
  64. switch ($row->type) {
  65. case 'separator':
  66. case 'component_item_link':
  67. break;
  68.  
  69. case 'url':
  70. if ( eregi( 'index.php\?', $row->link ) ) {
  71. if ( !eregi( 'Itemid=', $row->link ) ) {
  72. $row->link .= '&Itemid='. $row->id;
  73. }
  74. }
  75. break;
  76.  
  77. case 'content_item_link':
  78. case 'content_typed':
  79. // load menu params
  80. $menuparams = $this->createParameterObject( $row->params, $mainframe->getPath( 'menu_xml', $row->type ), 'menu' );
  81.  
  82. $unique_itemid = $menuparams->get( 'unique_itemid', 1 );
  83.  
  84. if ( $unique_itemid ) {
  85. $row->link .= '&Itemid='. $row->id;
  86. } else {
  87. $temp = split('&task=view&id=', $row->link);
  88.  
  89. if ( $row->type == 'content_typed' ) {
  90. $row->link .= '&Itemid='. $mainframe->getItemid($temp[1], 1, 0);
  91. } else {
  92. $row->link .= '&Itemid='. $mainframe->getItemid($temp[1], 0, 1);
  93. }
  94. }
  95. break;
  96.  
  97. default:
  98. $row->link .= '&Itemid='. $row->id;
  99. break;
  100. }
  101.  
  102. if ( strcasecmp( substr( $row->link,0,4 ), 'http' ) ) {
  103. $row->link = sefRelToAbs( $row->link );
  104. }
  105.  
  106. $row->url = $row->url = ampReplace( $row->link );
  107.  
  108. array_push($list, $row);
  109. $children[$pt] = $list;
  110. }
  111. $cacheIndex[$row->id] = $index;
  112. $this->items[$row->id] = $row;
  113. }
  114.  
  115. $this->children = $children;
  116. // second pass - collect 'open' menus
  117. $open = array ($this->Itemid);
  118. $count = 20; // maximum levels - to prevent runaway loop
  119. $id = $this->Itemid;
  120.  
  121. while (-- $count)
  122. {
  123. if (isset($cacheIndex[$id])) {
  124. $index = $cacheIndex[$id];
  125. if (isset ($rows[$index]) && $rows[$index]->parent > 0) {
  126. $id = $rows[$index]->parent;
  127. $open[] = $id;
  128. } else {
  129. break;
  130. }
  131. }
  132. }
  133. $this->open = $open;
  134. // $this->items = $rows;
  135. }
  136.  
  137. function loadMenu_15(){
  138. $menu = @JMenu :: getInstance();
  139. if(strtolower(get_class($menu)) == 'jexception') {
  140. $menu = @JMenu :: getInstance('site');
  141. }
  142. $user =& JFactory::getUser();
  143. $children = array ();
  144.  
  145. // Get Menu Items
  146. $items = &JSite::getMenu();
  147. $rows = $items->getItems('menutype', $this->getParam('menutype'));
  148. // first pass - collect children
  149. $cacheIndex = array();
  150. $this->items = array();
  151. $aid = $user->get('aid', 0);
  152. foreach ($rows as $index => $v) {
  153. if ($v->access <= $aid) {
  154. $pt = $v->parent;
  155. $list = @ $children[$pt] ? $children[$pt] : array ();
  156.  
  157. switch ($v->type)
  158. {
  159. case 'url' :
  160. if ((strpos($v->link, 'index.php?') !== false) && (strpos($v->link, 'Itemid=') === false)) {
  161. $v->url = $v->link.'&amp;Itemid='.$v->id;
  162. } else {
  163. $v->url = $v->link;
  164. }
  165. break;
  166.  
  167. default :
  168. coś tam dalej ale za dużo znaków

Proszę o pomoc bo kompletnie się nie znam na tym blinksmiley.gif a może gdzie indziej tkwi błąd questionmark.gif
Spawnm
Invalid argument supplied for foreach()
Czyli to co dajesz w foreach nie jest tablicą.

Daj var_dump($rows); przed pętlą i zobacz co tam masz.
k3d4rkk
a czy możesz napisac w której linijce to mam wstawic bo jak pisałem na samym początku nie mam zielonego pojęcia questionmark.gif
Smertius
Przed foreach w 152 lini

waqmaz
  1. $list = @ $children[$pt]
Czemu tłumisz znakiem @?
k3d4rkk
Cytat(Smertius @ 1.02.2011, 21:49:50 ) *
Przed foreach w 152 lini

Wstawiłem ta linijkę to mi błędy wyskoczyły a konkretnie cała litania
Kod
array(3) { [0]=> object(stdClass)#105 (25) { ["id"]=> string(1) "1" ["menutype"]=> string(8) "mainmenu" ["name"]=> string(15) "Strona główna" ["alias"]=> string(5) "start" ["link"]=> string(43) "index.php?option=com_content&view=frontpage" ["type"]=> string(9) "component" ["published"]=> string(1) "1" ["parent"]=> string(1) "0" ["componentid"]=> string(2) "20" ["sublevel"]=> string(1) "0" ["ordering"]=> string(1) "1" ["checked_out"]=> string(1) "0" ["checked_out_time"]=> string(19) "0000-00-00 00:00:00" ["pollid"]=> string(1) "0" ["browserNav"]=> string(1) "0" ["access"]=> string(1) "0" ["utaccess"]=> string(1) "3" ["params"]=> string(548) "num_leading_articles=5 num_intro_articles=1 num_columns=1 num_links=1 orderby_pri= orderby_sec=front multi_column_order=1 show_pagination=2 show_pagination_results=1 show_feed_link=1 show_noauth= show_title= link_titles= show_intro= show_section= link_section= show_category= link_category= show_author= show_create_date= show_modify_date= show_item_navigation=1 show_readmore= show_vote= show_icons=0 show_pdf_icon=0 show_print_icon=0 show_email_icon=0 show_hits= feed_summary= page_title= show_page_title=1 pageclass_sfx= menu_image=-1 secure=0 " ["lft"]=> string(1) "0" ["rgt"]=> string(1) "0" ["home"]=> string(1) "1" ["component"]=> string(11) "com_content" ["tree"]=> array(1) { [0]=> string(1) "1" } ["route"]=> string(5) "start" ["query"]=> array(2) { ["option"]=> string(11) "com_content" ["view"]=> string(9) "frontpage" } } [1]=> object(stdClass)#108 (25) { ["id"]=> string(1) "3" ["menutype"]=> string(8) "mainmenu" ["name"]=> string(9) "Logowanie" ["alias"]=> string(9) "logowanie" ["link"]=> string(36) "index.php?option=com_user&view=login" ["type"]=> string(9) "component" ["published"]=> string(1) "1" ["parent"]=> string(1) "0" ["componentid"]=> string(2) "14" ["sublevel"]=> string(1) "0" ["ordering"]=> string(1) "3" ["checked_out"]=> string(1) "0" ["checked_out_time"]=> string(19) "0000-00-00 00:00:00" ["pollid"]=> string(1) "0" ["browserNav"]=> string(1) "0" ["access"]=> string(1) "0" ["utaccess"]=> string(1) "0" ["params"]=> string(370) "show_login_title=1 header_login= login= login_message=0 description_login=0 description_login_text= image_login= image_login_align=right show_logout_title=1 header_logout= logout= logout_message=1 description_logout=1 description_logout_text=Zostałeś poprawnie wylogowany. Dziękujemy image_logout= page_title= show_page_title=1 pageclass_sfx= menu_image=-1 secure=0 " ["lft"]=> string(1) "0" ["rgt"]=> string(1) "0" ["home"]=> string(1) "0" ["component"]=> string(8) "com_user" ["tree"]=> array(1) { [0]=> string(1) "3" } ["route"]=> string(9) "logowanie" ["query"]=> array(2) { ["option"]=> string(8) "com_user" ["view"]=> string(5) "login" } } [2]=> object(stdClass)#110 (25) { ["id"]=> string(1) "4" ["menutype"]=> string(8) "mainmenu" ["name"]=> string(11) "Rejestracja" ["alias"]=> string(11) "rejestracja" ["link"]=> string(39) "index.php?option=com_user&view=register" ["type"]=> string(9) "component" ["published"]=> string(1) "1" ["parent"]=> string(1) "0" ["componentid"]=> string(2) "14" ["sublevel"]=> string(1) "0" ["ordering"]=> string(1) "4" ["checked_out"]=> string(1) "0" ["checked_out_time"]=> string(19) "0000-00-00 00:00:00" ["pollid"]=> string(1) "0" ["browserNav"]=> string(1) "0" ["access"]=> string(1) "0" ["utaccess"]=> string(1) "0" ["params"]=> string(69) "page_title= show_page_title=1 pageclass_sfx= menu_image=-1 secure=0 " ["lft"]=> string(1) "0" ["rgt"]=> string(1) "0" ["home"]=> string(1) "0" ["component"]=> string(8) "com_user" ["tree"]=> array(1) { [0]=> string(1) "4" } ["route"]=> string(11) "rejestracja" ["query"]=> array(2) { ["option"]=> string(8) "com_user" ["view"]=> string(8) "register" } } } NULL
Warning: Invalid argument supplied for foreach() in /homepages/........./Base.class.php on line 153

Nie wiem za co ten kawałek kodu był odpowiedzialny ale usunąłem tą linijkę czyli:
foreach ($rows as $index => $v) { oraz gdzieś tam niżej } i działa nie wywala błędów winksmiley.jpg
waqmaz
shit, w takim bałaganie to ja się nie połapię =/
foreach sluzy do tworzenia kluczy (zapewne wiesz) np:
czyli twoim bledem bylo to chyba ze nie dales tablicy albo nie wyswietliles tablicy zmiennej haha.gif
  1. <?php
  2.  
  3. $babcia =array('helena', 'magda', 'marycha');
  4.  
  5. foreach($babcia as $czlonek_rodziny) {
  6. echo $czlonek_rodziny . " :* ";
  7.  
  8. }
  9. ?>


...dostaniesz buziaka od każdej z babci za pomoca foreach haha.gif
niestety w kodzie cos twoim nie wiem o co chodzi, wiec tlyk ci przedstawilem dzialanie foreach.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.