Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using wordpress and into the content of pages i use a PHP code to send query to database and print result of that to the page, and now from in database i have many columns that one of them is a URL of a picture. in PHP code how can i show the picture on a page ? this is my following code and it's not clean :D and also i use INSERT PHP plugin for wordpress to insert the PHP code in content of a page:

[insert_php]
global $wpdb;
$wpdb->show_errors();
$pr = get_the_title();
$results = $wpdb->get_results("SELECT * from print_drug where Product_Name = '".$pr."'");
foreach ($results as $obj) {
  echo "<em><strong>Product Name:</strong></em> "; print_r($obj->Product_Name); echo "<br>";
  echo "<em><strong>Active Substance:</strong></em> "; print_r($obj->Active_Substance); echo "<br>";
  echo "<em><strong>Substance Sorting Identification Number:</strong></em> "; print_r($obj->Substance_Sorting_Identification_Number); echo "<br>";
  echo "<em><strong>Production Form of Use:</strong></em> "; print_r($obj->Production_Form_of_Use); echo "<br>";
  echo "<em><strong>Chemical Formation:</strong></em> "; print_r($obj->Chemical_Formation); echo "<br>";
  echo "<em><strong>Dosages of Use:</strong></em> "; print_r($obj->Dosages_of_Use); echo "<br>";
  echo "<em><strong>Approval Agency:</strong></em> "; print_r($obj->Approval_Agency); echo "<br>";
  echo "<em><strong>Year of Approval:</strong></em> "; print_r($obj->Year_of_Approval); echo "<br>";
  echo "<em><strong>Approval Assigned Company:</strong></em> "; print_r($obj->Approval_Assigned_Company); echo "<br>";
  echo "<em><strong>Approval Country:</strong></em> "; print_r($obj->Approval_Country); echo "<br>";
  echo "<em><strong>Prohibited Cases:</strong></em> "; print_r($obj->Prohibited_Cases); echo "<br>";
}
[/insert_php]

and then i there any way to clean this code ? i'm new in PHP :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
213 views
Welcome To Ask or Share your Answers For Others

1 Answer

I really prefer the following structure of code. By the way, in your code you didn't indicate what is the column for the image's URL, so, I'll assume in my following piece of code that it is: Image_URL

Please, pay attention to the use of the method $wpdb->prepare for sanitizing the query parameters. And pay attention to the method esc_attr in the image tag.

[insert_php]
global $wpdb;
$pr = get_the_title();
$results = $wpdb->get_results($wpdb->prepare("SELECT * from print_drug where Product_Name = %s", $pr));
foreach ($results as $obj) {
    echo "<em><strong>Product Name:</strong></em> {$obj->Product_Name}<br>
        <em><strong>Active Substance:</strong></em> {$obj->Active_Substance}<br>
        <em><strong>Substance Sorting Identification Number:</strong></em> {$obj->Substance_Sorting_Identification_Number}<br>
        <em><strong>Production Form of Use:</strong></em> {$obj->Production_Form_of_Use}<br>
        <em><strong>Chemical Formation:</strong></em> {$obj->Chemical_Formation}<br>
        <em><strong>Dosages of Use:</strong></em> {$obj->Dosages_of_Use}<br>
        <em><strong>Approval Agency:</strong></em> {$obj->Approval_Agency}<br>
        <em><strong>Year of Approval:</strong></em> {$obj->Year_of_Approval}<br>
        <em><strong>Approval Assigned Company:</strong></em> {$obj->Approval_Assigned_Company}<br>
        <em><strong>Approval Country:</strong></em> {$obj->Approval_Country}<br>
        <em><strong>Prohibited Cases:</strong></em> {$obj->Prohibited_Cases}<br>
        <image src='".esc_attr($obj->Image_URL)."'>";
}
[/insert_php]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...