0

For example if I have an array with one element as follows:

$dom = [file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964', false)];

How can I echo $dom[0]; and have it return JUST the URL (or the text value at minimum) instead of returning the result of the function?

Thank you.

Code:

<?php 
include "db.php";
include "Includes/header.php";
require 'simple_html_dom.php';

$dom = [file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964', false),
        file_get_html('https://www.homedepot.com/p/31180154554', false),
        file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-Adapts-to-20-Amp-CESMAD3020/100128920', false)
       ];

$answer = array();
for($i = 0; $i < sizeof($dom); ++$i)
{
if(!$dom[$i]->find('span#ajaxPrice',0))
{
    $formattedUrl = '$dom[$i] - SHOW URL HERE THAT SEARCH WAS ATTEMPTED ON';    
    echo "<tr><td>" . "Not Listed" . "</td><td>" . $formattedUrl . "</td><td>" . "Not Listed" . "</td></tr>";
    continue;
}

$element = $dom[$i]->find('span#ajaxPrice',0);
$title = $dom[$i]->find('h1.product-title__title',0);
$sku = $dom[$i]->find('h2.product_details.modelNo',0);
$formattedSku = str_replace("Model # ", "", $sku);
$titlePlain = $title->plaintext;

if(isset($element->content)) 
{
    $price = $element->content; 
    $priceFloat = floatval($price); 
}

$query = "INSERT INTO homedepot(Date,Title,Price) VALUES (CURDATE(), ?, ?)";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "sd", $titlePlain, $priceFloat);
mysqli_stmt_execute($stmt);

echo "<tr><td>" . $formattedSku . "</td><td>" . $titlePlain . "</td><td>" . $price . "</td></tr>";

sleep(1);
}

?>
7
  • With removing file_get_html? Commented Feb 4, 2020 at 20:12
  • @u_mulder preferable but not absolutely necessary Commented Feb 4, 2020 at 20:14
  • Delete file_get_html, no? Commented Feb 4, 2020 at 20:15
  • You need to come at this a different way and describe what you are trying to do at a higher level. Commented Feb 4, 2020 at 20:21
  • @AbraCadaver I am trying to display the contents of the array[element] as a STRING instead of the evaluation of a function - when specified HTML elements are not found on the searched URL page. See screenshot as I think it helps to clarify: prnt.sc/qxkyxx Commented Feb 4, 2020 at 20:26

1 Answer 1

2

Just store the URL in the array and then execute the file_get_html when you need it:

$urls = ['https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964',
         'https://www.homedepot.com/p/31180154554',
         'https://www.homedepot.com/p/Connecticut-Electric-30-Amp-Adapts-to-20-Amp-CESMAD3020/100128920'
        ];

foreach($urls as $url)
{
    $dom = file_get_html($url, false);

    if(!$dom->find('span#ajaxPrice', 0))
    {
        echo "<tr><td>" . "Not Listed" . "</td><td>" . $url . "</td><td>" . "Not Listed" . "</td></tr>";
        continue;
    }
    // more code . . .
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that's exactly what I was looking for, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.