Support » Plugins » Get data from database

  • Hi there,

    I’m new to writing plugins for wordpress. I’ve used the get_row() and get_results() functions. Now I want to access the results and I feel I have to rewrite (using foreach()) all the data again.

    The table used to show the results:

    <table class='widefat'>
        <thead>
            <tr>
                <th>Formname</th>
                <th>#Fields</th>
                <th></td>
            </tr>
        </thead>
    
        <tbody>
        <?php
        foreach(AllForms() as $key => $value)
        {
            echo "<tr>";
                echo "<td>".$value['name']."</td>";
                echo "<td>".CountFields($key)."</td>";
                echo "<td><a href=\"options-general.php?page=FormToPDF&action=editform\">Edit</a></td>";
            echo "</tr>";
        }
        ?>
        </tbody>
    </table>

    The functions used in the table:

    function AllForms(){
    GLOBAL $wpdb;
    
    $result = $wpdb->get_results('SELECT * FROM '.$wpdb->prefix.'FormToPDF_forms',ARRAY_A);
    
    foreach ($result as $data)
    {
        foreach ($data as $key => $value)
        {
            if ($key != 'id')
            {
                $forms[$data['id']][$key] = $value;
            }
        }
    }
    
    function CountFields($formid){
    GlOBAL $wpdb;
    
    $field_count = $wpdb->get_var('SELECT COUNT(*) FROM '.$wpdb->prefix.'FormToPDF_fields WHERE formID = '.$formid);
    return $field_count;
    }
    return $forms;
    }

    Am I doing this correct or is there an easier way? Any suggestions on how to work more efficient?

    Kind regards,

    Niels

  • The topic ‘Get data from database’ is closed to new replies.