Plugin Directory

Changeset 2180665


Ignore:
Timestamp:
10/26/2019 02:55:55 PM (6 years ago)
Author:
magent
Message:

Added vtm_count function for counting items in arrays or normally non-countable objects of the type returned by wpdb as default, updated for PHP7 error about counting non-countable objects

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vampire-character/trunk/inc/functions.php

    r2056053 r2180665  
    154154    $sql = "select DESCRIPTION, WP_PAGE_ID from " . VTM_TABLE_PREFIX . "ST_LINK where VALUE = %s;";
    155155    $results = $wpdb->get_row($wpdb->prepare($sql, $stlinkvalue));
    156    
     156       
    157157    $pageid = "Page not matched";
    158     if (count($results) == 1) {
     158    if (is_object($results)) {
    159159        $pages = get_pages();
     160       
    160161        foreach ( $pages as $page ) {
    161162            if ($page->ID == $results->WP_PAGE_ID) {
    162163                $pageid = $page->post_title;
    163164            }
    164         }       
    165     }
     165        }
     166    } 
    166167    return $pageid;
    167168
     
    174175   
    175176    $url = "Page not matched";
    176     if (count($results) == 1) {
     177    if (is_object($results)) {
    177178        $url = get_page_link($results->WP_PAGE_ID);
    178179    }
     
    931932        global $vtmglobal;
    932933
    933         $sql = "SELECT id
    934                 FROM " . VTM_TABLE_PREFIX . "CHARACTER
    935                 WHERE WORDPRESS_ID = %s";
    936         $cid = $wpdb->get_var($wpdb->prepare($sql, $character));
     934        if (!empty($character)) {
     935            $sql = "SELECT id
     936                    FROM " . VTM_TABLE_PREFIX . "CHARACTER
     937                    WHERE WORDPRESS_ID = %s";
     938            $cid = $wpdb->get_var($wpdb->prepare($sql, $character));
     939        } else {
     940            $cid = "";
     941        }
    937942       
    938943        if (empty($cid) && isset($_REQUEST['characterID'])) {
     
    944949            $vtmglobal['characterID'] = $cid;
    945950        }
    946 
     951       
    947952        return $cid;
    948953    }
     
    14451450    $data = $wpdb->get_results($sql);
    14461451   
    1447     if (count($data) > 0) {
     1452    if (vtm_count($data) > 0) {
    14481453        $i = 1;
    14491454        foreach ($data as $row) {
     
    15971602    return $report . $content;
    15981603}
     1604
     1605function vtm_count($input) {
     1606   
     1607    if (is_countable($input)) {
     1608        return count($input);
     1609    }
     1610    elseif (is_object($input)) {
     1611        //print_r($input);
     1612        $count = 0;
     1613        foreach ($input as $item) {
     1614            $count++;
     1615        }
     1616        //print "count is $count";
     1617        return $count;
     1618    }
     1619   
     1620    return 0;
     1621}
    15991622?>
Note: See TracChangeset for help on using the changeset viewer.