How to use checkboxes to retrieve specific data in a database

前端 未结 1 473
既然无缘
既然无缘 2021-01-26 01:02

Lately i have been working on a form with which a user can select a checkbox and as a result of the selection it shows the database information in a table.

I browsed the

1条回答
  •  隐瞒了意图╮
    2021-01-26 01:43

    I see at least 2 errors in your code.

    1. the $column_names associative array values are supposed to be passed as field names, so I assume that they are not correct, as you have spaces in them (and as I know wordpress by default does not have such field names.

    2. if some selection is provided by user you are adding some extra field names to the once which are passed by user and you have a colon after them so it will generate an error.

    I would rewrite the code like this

     '`field1`', '2' => '`field2`', '3' => '`field3`');
    if(isset($_POST['columns'])){
        $column_entries = $_POST['columns'];
        $sql_columns = array();
        foreach($column_entries as $i) {
            if(array_key_exists($i, $column_names)) {
                $sql_columns[] = $column_names[$i];
            }
        }
        $sql_columns[] = "authorss";
        $sql_columns[] = "research_source";
        $sql_columns[] = "research_title";
    } else {
        $all = true;
        $sql_columns[] = "*";
    }
    

    Also as you have said $wpdb->get_results returns already the results - array so that's why you get the errors. Plus before calling mysql_fetch_assoc it is better to check if the passed parameter is recource and if the number of rows is not 0.

    if($result!==false && mysql_num_rows($result)>0){
        while( $row = mysql_fetch_assoc($result)){
            ...
        }
    }  
    

    *********** UPDATE ***********

    according to last changes try this code:

     '`authorss`', '2' => '`research_source`', '3' => '`research_title`');
    if(isset($_POST['columns'])){
        $column_entries = $_POST['columns'];
        $sql_columns = array();
        foreach($column_entries as $i) {
            if(array_key_exists($i, $column_names)) {
                $sql_columns[] = $column_names[$i];
            }
        }
    } else {
        $all = true;
        $sql_columns[] = "authorss";
        $sql_columns[] = "research_source";
        $sql_columns[] = "research_title";
    }
    
    global $wpdb;
    
    //DNI CHECKBOX + ALL
    $tmp = $wpdb->get_results( "SELECT ".implode(",", $sql_columns)." FROM wp_participants_database"); 
    
    
    echo "";
    foreach($column_names as $k => $v) { 
        if($all || (is_array($column_entries) && in_array($k, $column_entries)))
            echo "";
    }
    echo "";
    
    if(count($tmp)>0){
        for($i=0;$i";  
                foreach($tmp[$i] as $key=>$value){
                    echo "";   
                }
                foreach($column_names as $k => $v) { 
                    if($all || (is_array($column_entries) && in_array($k, $column_entries))) {
                        echo "";
                    }
                }
            echo "";
        }
    }
    
    echo '
    authorss research_source research_title$v
    " . $value . "".$row[$v]."
    '; ?>

    0 讨论(0)
提交回复
热议问题