Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: simple_html_dom zapis do bazy
Forum PHP.pl > Forum > PHP
michalbd93
  1. $html = file_get_contents('https://www.strona.pl');
  2. //Create a new DOM document
  3. $dom = new DOMDocument;
  4.  
  5. //Parse the HTML. The @ is used to suppress any parsing errors
  6. //that will be thrown if the $html string isn't valid XHTML.
  7. @$dom->loadHTML($html);
  8.  
  9. //Get all links. You could also use any other tag name here,
  10. //like 'img' or 'table', to extract other tags.
  11. $links = $dom->getElementsByTagName('a');
  12.  
  13. //Iterate over the extracted links and display their URLs
  14. foreach ($links as $link){
  15. // tutaj chce zapisać do bazy danych informacje normalnie sie wyswitlają ale nie zapisuje do bazy
  16. }
viking
Po pierwsze gdzie tu masz simple_html_dom? A po drugie jak ten zapis robisz?
michalbd93
simple mam wyżej to jest tylko kawałek kodu
to jest w foreach
echo $link->getAttribute('href');

i chciałbym zapisać to do bazy
$z = $mysqli->query("INSERT INTO test SET nazwa='$link'");
viking
To skoro używasz simple to po co to wstawiasz domdocument? Masz połączenie? Jakieś błędy w logach? Podaj więcej informacji jak to robisz a nie każesz zgadywać.
michalbd93
  1. <?php
  2. require("simple_html_dom.php");
  3.  
  4. $html = file_get_contents('https://www.wp.pl');
  5. //Create a new DOM document
  6. $dom = new DOMDocument;
  7.  
  8. //Parse the HTML. The @ is used to suppress any parsing errors
  9. //that will be thrown if the $html string isn't valid XHTML.
  10. @$dom->loadHTML($html);
  11.  
  12. //Get all links. You could also use any other tag name here,
  13. //like 'img' or 'table', to extract other tags.
  14. $links = $dom->getElementsByTagName('a');
  15.  
  16. //Iterate over the extracted links and display their URLs
  17. foreach ($links as $link){
  18.  
  19. //Extract and show the "href" attribute.
  20. echo $link->getAttribute('href');
  21.  
  22. }
  23.  
  24. ?>

powiedzmy ze to jest cały kod i chciał bym te wyniki z $link zapisać do bazy
mam połączenie z baza danych ale jak chce tam zapisać to wyskakuje bład
Catchable fatal error: Object of class DOMElement could not be converted to string in /prz/index.php on line 35


mam nadzieję że dobrze opisałem
viking
$z = $mysqli->query("INSERT INTO test SET nazwa='$link->getAttribute('href')'");
Na przyszłość podawaj błędy. I dalej nigdzie w tym kodzie nie używasz simple html
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.