How do I overcome the “undefined offset 0” and “undefined variable:str” notice in PHP?

I have an if and array statement in my PHP which is showing the notice undefined offset & undefined variable. How do I overcome those two errors in below code?

undefined offset 0

Read More

undefined variable:str

 function calc_pending_conf(){
$comm = array();
global $wpdb;
$unsecurearray = array();
$nps = $wpdb->get_results("SELECT ID FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' AND comment_status = 'open'");
$numposts=0;
foreach($nps as $np){
    if (comments_open($np->ID)){
        $numposts++;
        $unsecurearray[]=$np->ID;
    }
}
$allusers = $wpdb->get_results("SELECT ID FROM wp_users");
foreach ($allusers as $usr):
    $comcount=0;
    $args = array('user_id' => $usr->ID);
    $usercomment = get_comments($args);
    foreach($usercomment as $cot) :
        if(in_array($cot->comment_post_ID,$unsecurearray)){$comcount++;}
    endforeach;
    $snumposts = $wpdb->get_var("SELECT COUNT(*) FROM wp_ps_security, wp_posts WHERE wp_ps_security.sec_protect_id = wp_posts.ID AND wp_posts.post_type = 'post' AND wp_posts.comment_status = 'open'");
    $unsecure = $numposts - $snumposts;
    $cid = $usr->ID;
    $groupid = $wpdb->get_results("SELECT grel_group_id FROM wp_ps_group_relationships WHERE grel_user_id = '".$cid."'");
    if(isset($groupid) && is_array($groupid) && is_object($groupid[0])):
    if($groupid[0]->grel_group_id==11 || $groupid[0]->grel_group_id==3 || $groupid[0]->grel_group_id==1 || sizeof($groupid)==0):
        $pendingc = 0;
    else:
        $applicable=0;
        foreach($groupid as $grp){
            $str = $str."wp_ps_security.sec_access_id=".$grp->grel_group_id." OR ";
        }
        global $applicablearray;
        $applicablearray = array();
        $aarray = $wpdb->get_results("SELECT DISTINCT wp_ps_security.sec_protect_id FROM wp_posts,wp_ps_security WHERE wp_posts.comment_status='open' AND wp_posts.post_status='publish' AND wp_posts.post_type='post' AND (".substr($str, 0, -3).") AND wp_posts.ID=wp_ps_security.sec_protect_id");
        foreach ( $aarray as $aa ) 
        {
            $applicablearray[]=$aa->ID;
        }
        $applicable = sizeof($applicablearray);
        
        $pendingc = $unsecure + $applicable - $comcount;
    endif;
    $wpdb->query( "UPDATE wp_users SET confpending=".$pendingc." WHERE ID=".$cid);
    update_user_meta( $cid, 'confpending', $pendingc );
endforeach;

}

Related posts

Leave a Reply

3 comments

  1. php checks if conditions lazily and he won’t continue forward if something fails the if.

    For example IF (A AND B), if A is false, B is never checked.

    You can use this in your example, you can check isset($groupID[0]) AND (everything else).

    if $groupID[0] isn’t set, the whole if check is ignored.

  2. Don’t use variables that are undefined?

    Make use of isset to ensure that a variable exists before trying to access it.

    Personally, and do not do this yourself, but personally I set error reporting to E_ALL ^ E_NOTICE. It’s a bad idea and I say again you should not do this, but it is an option if you’re willing to accept the risk that things will silently screw up if you mis-type a single variable name.

  3. Check $groupid before:

        // you just need insert this line before your if statement:
        if(isset($groupid) && is_array($groupid) && is_object($groupid[0]))
        // Your code
        if($groupid[0]->grel_group_id==11 || $groupid[0]->grel_group_id==3 || $groupid[0]->grel_group_id==1 || sizeof($groupid)==0):