• Hi,
    I have created a Custom Post Type by name “Project” and a Custom Database table named wp_project_db for it.

    The wp_project_db consist of 4 Columns – order_id, user_id, project_id and reward_amount.

    My question is that I want to fetch & display the data from the table mentioned above on the project page.

    I used following code to display:

    <?php
    global $wpdb;
    $customers = $wpdb->get_results("SELECT * FROM wp_project_db");
    ?>
    
    <table class="table table-hover">
    
    <?php foreach($customers as $customer){ ?>
    
    <tr>
     <td><center><?php echo $customer->order_id; ?></center></td>
     <td><center><?php echo $customer->user_id; ?></center></td>
     <td><center><?php echo $customer->project_id; ?></center></td>
     <td><center><?php echo $customer->reward_amount; ?></center></td>
    </tr>
    
    <?php } ?>
    
    </table>

    The problem is that the above code displays whole data from the database table. I want to display specific data relating to a project on that particular project page.

    Plz help, Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You need a WHERE clause in your SQL that limits what’s returned by some criteria. To get data relating to a particular project id. For example, if the desired project ID is 123, you resulting SQL might be something like
    SELECT * FROM wp_project_db WHERE project_id = 123;
    Because you probably would be using a variable to specify the ID to get, your SQL needs to be run through $wpdb->prepare(), which utilizes a sprintf() type of format. If your ID field is not a numeric type, the argument should be within single quotes and the sprintf placeholder would be %s instead of %d.

    Thread Starter Mineshrai

    (@mineshrai)

    Thanks @bcworkz,
    I have tested this code previously but it will display data of only specific project ID. I want to display data related to every project on that particular project page dynamically.

    Plz help.

    Moderator bcworkz

    (@bcworkz)

    I’m sorry but I don’t understand what you are after. To me, “data related to every project on that particular project” either does not make sense or is the same as “data of only specific project”.

    Or slightly more concisely, you want every project on that project. Isn’t that all data with the same project ID? You say no. I must have distilled what you said down to the wrong essence. So then what does the data you want have in common? Sorry, I’m not following. Please explain further. Perhaps with an example. Thanks!

    Thread Starter Mineshrai

    (@mineshrai)

    @bcworkz,

    Actually I want to display data dynamically on every project page just like we display title, content, metas etc.

    In short Data of each project should be displayed on that respective project only.

    I m sry as I m not able to explain u, what I mean.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to display specific data from custom database table in WordPress’ is closed to new replies.