WordPress.org

Ready to get started?Download WordPress

Forums

How to make page that updates with user query (4 posts)

  1. Holoman
    Member
    Posted 4 months ago #

    Hi

    I am a bit of a novice when it comes to making websites. I want to be able to display a large set of information on my site. This is in the form of thousands of tables all of the same format and referred to by codes, say for example X and Y, and these are all saved as either html files or images in a folder on my site.

    I want to make a webpage where when a user wants to find the tables for code X, they either click a button or type it in a box then press enter. Then they are taken to a page which brings through the tables for X. So it takes some general filepath and adds X to the end to bring through the correct table, and its embedded on a generic page which will be the same for all codes.

    I don't want to have to make a separate page for each code, as there will be thousands and I don't know a quick way to do it, so is this idea possible?

  2. bcworkz
    Member
    Posted 4 months ago #

    Sorry for the late reply, I only just now saw your topic.

    Yes, this is possible! It can all be done from a single custom page template. The data can be passed as an URL parameter or a permalink endpoint, or as a POST array. If no data is passed, the user selection dialog is presented. If data is passed, PHP determines the data passed and creates a query or includes the proper image file based on this information and presents the resulting data to the user.

    In a sense, this is what WP does to present blog information. An URL parameter or permalink meaning is determined, and corresponding data is queried and the results displayed in accordance with a particular template from your theme. Thousands of different posts can all utilize this same template.

  3. Holoman
    Member
    Posted 4 months ago #

    Thanks. I have been playing about with php and using it to change the html code of the page depending on what the user inputs, linking to images etc via urls which use the same codes as the inputs, and with some redirects if there are errors or they try to access the page directly.

    At the moment it means I have to save a manual page on my server that wont update when I change widgets and things so is a bit of a pain but it works and is about my limit in terms of technical know-how. Not sure if there is a way to incorporate this into wordpress. When I tried putting the php code into a WordPress page it didn't work because that page isn't actually saved as a file on my server so I didn't know how to get the input form to refer to it.

  4. bcworkz
    Member
    Posted 4 months ago #

    Yes, the options for referring to custom code pages that access WP resources is very limited because of the many ways WP folders can be configured. A directly referenced code page needs to include wp-load.php in order to access WP resources, but the code page cannot know where that file resides in relative terms for any given installation.

    There are actually only 3 ways to load the WP environment for custom code, 2 involve going through either admin-ajax.php or admin-post.php to access a custom coded action callback that does the heavy lifting. The concepts involved are a little hard to grasp for novice WP coders.

    Easier to understand is creating custom page templates. By virtue of being a page, the WP environment is automatically available. The quirky thing is you cannot directly reference the template on a form by filename. You must first create a page post type based on the template in order to establish a page title slug which is used to reference the page and it's underlying template. There is typically no need to provide any content unless the template is expecting it. Just provide a title and publish. Then use the permalink as the form's action attribute.

    If setup correctly, there should be no need to ever alter the template or create derivative versions, nor need to save various page post types based on that template, though certain data storage schemes may make use of saved page post types to store data.

    The template can also contain it's related form, so everything is contained in one file. If the request is GET, the form is displayed. It submits by POST to itself. If the request is POST, the form is not displayed, rather the form values are processed, data is queried, and the results output, all by the one template.

    Here is a simplistic example of a page template (in "pseudocode" so I don't have to worry about syntax ;) )

    <?php
    /* Template Name: Simple Example */
    get_header();
    if ( 'GET' == request_method()) {
      echo '<form action="" method="POST">'.show_dropdown-submit().'</form>';
    }else{ // POST request
      switch $_POST['dropdown_value'] {
        case 1:
          echo '<img src="/wp-content/uploads/selection-1.jpg" />';
          break;
        case 2:
          echo '<img src="/wp-content/uploads/selection-2.jpg" />';
          break;
        else:
          echo 'Invalid Selection';
      }
    }
    get_footer()

    Save this page in your child theme folder. Create a page based on this template, providing just the title: My Example, which results in a permalink of example.com/my-example/. This link can be included in the nav menu or as a sidebar link or whatever.

    When one clicks on the link, they are presented with a drop down form and submit button, all styled by the theme including header and footer. The user picks from the dropdown and clicks the submit button. An image corresponding to the selection is displayed on the next page, also styled by the theme. Of course additional HTML containers are required to effectively style the page according to the theme. These can be easily copied from related theme templates.

Reply

You must log in to post.

About this Topic

Tags