WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] cformsII php+mysql help! (6 posts)

  1. nihil.core
    Member
    Posted 3 years ago #

    Hello everyone,
    i'm not a database hard coder, but i love wordpress so i'd like to know more.

    i'm using wordpress + cformsII for a school website.
    i have a form for each lesson, so students can subscribe to single lessons, and i have to manage a maximum number of students registrations, different for each lesson.
    so in the plug-in backend i've set a maximum number of submission different for each form resulting in a "Registrations are closed" sentence after the number of submission is reached.

    but now i'd love to have a counter for the number of seats left, such as "There are 2 more seats left, hurry up!"
    i didn't find an automatic way to do this so i'm trying to do a query directly on the database and having a numerical result to show on the page.

    i've found that the number of submissions is stored in the new table wp_cformssubmission under form_id
    and the maximum number of submission set is stored in the table wp_cformsdata under field_val where field_name is "Set the maximum number of students" and sub_id is the number of the lesson.

    so now, math should be simple: (maximum number students) - (registrations) = (number of seats left)
    but my poor knowledge of MySQL won't help me in getting the query works...
    i tried different ways starting from here http://codex.wordpress.org/Function_Reference/wpdb_Class but i'm stuck.

    any help?
    thank you in advance.

    alessandro

  2. nihil.core
    Member
    Posted 3 years ago #

    in the end i did it myself!
    i'll post the code just in case it can be helpful for someone else:

    <?php
    if ( is_single('240')) {
    
    $max = $wpdb->get_var( "SELECT field_val FROM $wpdb->cformsdata WHERE field_name = 'Indica un numero massimo di studenti' AND sub_id = '12' " );
    echo '<span style="float:left;">Maximum number of submissions:<b> ' . $max . '</b></span>';
    
    $registrations = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->cformssubmissions WHERE form_id = '3' " );
    
    $seats = $max - $registrations;
    echo '<br><span style="float:left;">Seats left:<b> ' . $seats . '</b></span>';
    } ?>

    where:
    is_single('240') is the ID of the post
    'Indica un numero massimo di studenti' (in english = Set the maximum number of students) is the field name of the field in the form that the teachers use to propose lessons to select how many students can register to the lesson
    sub_id = '12' is the ID where the data of the "teacher's form" has been stored in the DB (to find this number you have to enter your phpmyadmin, go to the 'wp_cformsdata' table and then print or export your 'sub_id' - DO THIS ONLY IF YOU KNOW WHAT YOU'RE DOING!)
    form_id = '3' is the ID of the form that the students are using to register to the lesson (you can find this one by entering your phpmyadmin, going to the 'wp_cformssubmissions' table and then to form_id - DO THIS ONLY IF YOU KNOW WHAT YOU'RE DOING!)
    › in the end there is the easy math and the printing of the value of the variable!

    i've put everything into an if construct because i have a different form for each post, so in my case i've had to duplicate the code for each post ID with different values of tables' ID.
    this code is in the single.php file.

    i know it's not very automatic but i need it to work in a very short time and i will try to improve it later. For the moment it works!
    You can watch it here http://milano.tradeschool.it

  3. Mediatory
    Member
    Posted 3 years ago #

    Hi nihil.core,

    I need to do the exact same thing now on one of my sites. I'm stuck at the point where I need to export/print the sub_id.
    Could you explain how that actually works. I think I can manage the rest alone.

    Thank you for finding a solution to this an sharing it.

    Cheers,
    mediatory

  4. Mediatory
    Member
    Posted 3 years ago #

    Nobody her anymore!? :-(

  5. nihil.core
    Member
    Posted 2 years ago #

    hi mediatory,

    sorry my late reply...
    i'm confident you solved the problem on your own, anyway to answer your question you just have to dump the 'wp_cformsdata' table and then look at the sub_id value of the form you are interested to get the value from.

    if your DB runs on phpmyadmin you can find an "Export" icon in the menu on top!
    i hope this could help :)

    bye

  6. Mediatory
    Member
    Posted 2 years ago #

    Hi nihil.core,

    thanks for the reply. I'm closer now but it still doesn't work. But I have someone that is helping me with this so I don't need to bother you anymore :-).

    Thanks,
    Mediatory

Topic Closed

This topic has been closed to new replies.

About this Topic