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

495
Views
How to retrieve a PHP variable in HTML code?

A web development dummy here :) How do I put a php variable inside an html tag? for example, here I want to print each product's name, price, and image (also could you please suggest whether the way I retrieve the image is correct?)

 <?php
$doc = new DOMDocument();
$doc->load('database/products.xml');

$products = $doc->getElementsByTagName("fruit");
foreach ($products as $fruit) {
    $names = $fruit->getElementsByTagName("name");
    $name = $names->item(0)->nodeValue;

    $prices = $fruit->getElementsByTagName("price");
    $price = $prices->item(0)->nodeValue;

    $images = $fruit->getElementsByTagName("image");
    $image = $images->item(0)->nodeValue;

    echo "<b>$name - $price - $image\n</b><br>";


echo'

 <div class="container">
    <a href="p3Apples.html">
       
            <img src="img/'.$image.'" class="item-image">
            <div class=‘iamge-title’>$name</div>
            <div class=‘item-price’> $.$price </div>
            <a href=‘shoppingcart.html’ class=‘b-menu’>
                <img id=‘test’ src=‘img/addToCart.png’> </a>
        </form>
    </a>
    </div>

    ';


};

    ?>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

When using ' variables aren't processed, use " in this case

$doc = new DOMDocument();
$doc->load('database/products.xml');
$products = $doc->getElementsByTagName("fruit");

foreach ($products as $fruit) {
    $names = $fruit->getElementsByTagName("name");
    $name = $names->item(0)->nodeValue;
    $prices = $fruit->getElementsByTagName("price");
    $price = $prices->item(0)->nodeValue;
    $images = $fruit->getElementsByTagName("image");
    $image = $images->item(0)->nodeValue;

    echo "<b>$name - $price - $image\n</b><br>";

    echo "
        <div class='container'>
            <a href='p3Apples.html'>
                <img src='img/".$image."' class='item-image'>
                <div class='iamge-title'>$name</div>
                <div class='item-price'> $.".$price."</div>
                <a href='shoppingcart.html' class='b-menu'>
                    <img id='test' src='img/addToCart.png'>
                </a>
            </form>
            </a>
        </div>
    ";
}
over 4 years ago · Santiago Trujillo 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!