Wordpress invalid argument supplied for foreach - wordpress

Hello here is my website i receive this error on my header site
error code :
Warning
: Invalid argument supplied for foreach() in
/home/enghouse/kastratihome.com/wp-content/themes/funiter/framework/includes/theme-functions.php
on line
828
My code :
823 if ( $funiter_enable_vertical == 1 ) :
824 $locations = get_nav_menu_locations();
825 $menu_id = $locations['vertical_menu'];
826 $menu_items = wp_get_nav_menu_items( $menu_id );
827 $count = 0;
828 foreach ( $menu_items as $menu_item ) {
829 if ( $menu_item->menu_item_parent == 0 ) {
830 $count ++;
831 }
832 }
Here is the code please help me with it...

try{
/* [ your iteration on header.php, line 828, likely something that looks like foreach($foo as $f) { ... } */
}
catch(Exception $e){
/* technically, you don't need to do anything here unless it serves you */
/* but it's common to log an error or return a value */
}

You should make sure that you are passingan array to foreach using the is_array function.
If you are not sure whether you're going to pass an array or not, then you can change your code like the following.
If you are not sure it's going to be an array you can always check using the following PHP example code:
if(is_array($value) || is_object($value)){
foreach ($variable as $item) {
//do something
}
}
Check your foreach() in /home/enghouse/kastratihome.com/wp-content/themes/funiter/framework/includes/theme-functions.php and make sure you check whether it is an array before using foreach().

Related

T_OBJECT_OPERATOR - Drupal 7

this is the php code:
<?php
$tid = $job_node->field_job_cv_destination['und'][0]['tid'];
$term = taxonomy_term_load($tid);
$name = $term->name;
?>
and that is the error:
ParseError: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in -
rules_php_eval() (line 2 -/web/maavarim/sites/all/modules/contrib/rules/modules/php.eval.inc(125) : eval()'d code)
can anyonw know what to do?
Try to implement your code as below
function mytheme_preprocess_page(&$vars, $hook) {
if (isset($vars['node']) && in_array($vars['node']->type, array('article', 'column'))) {
// Set header to topic for articles and columns
// #TODO: consider primary topic field (no multiselect), default to below if empty
$term = taxonomy_term_load($vars['node']->field_topic_ref[LANGUAGE_NONE][0]['tid']);
$vars['head_title'] = $term->name;
}
else {
$vars['head_title'] = $vars['title'];
}
}

ORA-6502, from WEB

I have a problem with Oracle DB.
<?php
require_once 'includes/conn.php';
function connect_db()
{
if ($c=oci_pconnect(uname,pwd, host,'AL32UTF8'))
return $c;
else
die( "ERROR");
}
$conn=connect_db();
$query = "BEGIN :ds_id :=DS.REG_DS1(:F_NAME);END;";
$stmt=oci_parse($conn,$query);
$f_name='John Doe';
$ds_id=-1;
oci_bind_by_name($stmt, ":ds_id", $ds_id);
oci_bind_by_name($stmt, ":F_NAME", $f_name);
if(oci_execute($stmt))
{
echo 'good';
}
else
print_r(oci_error($stmt));
?>
Here is function REG_DS1
FUNCTION REG_DS1(F_NAME IN VARCHAR) RETURN NUMBER AS
DS_ID NUMBER(8,0):=9988;
BEGIN
-- INSERT INTO TEST VALUES(F_NAME,SYSDATE);
RETURN DS_ID;
END REG_DS1;
When I try to execute this function from Sql Developer, it runs with no problem.
But if I execute from PHP script above, it gives me error:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 1 [offset] => 0 [sqltext] => BEGIN :ds_id :=DS.REG_DS1(:F_NAME);END; )
If i change DS_ID variable to another number less than 100, it works great from both. but if i set it to a number more than 99, it gets me error from php script.
What can be a problem?
--jst idea and this is not solutioN
variable decleration or some defination problem
oci_connect($ODBuser,$ODBpass,$ODBhost,$db_charset);
should try like this
not like
oci_pconnect(uname,pwd, host,'AL32UTF8')
sample code :
-- just idea
<?php
$file = "../script/param.CML";
$encryptObj = new cast128;
$encryptObj->setkey("SABBIllustrateKey");
$fp=fopen($file,'r');
$strContent = fread($fp, filesize($file));
fclose($fp);
$strContent=$encryptObj->decrypt($strContent);
$ArConnect=split(" ",$strContent);
$OraService=$ArConnect[8];
$OraUser=$ArConnect[9];
$OraDBpass=$ArConnect[10];
$db_charset = 'AL32UTF8';
$cursor=oci_connect($OraUser,$OraDBpass,$OraService,$db_charset);
if(!$cursor)
{
echo "<br>Error In connection:- reason<BR>";
$e = oci_error(); // For oci_connect errors pass no handle
}
?>
<?php
$file = "../script/param.CML";
$encryptObj = new cast128;
$encryptObj->setkey("SABBIllustrateKey");
$fp=fopen($file,'r');
$strContent = fread($fp, filesize($file));
fclose($fp);
$strContent=$encryptObj->decrypt($strContent);
$con=split(" ",$strContent);
$OraService=$con[8];
$OraUser=$con[9];
$OraDBpass=$con[10];
$db_charset = 'AL32UTF8';
$cursor=oci_connect($OraUser,$OraDBpass,$OraService,$db_charset);
if(!$cursor)
{
echo "<br>Error In connection:- reason<BR>";
$e = oci_error(); // For oci_connect errors pass no handle
}
?>
Thx guys, finally I solved problem myself.
The problem was in variable size of returning value
By changing
oci_bind_by_name($stmt, ":ds_id", $ds_id);
to
oci_bind_by_name($stmt, ":ds_id", $ds_id,10,SQLT_INT );
the problem was solved.

