• Xaib Aslam

    (@lahorimela)


    Hello,
    I create a site with WordPress, then I install CPT UI to create custom post types. After that, I create a table in the database with the name (wp_christian_names) and I upload that file to excel and add some data. I have more than 7000+ rows inside, see this https://ibb.co/4Fzw539.

    So I was thinking that how I can make 7000+ URLs for that, so I created a taxonomy-like tag system, and I add all these names only, so I will get the URL for that. now I am confused that how I can show the exact name data for that. I do a search on google and found the answer but can’t figure out how WordPress knows that this name has data in the database.

    <?php
      global $wpdb;
      $table_name = $wpdb->prefix . "wp_christian_names";
      $user = $wpdb->get_results( "SELECT * FROM $table_name" );
    ?>
    
    <table border="1">
        <tr>
         <th>NAME</th>
         <th>GENDER</th>
         <th>ORIGIN</th>
         <th>RELEGION</th>
        </tr>
    
    <?php foreach ($user as $row){ ?>
    <tr>
    
        <td><?php echo $row->name ?></td>
        <td><?php echo $row->gender ?></td>
        <td><?php echo $row->origin ?></td>
        <td><?php echo $row->relegion ?></td>
    </tr>
    <?php } ?>
    
    </table>

    for example, I have a tag with the name Aana and want to fetch data of this name from the database.
    Can anyone help me with this please.

    • This topic was modified 2 years ago by Xaib Aslam.
