WordPress.org

Ready to get started?Download WordPress

Forums

Making a two-sided database (4 posts)

  1. Boris K
    Member
    Posted 10 months ago #

    Hi, guys. I'm trying to make a site for employers and workers to register and be able to find each other. I'm running the site on my local server in WAMP right now.

    I have built the registration forms using the Ninja Forms plugin, and am now trying to set up a search function loop, where a MySQL query is formulated based on whether the user is looking for workers or employers, and is logged on or not, then the output is displayed in a table.

    I know how to do all this in vanilla procedural PHP/MySQL, but am new to WordPress and don't know where to begin here.

    I've found the table that the forms save data to in PHP MyAdmin, and all the user data from each registration is dumped into one field of a new row as an array. For instance, here are entries for an employer and employee.

    Employer:

    'a:13:{i:0;a:2:{s:8:"field_id";i:1;s:10:"user_value";s:3:"Bob";}i:1;a:2:{s:8:"field_id";i:2;s:10:"user_value";s:5:"Jones";}i:2;a:2:{s:8:"field_id";i:3;s:10:"user_value";s:10:"1 Jones St";}i:3;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:10:"Jonesville";}i:4;a:2:{s:8:"field_id";i:5;s:10:"user_value";s:5:"99191";}i:5;a:2:{s:8:"field_id";i:6;s:10:"user_value";s:2:"US";}i:6;a:2:{s:8:"field_id";i:7;s:10:"user_value";s:21:"bobjones@fakemail.com";}i:7;a:2:{s:8:"field_id";i:9;s:10:"user_value";s:14:"(555) 555-5555";}i:8;a:2:{s:8:"field_id";i:11;s:10:"user_value";s:10:"11/11/1911";}i:9;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:2:"AF";}i:10;a:2:{s:8:"field_id";i:13;s:10:"user_value";s:2:"na";}i:11;a:2:{s:8:"field_id";i:28;s:10:"user_value";s:6:"Center";}i:12;a:2:{s:8:"field_id";i:29;s:10:"user_value";s:1:"5";}}

    Employee:

    a:13:{i:0;a:2:{s:8:"field_id";i:1;s:10:"user_value";s:3:"Bob";}i:1;a:2:{s:8:"field_id";i:2;s:10:"user_value";s:5:"Jones";}i:2;a:2:{s:8:"field_id";i:3;s:10:"user_value";s:10:"1 Jones St";}i:3;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:10:"Jonesville";}i:4;a:2:{s:8:"field_id";i:5;s:10:"user_value";s:5:"99191";}i:5;a:2:{s:8:"field_id";i:6;s:10:"user_value";s:2:"US";}i:6;a:2:{s:8:"field_id";i:7;s:10:"user_value";s:21:"bobjones@fakemail.com";}i:7;a:2:{s:8:"field_id";i:9;s:10:"user_value";s:14:"(555) 555-5555";}i:8;a:2:{s:8:"field_id";i:11;s:10:"user_value";s:10:"11/11/1911";}i:9;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:2:"AF";}i:10;a:2:{s:8:"field_id";i:13;s:10:"user_value";s:2:"na";}i:11;a:2:{s:8:"field_id";i:28;s:10:"user_value";s:6:"Center";}i:12;a:2:{s:8:"field_id";i:29;s:10:"user_value";s:1:"5";}}

    I guess I could make the registration all on one page, and just let the user pick whether they are an employee or employer in the first form, then set up a PHP function to query the DB, loop through the output, and for each array that is returned where the first element is of the proper type, loop through the rest of it, outputting it as a table. This seems a bit...loopy, though. Perhaps there is a better free plugin that lets me make forms and directly determine which table and field they post to?

    Also, how do I even access the source code for something like this? I tried to do so in the Appearance-Editor on cPanel, but no joy.

    Thanks for your help,

    Boris

  2. vtxyzzy
    Member
    Posted 10 months ago #

    Is the problem that you don't know how to split the data out from the single field, or that there is no way to distinguish employee from employer?

    The data is 'serialized'. You use the unserialize() function to extract it back into an array, like this:

    $data = unserialize($your_field_name);
  3. Boris K
    Member
    Posted 10 months ago #

    Got it, thanks.

    There is also no real way to distinguish employee from employer, since NinjaForms sticks ALL form data into the same table in the same format. Of course, I could have the user choose whether they are an employee or employer in a pull-down menu up top, then write a function to query the database and spit out employee or employer data based on the searching user's preference, but it seems like an ugly way of doing things. I should be able to go into the NinjaForms plugin and edit it to save user input into different tables based on the page it's coming from, right?

  4. vtxyzzy
    Member
    Posted 10 months ago #

    If you have two separate forms, you could add a hidden field to each with the value of 'Employer' or 'Employee' as appropriate.

    Then set your query with a 'where' clause:

    $sql = "SELECT * FROM {$wpdb->prefix}ninja_forms_subs WHERE data LIKE '%Employer%'";
    $results = $wpdb->get_results($sql);;

Reply

You must log in to post.

About this Topic