• Hey all!

    I’m a new WordPress user, and I have a couple of questions regarding tables, sessions and SQL.

    I have created some additional tables of info in the WP database, which are client projects.

    1. I’m presuming I can create a 1toMany relationship between my tables and the wp_user table?
    2. If yes on the above, can I then query information from ‘projects’ table based on the user login (session based)?
    3. Finally, how can I query single cells of information and place them into a page? For example a page might look like this.

    Welcome, ($$user_logged_in$$)
    Project: ($$project_name$$)
    Status: ($$project_status$$)
    Project address: ($$client_address$$).

    Is that something I can do in WP?

    Thanks all! πŸ™‚

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Yes. If the tables are in the same database, you can access them with the wordpress database methods. See https://developer.wordpress.org/reference/classes/wpdb/

    Thread Starter jonuk80

    (@jonuk80)

    Thanks Steve,

    Can anyone shed any light on how I might achieve the above? Specifically question 3?

    Cheers

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The easiest thing to do might be to create a shortcode, so you’d have something like

    Welcome, [user]
    Project: [project output="name"]
    Status: [project output="status"]
    Project address: [project output="client_address'].
    Thread Starter jonuk80

    (@jonuk80)

    Thanks, I’ll look into shortcode.

    With my tables then, I’m guessing I will have to create a FK in projects, which links to the UserID (the default PK in WP)? I’m a bit stuck on how to do this. I know how to link the two in SQL, but I’m not sure how I link a query to the shortcode above!

    Thanks again!

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The shortcodes are just “links” to PHP code, so you’d use the WPDB methods to create your SQL statements and process the results.

    Just curious: Why did you choose to use custom tables rather than custom post types?

    Thread Starter jonuk80

    (@jonuk80)

    Okay looks like I need to do some WPDB learning :/ I wonder if there are any plugins that can help.

    Naivety? I don’t really understand what I’m doing with WP and I’m trying to make a custom client portal. I’ve looked into plugins but none of them seem to do what I want. What are custom post types?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Custom Post Types are like a post or page, but of a type you define and to which you can add custom fields, either via hand-coded PHP or plugins like Advanced Custom Fields.

    Thread Starter jonuk80

    (@jonuk80)

    Thanks Steve. That looks like quite a bit of coding know how.

    I can’t believe it’s so hard to query single fields at a time and stick them on a page. Maybe I’m just being naive in thinking I can do it!

    Thread Starter jonuk80

    (@jonuk80)

    I think I’m getting closer to what I want. Using php snippets, I am able to insert the $wpdb->get_var command and pull single instances from the WP database, which works.

    Maybe someone can help with the following questions.

    1. On a page I have the following snippet

    <?php
    $username = $wpdb->get_var( "SELECT display_name FROM $wpdb->users WHERE ID=2" );
    echo "{$username}";

    This currently echos the display name from whoever user ID 2 is. How do I amend this statement so it shows the name of whoever’s logged in?

    2. Going one step futher, I have created some custom tables i.e. wp_projects. For this one, I made this query.

    <?php
    $projectref = $wpdb->get_var( "SELECT pr_no FROM $wpdb->projects WHERE user_id=1" );
    echo "{$projectref}";

    However, it echos nothing, and comes up blank. Does the wpdb shortcode not work with custom tables?

    3. If you can solve the above, can I go one step further with that code. You can see it’s just WHERE user_id=1, but I’d like it to display only the project number(s) relevant to the person logged in.

    There is a FK in ‘projects’ user_id, which links to ‘wp_users’ ID.

    Many thanks!

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    start with get_current_user() to get the current user’s user object.

    Thread Starter jonuk80

    (@jonuk80)

    Thank you, that worked. I changed it to

    <?php
    $current_user = wp_get_current_user();
    $username = $wpdb->get_var( "SELECT display_name FROM $wpdb->users WHERE ID=$current_user->ID" );
    echo "{$username}";

    Please can you shed some light on questions 2 & 3 above?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    use a print_r to see what’s in $projectref and correct the PHP — remove the {} around it.

    I think this has gone as far as I’m willing to take it.

    Thread Starter jonuk80

    (@jonuk80)

    No problem Steve – not trying to offend here, I’m just trying to learn.

    Thread Starter jonuk80

    (@jonuk80)

    For anyone who’s interested, you don’t need the $wdbp prefix on custom tables in a query. Amending the code to below fixes it.

    <?php
    $current_user = wp_get_current_user();
    $projectref = $wpdb->get_var(“SELECT pr_no FROM projects WHERE ID=$current_user->ID”);
    echo “{$projectref}”;

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Tables, Sessions & SQL’ is closed to new replies.