admin管理员组

文章数量:1277910

Hello everyone I need to fetch rows and display them. This is not a aproblem, but! I have column icon, which contains the url of the icon, so I should display the image from the url. Could you tell me how to display that image?

  <?php

    global $wpdb;
    // this adds the prefix which is set by the user upon instillation of 
    wordpress
    $table_name = $wpdb->prefix . "tablename";
    // this will get the data from your table
    $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
    ?>
    <ul>
        <?php
        foreach ($retrieve_data as $retrieved_data){ ?>


      <div class="innerApp">
        //THIS IS THE PROBLEMATIC LINE
        <img  class="appLogo" src="<?php bloginfo('template_url') ?><?php 
       echo $retrieved_data->icon?>">   
        <p class="justTextTextApp"><?php echo $retrieved_data->NAME?></p>
        <p class="justDescription"><?php echo $retrieved_data->description?>
        </p>
      </div>

        <?php 
                                                   }
        ?>
       </ul>

Hello everyone I need to fetch rows and display them. This is not a aproblem, but! I have column icon, which contains the url of the icon, so I should display the image from the url. Could you tell me how to display that image?

  <?php

    global $wpdb;
    // this adds the prefix which is set by the user upon instillation of 
    wordpress
    $table_name = $wpdb->prefix . "tablename";
    // this will get the data from your table
    $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
    ?>
    <ul>
        <?php
        foreach ($retrieve_data as $retrieved_data){ ?>


      <div class="innerApp">
        //THIS IS THE PROBLEMATIC LINE
        <img  class="appLogo" src="<?php bloginfo('template_url') ?><?php 
       echo $retrieved_data->icon?>">   
        <p class="justTextTextApp"><?php echo $retrieved_data->NAME?></p>
        <p class="justDescription"><?php echo $retrieved_data->description?>
        </p>
      </div>

        <?php 
                                                   }
        ?>
       </ul>
Share Improve this question asked Jan 31, 2018 at 9:10 AlisonVilmoreVAlisonVilmoreV 131 silver badge4 bronze badges 6
  • what is url of your image? how does it related to page? does it static or dinamic? – Samvel Aleqsanyan Commented Jan 31, 2018 at 9:17
  • The url is just the place from where the image is derieved. It has no relation with the page. So basically, we have url(the icon url) and I assume I should use it as src"" in my <img>. But the table is custom, so I am not sure which function I can use. – AlisonVilmoreV Commented Jan 31, 2018 at 9:23
  • does $retrieve_data contain your image url? – Samvel Aleqsanyan Commented Jan 31, 2018 at 9:25
  • my url is in $retrieved_data->icon – AlisonVilmoreV Commented Jan 31, 2018 at 9:29
  • 1 then why do you use bloginfo('template_url')? – Samvel Aleqsanyan Commented Jan 31, 2018 at 9:32
 |  Show 1 more comment

1 Answer 1

Reset to default 1

I'd highly recommend to escape all values before output, e.g. echo esc_html( $retrieved_data->NAME ).

The same for the image:

<img src="<?php echo esc_url( $retrieved_data->icon ); ?>" />

You can read more about Securing Output in the developer handbook.

本文标签: phpHow to get image from url from the database