WordPress function get_row() doesn't work properly

I got problem with function get_row() in WordPress and can't find satisfied answer for my situation. That is a piece of my code
function take_category() {
$table_result = array();
$connect_to = connect_to_db();
$question = "select id_cat, name_cat from category";
if (!($result=$connect_to->get_results($question, ARRAY_N))) {
return false;
}
$prepared_row =$connect_to->prepare("%s", $question);
var_dump($prepared_row);
$row=$conntect_to->get_row($prepared_row, ARRAY_N, 1); // line 689
var_dump($row).'</br>';
for ($count=0; $row=$conntect_to->get_row($question, ARRAY_N, 1);
$count++) {
$table_result[$count] = $row;
.........
return $table_result;
this function is placed into functions.php. Function connect_to_db() is defined in this file as well:
function connect_to_db() {
global $wpdb;
$wpdb = new wpdb('****', '', '****store', 'localhost');
if (!$wpdb) {
return false;
} else{
$wpdb->query('SET autocommit = 1;');
return $wpdb;
}
}
Connection to Database is worked properly. Function prepare works correctly as well. This is var_dump($prepared_row):
string(39) "'select id_cat, name_cat from category'"
But next piece of code
$row=$conntect_to->get_row($prepared_row, ARRAY_N, 1); // line 689
var_dump($row);
shows result NULL: and fatal error
***Fatal error:** Uncaught Error: Call to a member function get_row() on
null in ....\functions.php:689 Stack trace: #0 ...\indeks.php(12):
take_category() #1 {main} thrown in ...\functions.php on line 689*
File indeks.php is a file where function take_category() is called:
$cat_table = take_category();
But if I changes my line 689 like this:
$row=$connect_to->get_row("select id_cat, name_cat from category",
ARRAY_N, 1);
I see var_dump($row):
array(2) { [0]=> string(1) "2" [1]=> string(9) "Textbooks" }
and the same fatal error!
What is wrong with function get_row?
Dear gentelments i found the problem. Sorry for waisting your time...
Should $conntect_to->get_row() not be $connect_to->get_row()?
Typo error in conntect_to-> and connect_to->. Use it carefully.
Variable is mispelled. You set $connect_to earlier. However now you are accessing $conntect_to. Rename it to $connect_to.

Display a rounded count of Custom Post Type

I'd like display a rounded up count of the total number of custom post types.
Currently I am using this
<?php
$count_posts = wp_count_posts('listing');
$published_posts = $count_posts->publish;
echo $published_posts;
?>
but would like to round the number to nearest hundred, so for example,
if the count was 120 it would show 100
if the count was 158 it would show 200
if the count was 1088 it would show 1000
Thanks
This question not related to WordPress, but you will get solution by follow
PHP Custom functions: add into functions.php
if( !function_exists('ceiling') )
{
function ceiling($number, $significance = 1)
{
return ( is_numeric($number) && is_numeric($significance) ) ? (round($number/$significance)*$significance) : false;
}
}
if( !function_exists('ceilNumber') )
{
function ceilNumber($number)
{
return ceiling($number, (int)'1'.str_repeat('0', strlen($number)-1));
}
}
Call above function into your code
$count_posts = wp_count_posts('listing');
$published_posts = $count_posts->publish;
echo ceilNumber($published_posts);

get_categories order by meta key issue?

I'm trying to search for a way to order categories by meta value. From what I read, it seems like I can use:
get_categories('child_of=92&hide_empty=false&orderby=meta_value&meta_key=date&order=ASC');
However, this does not work at all, the categories are still not in the order I want. I wonder how I can:
correct this to make it work
print out the sql to see what is really going on inside?
Thank you very much in advance.
First of all, I must mention that I'm using the module custom category fields, and second of all I'm a complete WP newbie
Anyhow, after learning that this cannot be done by default, I looked into the get_categories functions and finally came up with a solution
function category_custom_field_get_terms_orderby( $orderby, $args ){
if($args['orderby'] == 'category_custom_field' && isset($args['category_custom_field']))
return 'cv.field_value';
return $orderby;
}
function category_custom_field_get_terms_fields( $selects, $args ){
if($args['orderby'] == 'category_custom_field' && isset($args['category_custom_field']))
$selects[] = 'cv.*';
return $selects;
}
function category_custom_field_terms_clauses($pieces, $taxonomies, $args){
global $wpdb;
if($args['orderby'] == 'category_custom_field' && isset($args['category_custom_field']))
$pieces['join'] .= " LEFT JOIN $wpdb->prefix" . "ccf_Value cv ON cv.term_id = tt.term_id AND cv.field_name = '".$args['category_custom_field']."'";
return $pieces;
}
add_filter('get_terms_orderby', 'category_custom_field_get_terms_orderby',1,2);
add_filter('get_terms_fields', 'category_custom_field_get_terms_fields',1,2);
add_filter('terms_clauses', 'category_custom_field_terms_clauses',1,3);
(The code above can be put into the theme functions.php file)
then the code to get categories is:
get_categories('child_of=92&hide_empty=false&orderby=category_custom_field&category_custom_field=date&order=DESC');
Any correction is greatly appreciated!
You can also give the get_categories new meta and sort using usort.
$subcategories = get_categories();
foreach ($subcategories as $subcategory) {
$subcategory->your_meta_key = your_meta_value;
}
foreach ($subcategories as $subcategory) {
blah blah blah
}
function my_cmp($a, $b) {
if ($a->ordering == $b->ordering) {
return 0;
}
return ($a->ordering < $b->ordering) ? -1 : 1;
}
usort($subcategories, "my_cmp");

Resources