0

I found a strange behavior of my functions.

I thought to pass the id from display_rows() to column_cb(), but the problem is that the program passes only the first digit. Any hint?

column_cb():

function column_cb($item) {
    return sprintf('<input type="checkbox" name="id[]" value="%s" />', $item['id']);
}

display_rows():

function display_rows() {
    $records = $this->items;
    list( $columns, $hidden ) = $this->get_column_info();

    if(!empty($records)){
        foreach($records as $rec) {
            echo '<tr id="record_'.$rec->id.'">';
            foreach ( $columns as $column_name => $column_display_name ) {
                $class = "class='$column_name column-$column_name'";
                $style = "";
                if ( in_array( $column_name, $hidden ) ) $style = 'style="display:none;"';
                    $attributes = $class . $style;
                    $editlink  = '/wp-admin/link.php?action=edit&id='.(int)$rec->id;

                    //Display the cell
                    switch ( $column_name ) {
                        case "cb":  echo '<th scope="row" class="check-column">'.$this->column_cb(stripslashes($rec->id)).'</th>'; break;
                        case "col_name":    echo '<td '.$attributes.'><strong>'.$this->column_title(stripslashes($rec->product_name_name)).'</strong></td>'; break;
                    }
             }
                    echo'</tr>';
        }
     }
 }

The same problem happens if I try to pass the value name to column_title().

column_title():

function column_title($item){
    $actions = array(
                     'edit'      => sprintf('<a href="?page=%s&action=%s&product=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),
                     'delete'    => sprintf('<a href="?page=%s&action=%s&product=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']),
        );

       //Return the title contents
       return sprintf('<a href="#"><b>%1$s</b></a> %3$s',
            /*$1%s*/ $item['product_name'],
            /*$2%s*/ $item['id'],
            /*$3%s*/ $this->row_actions($actions)
        );
    }
  • If you dump the passed $item does it contain expected data? – Rarst May 29 '14 at 13:59
  • Nope, it prints: string(1) "1"instead of string(1) "15". In fact the program works great with numbers from 1 to 9, and it has problem with number with 2 or N digits. But I do not understand why. If I print $rec->id in the display_rows() it prints for example 15, in the column_cb() or column_title() it prints 1. – Mark May 29 '14 at 14:40
  • If I write everything in display_rows() like this: case "cb": echo '<th scope="row" class="check-column"><input type="checkbox" name="id[]" value="'.stripslashes($rec->id).'" /></th>'; break; everything seems working, but I need to use the other functions! Any hint? – Mark May 29 '14 at 15:29
0

Instead of $item['id'] I have to write $item. Fixed!

  • Just a suggestion, whenever you develop a theme/plugin, set your debug to true in wp-config.php. Most problems are easily solved by just doing this one simple thing, as all problems in code is printed to screen – Pieter Goosen May 29 '14 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.