T_OBJECT_OPERATOR - Drupal 7 - drupal

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'];
}
}

Related

Fatal error: Uncaught Error: Cannot use object of type WP_Error as array

Am trying to fetch user data from wordpress table. When I put the data into an array it shows me an fatal error.
This is my php code:
$getuser_data = $wpdb->get_results($user_query);
$userdata = array();
foreach($getuser_data as $data)
{
$userfeed['username'] = $data->user_login;
$userfeed['name'] = $data->user_email;
$userfeed['email'] = $data->display_name;
$userfeed['user_id'] = $data->ID;
$userfeed['profile_pic'] = get_wp_user_avatar_src($data->ID);
$usercount = count($getuser_data);
}
$userdata[] = $userfeed;
Its showing the fatal error Cannot use object of type WP_Error as array in last line $userdata[].
Any help would be appreciated.
Thanks in advance.
There might be something wrong with the $user_query and as a result $wpdb->get_results($user_query) is returning WP_Error object instead of the query result. Wrap the code inside the following block and check what's the error.
if( !is_wp_error( $getuser_data ) ) {
$userdata = array();
foreach($getuser_data as $data)
{
$userfeed['username'] = $data->user_login;
$userfeed['name'] = $data->user_email;
$userfeed['email'] = $data->display_name;
$userfeed['user_id'] = $data->ID;
$userfeed['profile_pic'] = get_wp_user_avatar_src($data->ID);
$usercount = count($getuser_data);
$userdata[] = $userfeed;
}
} else {
echo $getuser_data->get_error_message();
}

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.

Wrap the code to translate with wordpress/poedit?

Current I have WP code like this. I need to make it translateable by poedit. How do I wrap the code to make it work? Im not sure which method is use for this case. Some thing like:
<?php my_e( 'Total sales' ); ?> or __('Total sales', 'my')
This is the code. I need to translate ["Sales amount"], ["Number of sales"]
foreach ($results as $result) {
$date = $result->formatted_post_date;
$statistics[$date]["Sales amount"] += $wp_list_table->column_total_sales($result->ID);
$statistics[$date]["Number of sales"]++;
$statistics[$date]["date"] = $date;
$max_number_of_sales = max(array($max_number_of_sales,$statistics[$date]["Number of sales"] )); }
Thank you for help
You have to use __('string','textdomain') to assign a translated string to some variable. And _e('string','textdomain') to echo a translated string. See I18n_for_WordPress_Developers.
Two observations:
you'll not be able to translate array keys, see php.net/manual/en/language.types.array.php
what you're doing seems wrong. I'd do it like:
$sales_amount = 0;
$sales_number = 0;
foreach ($results as $result) {
$sales_amount += $wp_list_table->column_total_sales($result->ID);
$sales_number++;
$date = $result->formatted_post_date;
$statistics[$date]["sales_amount"] = $sales_amount;
$statistics[$date]["sales_number"] = $sales_number;
}
echo __( 'Sales Amount', 'my' ) . $sales_amount;

How to execute a php loop in an andWhere clause using QueryBuilder?

I would like to execute this type of query using QueryBuilder in my FakeRepository.php (it's for a search form where the user can check some boxes).
if (sizeof($p['types']) > 0) {
$qb->andWhere(
foreach ($p['types'] as $type_id)
{'type.id=' .$type_id.' OR ' }
'1=0');
}
But I have an error with my syntax but I don't know how to fix it :
Parse error: syntax error, unexpected T_FOREACH, expecting ')' in /MyBundle/Entity/FakeRepository.php
Thanks a lot for your help
You need to first construct your OR condition and then pass it to the query builder
$first = true;
$orQuery = '';
foreach ($p['types'] as $type_id)
if ($first) {
$first = false;
} else {
$orQuery .= ' OR ';
}
$orQuery .= 'type.id=' .$type_id;
}
$qb->andWhere($orQuery);
You can also resove this problem:
$arr = array();
foreach ($p['types'] as $type_id){
$arr[] = $qb->expr()->orX("type.id = $type_id");
}
$qb->andWhere(join(' OR ', $arr));
Another solution to keep QueryBuilder functionality
$orX = $qb->expr()->orX();
foreach ($types as $key => $type) {
$orX->add($qb->expr()->eq('types', ':types'.$key));
$qb->setParameter('types'.$key, $type->getId());
}
$qb->andWhere($orX);

Wordpress - Excerpt character alternative?

I'm totally new to WordPress so be easy :)
I the following code in a template:
<?php excerpt(20);?>
What this does is limit the text with 20 words. I am now wondering if there is some sort of similar function that limits by characters instead of words?
Thanks!
I use this:
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return '500';
}
function better_excerpt($limit, $id = '') {
global $post;
if($id == '') $id = $post->ID;
else $id = $id;
$postinfo = get_post($id);
if($postinfo->post_excerpt != '')
$post_excerpt = $postinfo->post_excerpt;
else
$post_excerpt = $postinfo->post_content;
$myexcerpt = explode(' ', $post_excerpt, $limit);
if (count($myexcerpt) >= $limit) {
array_pop($myexcerpt);
$myexcerpt = implode(' ',$myexcerpt).'...';
} else {
$myexcerpt = implode(' ',$myexcerpt);
}
$myexcerpt = preg_replace('`\[[^\]]*\]`','',$myexcerpt);
$stripimages = preg_replace('/<img[^>]+\>/i', '', $myexcerpt);
return $stripimages;
}
And then in my theme file, I just call it in with:
better_excerpt('50') //50 being how many words I want
Useful for custom plugins/widgets too.
Wordpress doesn't support the character delimiter for the excerpt method, there's a plugin called Advanced Excerpt that does. After installing you can call the_advanced_excerpt('length=20&use_words=0')
I use this in my functions.php:
function truncate ($str, $length=10, $trailing='...'){
// take off chars for the trailing
$length-=mb_strlen($trailing);
if (mb_strlen($str)> $length){
// string exceeded length, truncate and add trailing dots
$str = mb_substr($str,0,$length);
$str = explode('. ',$str);
for( $i=0; $i<(sizeof($str)-2); $i++ ):
$newstr .= $str[$i].". ";
endfor;
return $newstr;
} else{
// string was already short enough, return the string
$res = $str;
}
return $res;
}
It should truncate to a character count, but then truncate back further to the last period before the truncation. It does get problematic when your excerpt includes links, however, or other markup - in other words, it's best to use the Excerpt field in the post rather than auto-excerpting with this function, because you can't use HTML in the excerpt field.
Please use this code for limiting post content...
<?php substr($post->post_content, 0, xy); ?> ...
Change the limit of XY....

Resources