0

I want to capture the data with the title "Groom Name" in Label, that is, I want to print the "printyazdırasd" output on the screen. How can I get the value inside the label according to the title? With the code below, I print the title of the label on the screen. In other words, I have printed "Damat Adi" on the screen. I want to print the text. I want to print the text according to the title.

HTML TAG

<label .... title="Damat Adi">yazdıryazdırasd</label>

PHP

 $sql = "SELECT html_kod from  ...";
        $stmt = sqlsrv_query( $conn, $sql );

    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
        $paketKFIYAT = $row['html_kod'];
        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($paketKFIYAT);
        $iFrame = $dom->getElementsByTagName('label')->item(0);
        $src = $iFrame->getAttribute('title');
        $results = Array("tag" => $src);
        echo json_encode($results);
        }
hellobeko
  • 31
  • 6
  • Why not use PDO instead of the low-level SQL Server driver? – tadman Apr 03 '20 at 20:05
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Apr 03 '20 at 20:05
  • How do I do this when I use PDO? I want to print the title, the name of the groom, on the screen. @tadman – hellobeko Apr 03 '20 at 20:08
  • PDO is just a lot friendlier to use, but it also works with other SQL databases so your learning has broader applicability. I mention a framework because this is a solved problem, you don't need to invent your own methods here. – tadman Apr 03 '20 at 20:15
  • Can you construct a CSS selector for the element you want? If so you can fetch it by a selector. – tadman Apr 03 '20 at 20:15
  • Can you share working code that triggers your problem, along with your debugging attempts? There is nothing like "Groom name" in the given code, neither is there anything like "printyazdırasd" – Nico Haase Apr 03 '20 at 20:20
  • I want to give the text according to the title among the html tags. So I want to print the text with the label title "Damat Adı" on the screen @NicoHaase – hellobeko Apr 03 '20 at 20:35
  • I want to shoot the text in this tag in php. @tadman – hellobeko Apr 03 '20 at 20:37
  • You can [select it by xpath](https://stackoverflow.com/questions/18182857/using-php-dom-document-to-select-html-element-by-its-class-and-get-its-text). – tadman Apr 03 '20 at 20:39
  • thank you, it's work @tadman – hellobeko Apr 03 '20 at 20:51
  • You'll want to post a self-answer with your working code. That will not only help others, but perhaps solidify what you've learned. – tadman Apr 03 '20 at 20:53
  • Thank you, much much @tadman – hellobeko Apr 03 '20 at 20:55

1 Answers1

1

I have captured the data with the title "Groom Name", I get the output of such text to design.

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

    $paketKFIYAT = $row['html_kod'];


    $classname = 'Damat Adi';
    $dom = new DOMDocument;
    $dom->loadHTML($paketKFIYAT);
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@title='" . $classname . "']");

    if ($results->length > 0) {
        echo $review = $results->item(0)->nodeValue;
    }

}
hellobeko
  • 31
  • 6
  • 2
    Don't forget you can interpolate in PHP strings, so `"//*[@title='$classname']"` is sufficient. No need for superfluous concatenation. – tadman Apr 03 '20 at 20:56