duankeng2026 2014-12-29 02:26
浏览 36

在PHP变量中获取自定义HTML标记

Here is what I want to accomplish.

I have many pages with products tests, and I need to create a price comparison for the specific product of this page. To do that, I have to identify the product of the page to query my database.

I thought the best way was to create a custom HTML tag with the name of the product and search for it in the page to put it in a PHP variable.

If this is the good way to do it, how can I achieve this? I'm not a PHP developer and I saw something about DOMelement and some code which looks like exactly what I need to do.

<?php
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<books>
 <book>Patterns of Enterprise Application Architecture</book>
 <book>Design Patterns: Elements of Reusable Software Design</book>
 <book>Clean Code</book>
</books>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
    echo $book->nodeValue, PHP_EOL;
}
?>

This is for XML but it should work for HTML as well. The only thing is, how can I parse the current HTML page with this DOMelement?

Is this the way to do it? If so, how would you do it?

EDIT :

Sorry if it was not clear, but I understand what ArtisiticPhoenix is saying and this is why my approach will probably not work.

What I have is an HTML page with a review of a product, I want to add a price comparison for this product at the bottom of the article. My database is already setup with prices from different shops. Here is the code I wrote to get the values from my database:

<?php

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$result = mysqli_query($conn,"select   p.Nom_produit, 
                                       b.Nom_boutique,
                                       b.logo_boutique,
                                       pp.Prix_produit, 
                                       pp.Prix_livraion,
                                       pp.Prix_produit + pp.Prix_livraion as Prix_total,
                                       pp.url_produit
                                from   Boutiques b
                                       Prix_produit pp
                                       Produits p
                               where   P.id_produit = pp.id_produit
                                 and   b.id_boutique = pp.id_boutique
                                 and   p.Nom_Produit = '.$NOM_PROD'
                              HAVING   pp.date_maj = max(date_maj)
                              order by Prix_produit+Prix_livraison asc LIMIT 5");

if ($result->num_rows > 0) {
    // output data of each row
    $table_resultats .= "
    $table_resultats .= "<table>"
    $table_resultats .= "<tr>"
    $table_resultats .= "<th>Logo</th>"
    $table_resultats .= "<th>Nom boutique</th>"
    $table_resultats .= "<th>Nom Produit</th>"
    $table_resultats .= "<th>Prix Produit</th>"
    $table_resultats .= "<th>Prix Livraison</th>"
    $table_resultats .= "<th>Prix Total</th>"
    $table_resultats .= "<th>Acheter</th>"
    $table_resultats .= "</tr>"

    while($row = $result->fetch_assoc()) {

        $table_resultats .= "<tr>"
        $table_resultats .= "<td>" . $row["logo_boutique"]. "</td>"
        $table_resultats .= "<td>" . $row["Nom_boutique"]. "</td>"
        $table_resultats .= "<td>" . $row["Nom_produit"]. "</td>"
        $table_resultats .= "<td>" . $row["Prix_Produit"]. "</td>"
        $table_resultats .= "<td>" . $row["Prix_livraison"]. "</td>"
        $table_resultats .= "<td>" . $row["Prix_total"]. "</td>"
        $table_resultats .= '<td><form action="'. $row["url_produit"]. '"><input type="button" value="Acheter"></form></td>'
        $table_resultats .= "</tr>"
    }
    $table_resultats .= "</table>"
} else {
    echo "Pas de résultats";
}
echo $table_resultats
$conn->close();
?>

My main problem is how to set the variable $NOM_PROD in my query. I have to make a link between the product and my article, and I'm looking for a way to create this link.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 三种调度算法报错 有实例
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥20 java在应用程序里获取不到扬声器设备