Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
How do I display an image after you've made your selection with a php Array?

My question is, what would be the best way to go about this?

<?
if (in_array("1434", $category)) {
  if($item > 0) {
    $wc_product = wc_get_product($item);
    echo '<img src="' . get_the_post_thumbnail_url($item, 'medium') . '">'; 
  }
}
?>

[1]:

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The general method that you would use is

  • someone clicks on the product which calls a javascript function
  • your javascript makes an ajax call to your PHP script on the back end
  • your php script gets the data and echoes it back as a JSON encoded string
  • your javascript gets this new data, parses into JSON and makes the image appear

Using php, you build your initial html page. Lets say you have an image element to show the selected image, like:

<img id='mainImg' src='/placeholder-image.jpg' />

Use something like the product id as the argument for the function that is called from the button

<button onclick='getProductData(1234)'>View Product</button>

Your JS function would look something like this

function getProductData(id) {
   let url = '/myBackendPHPendpoint.php?pid=' + id
   $.ajax({url: url,
        success: function(json){
          json = JSON.decode(json)
          $('#mainImg').attr('src', json.image)
        }
    })
}

Your PHP page myBackendPHPendpoint.php would look something like

<?php
  $id = $_GET['pid'];
  // database query to get the product data
  // get the result as an object or array in php  
  echo json_encode($result);
?>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!