Viewing 15 replies - 16 through 30 (of 32 total)
  • Moderator bcworkz

    (@bcworkz)

    Have you seen the size of the WP code base? ?? What you could do to shorten that snippet would be minuscule in proportion to the overall code base. Excess memory usage likely has more to do with assigning lots of data to variables and object caches than to machine code compiled for execution.

    Regarding my social media accounts, I have a few, but they are largely inactive. I don’t post anything, the accounts are only used to occasionally access content of others.

    Additionally, we strongly discourage members from initiating off forum contact. Taking topical discussion off of the forum deprives others from benefiting from the ensuing discussion. Also, many members are not very tech savvy and can fall victim to scams and fraud. I am in no way suggesting you have such intent or are vulnerable to such. Because it’s impossible to discern other’s intent in a public forum, it’s necessary to impose a blanket ban that also impacts otherwise completely innocent interactions. Bad people is why we cannot have nice things.

    Thread Starter Xaib Aslam

    (@lahorimela)

    Hi, I am trying to add the get template part but my fields are not working there. just showing the table heading row. I am trying to like this.

    <?php
    global $wpdb;
    $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
    $limit   = 12;
    $offset  = ( $pagenum - 1 ) * $limit;
    $rows = $wpdb->get_results( "SELECT * FROM wp_names_christian WHERE name_gender LIKE '%boy%' LIMIT $offset, $limit" );
    ?>
    
    <?php get_template_part('names/table'); ?>
    
    <?
    $total        = $wpdb->get_var( "SELECT COUNT(<code>name_english</code>) FROM wp_names_christian WHERE name_gender LIKE '%boy%'" );
    $num_of_pages = ceil( $total / $limit );
    $page_links   = paginate_links( array(
        'base'      => add_query_arg( 'pagenum', '%#%' ),
        'format'    => '',
        'mid_size' => 1,
        'end_size' => 0,
        'prev_text' => __( '<i class="fas fa-arrow-left"></i>' ),
        'next_text' => __( '<i class="fas fa-arrow-right"></i>' ),
        'total'     => $num_of_pages,
        'current'   => $pagenum
    ) );
    
    if ( $page_links ) {
        echo '<div class="pagination">' . $page_links . '</div>';
    } ?>

    Here is my table.php file

    <table class="names_table_boy table table-striped table-hover">
      <thead>
        <tr>
          <th>NAME</th>
          <th>GENDER</th>
          <th>SHORT NAME</th>
          <th>NAME LENGTH</th>
        </tr>
      </thead>
      <tbody>
        <?php $count = 1; foreach( $sql as $row ) { ?>
          <tr>
            <td><a target="_blank" href="https://lahorimela.com/name/christian/boy/name-meaning-in-english/<?php echo strtolower ($row->name_english); ?>/"><?php echo $wpdb->$row->name_english ?></a></td>
            <td><?php echo $wpdb->$row->name_gender ?></td>
            <td><?php echo $wpdb->$row->name_short ?></td>
            <td><?php echo strlen("$row->name_english"); ?> Letters and <?php echo str_word_count("$row->name_english"); ?> Word</td>
          </tr>
        <?php $count++; ?>
        <?php } ?>
      </tbody>
    </table>
    Thread Starter Xaib Aslam

    (@lahorimela)

    I apologize for not responding to your last message. There are a lot of bad people out there, and I understand that. The words you said meant a lot to me, and your coding expertise was extremely valuable to me.

    Moderator bcworkz

    (@bcworkz)

    No apologies necessary. Thank you for understanding.

    In the parent file you assign query results to $rows but in the template part you try to loop through the results in $sql instead of $rows.

    Thread Starter Xaib Aslam

    (@lahorimela)

    I try but can’t figure it out. ok, I have one more question.

    That I create custom post type names then I create custom taxonomies

    christian_boy_names
    christian_girl_names
    christian_alphabets

    So I create these templates for that taxonomies

    taxonomy-christian_boy_names.php
    taxonomy-christian_girl_names.php
    taxonomy-christian_alphabets.php

    Now I am going to make more taxonomies under CPT names

    muslim_boy_names
    muslim_girl_names
    turkish_boy_names
    turkish_girl_names
    indian_boy_names
    indian_girl_names

    Is it possible to make 1 template file for all taxonomies only for specific Custom post type names?

    This means there should be a single template taxonomy-names_meaning and put code inside and use IF ELSE conditions.

    And you know it that how I am fetching data from the database.

    If yes can you help me with that?

    Moderator bcworkz

    (@bcworkz)

    By default WP only supports single term templates or all taxonomy templates. There is no built-in mechanism for grouping term templates. However, you can create your own grouping scheme by using the “template_include” filter.

    Your filter callback could check the query vars in the global $wp_query object. If it’s not a request for *_names taxonomy term, return the passed template path unchanged. In other words, use default behavior.

    If it is a *_names request, return the path to the appropriate template file. WP will include whatever path is returned from this filter. There are some example code snippets in the above linked doc page you can use as guidance on what to do.

    Thread Starter Xaib Aslam

    (@lahorimela)

    I got it, but template_include is not suitable for me, for that I can go with multiple templates like this…

    taxonomy-christian_boy_names.php
    taxonomy-christian_girl_names.php
    taxonomy-christian_alphabets.php
    taxonomy-muslim_boy_names.php
    taxonomy-muslim_girl_names.php
    taxonomy-muslim_alphabets.php

    what about if else, can you create an example for me? e.g

    If this custom taxonomy christian_boy_names then shows this div
    If this custom taxonomy christian_girl_names then shows this div
    Else default layout.

    Thread Starter Xaib Aslam

    (@lahorimela)

    I am trying with this but its not working…

    <?php if(taxonomy_exists(‘christian_boy_name’)){ ?>

    <?php
    $name = get_query_var(‘christian_boy_name’);
    global $wpdb;
    $sql = $wpdb->prepare(“SELECT * FROM wp_names_christian WHERE name_english=%s;”, $name );
    $row = $wpdb->get_row( $sql );
    ?>

    <?php } ?>

    Ok i figured out…

    <?php if ( is_tax( 'christian_girl_name' ) ) {?>

    • This reply was modified 1 year, 11 months ago by Xaib Aslam.
    • This reply was modified 1 year, 11 months ago by Xaib Aslam.
    Thread Starter Xaib Aslam

    (@lahorimela)

    ok, I want to know how I can add PHP code inside this code…

    taxonomy' => 'christian_boy_name', 
    'hide_empty' => false, 
    'order' => 'ASC

    Like this…

    taxonomy' => '<?php echo ($row->name_religion); ?>_boy_name', 
    'hide_empty' => false, 
    'order' => 'ASC
    Thread Starter Xaib Aslam

    (@lahorimela)

    is that correct statement

    <?php if ( is_tax('christian_boy_name') || ('christian_girl_name') ) {?>

    Thread Starter Xaib Aslam

    (@lahorimela)

    Ok, I got it… I figured it out…

    <?php if ( is_tax('christian_boy_name') || is_tax('christian_girl_name') ) { ?>

    Thread Starter Xaib Aslam

    (@lahorimela)

    Hello,
    I have a question I created a custom taxonomy and some names. ok now let’s say I have 3 names.

    Alia
    John
    BC Workz

    and their URL is

    https://mydomain.com/name/alia
    https://mydomain.com/name/john
    https://mydomain.com/name/bc-Workz

    Ok 1st two links are working fine but when I open https://mydomain.com/name/bc-Workz there is no data for that because in my database this name is saved with space BC Workz so how I can retrieve data for that too?

    This is the code you create for me.

    <?php
    $name = get_query_var('christian_boy_name');
    global $wpdb;
    $sql = $wpdb->prepare("SELECT * FROM wp_names_christian WHERE name_english=%s;", $name );
    $row = $wpdb->get_row( $sql );
    ?>
    Moderator bcworkz

    (@bcworkz)

    Taxonomy term slugs should not have upper case chars. It’s unrelated to your question, just an observation. Case may not matter on your system, but it does on many. 'w' != 'W'. Also “BC” would be my given name, “Workz” would be my family name, so there is no space involved ?? It’s a made up name anyway, but I understand your question all the same.

    Back on topic, I think it’s a flaw in your DB schema. Notice how default WP tables have separate name and slug fields to deal with this issue. It’s not feasible to get term slugs to properly match the proper spelling of all names. Making a query case insensitive deals with common names, so 'alia' == 'Alia'. But queries are unable to match slugs to names that contain spaces, apostrophe’s, or extended characters like ?, since those get stripped or replaced in term slugs.

    You could replace the hyphen a term slug with SQL wildcard char _, which will match a space or anything in the place of the hyphen. This would not address apostrophes and extended chars in names though. The only solutions I see is either adding another field for the actual term slug, or relating the WP term table to your names table. For example, the term could have a meta value that will correctly match a name in your table. The query gets pretty complicated because you’d be dealing with 3 tables related together, but it’s a feasible solution if it’s not possible to add a column to your names table.

    One way or another, there should to be a way to relate disparate term slugs to actual names in your table. Ignoring case or wildcards will not fully address the issue.

    Moderator bcworkz

    (@bcworkz)

    Oops, ignore the term meta aspect of my last reply. I was overthinking the problem. The name with a space which you need to match in your table is presumably the same as the term name field (not slug), correct? Then only two tables are involved, join those in a query, relating each table’s name field together, matching the requested name to the term’s slug field. By the joined relation, you’ll get the desired name data from your table.

    TBH, I’m not that good with SQL. You should be able to work out a proper query by searching the internet on how to do mySQL table joins. It’s what I’d need to do to help you further since I couldn’t suggest the right query just from what I presently know about the topic. If you still encounter difficulty, let me know. Maybe we can figure something out together.

    Thread Starter Xaib Aslam

    (@lahorimela)

    ok got it. I do lots of searches but nothing was found. As you created the SQL query and it’s working awesome but got a problem when got space in the name. This is my DB https://ibb.co/tQTw2YB

    So I think there is a solution as you said that I have to create a slug column for that.

Viewing 15 replies - 16 through 30 (of 32 total)
  • The topic ‘Display data from database’ is closed to new replies.