Jump to content

Recommended Posts

I have a php script that looks at a supplier website and queries from a pricing calculator, the script displays the result in a table. The result is my actual cost from my supplier and the price is displayed as a string.

 

I need to convert my wholesale cost from a string to a value and then add my retail markup cost then display the Retail Cost as a string in my table.

 

Unfortunately, I am a complete newbie and have been searching various methods, but no matter what I try, it fails.

 

Here is the script:

 

<?php
// Request parameters
$materialid = $_GET['materialid'];
$sizeid = $_GET['sizeid'];
$quantity = $_GET['quantity'];
$numcolors = $_GET['numcolors'];
// form URL for request
//$url = "http://www.suppliersite.com/pricing?materialid=104&sizeid=37&quantity=1&numcolors=1";
$url = "http://www.suppliersite.com/pricing?materialid=".$materialid."&sizeid=".$sizeid."&quantity=".$quantity."&numcolors=".$numcolors;
// Create DOM from URL or file
$html = file_get_html($url);
// Echo page
//echo $html->plaintext;

$price_per_item = $html->find('#price-per-item');
$total_price = $html->find('#total-price');
$description = $html->find('.description');
$name = $html->find('h3');
//echo $price_per_item[0]."<br />";
//echo $total_price[0]."<br />";
//echo $description[0]."<br />";
//echo $name[1]."<br />";

// Iterate through all combinations:
echo "<table id=\"products\">";
echo "<tr>";
//echo "<th>Price per item</th><th>Total Price</th><th>Product</th><th>Description</th><th>Size</th><th>Quantity</th>";
echo "<th width=\"25%\">Price per item</th><th width=\"15%\">Total Price</th><th width=\"30%\">Product</th><th width=\"10%\">Size</th><th width=\"10%\">Colors</th>";
echo "<tr>";
$price_per_item = $html->find('#price-per-item');
$total_price = $html->find('#total-price');
$description = $html->find('.description');
  $size = $html->find('select[id='.'top-sizes-select'.'] option[value='.$sizeid.']');	  
  echo"<tr>";
 echo "<td>".$price_per_item[0]."</td>".
  "<td>".$total_price[0]."</td>".
  "<td>".$name[1]->innertext."</td>".
//   "<td>".$description[0]."</td>".  
  "<td>".$size[0]->innertext."</td>".  
  "<td>".$numcolors."</td>";
echo "</table>";
echo "<br />";
echo "<div style=\"width:40%\">";
echo $description[0];
echo "</div>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<head>
<title>Extractor</title>
<style media="screen" type="text/css">
#products
{
font-family:Arial, Helvetica, sans-serif;
width:60%;
border-collapse:collapse;
font-size:12px;
}
#products td, #products th
{
font-size:1em;
border:1px solid #777777;
padding:3px 7px 2px 7px;
}
#products th
{
font-size:1.1em;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#777777;
color:#ffffff;
}

</style>
</head>
<body>

</body>
</html>

 

Attached is a screenprint of the results, you will see the cost per item is $11.55 and I simply want to add 20% or as in the code: = price-per-item*1.2 -----so the price should show $13.86 per item

 

I have tried to define a value such as:

 

$RETAIL_price_per_item = $html->find(int('#price-per-item')*1.2);

 

but that does not work

 

I have tried many other versions and in different areas of the code but nothing works.

 

Any help would be greatly appreciated. Thanks

post-139134-0-30167200-1360022227_thumb.jpg

Does anyone have a clue where to start?

 

What I do know is that with these two lines of code that it gets the price from the supplier and Displays in the table (see screenprint) as a string:

 

$price_per_item = $html->find('#price-per-item');

$total_price = $html->find('#total-price');

 

 

What I need is instead of displaying it right away in the table, but be able to calculate the markup on that string as a value, then display it in the table.

 

Does that make sense? It seems like it should be pretty simple?

 

Please help.

PHP is a loosely typed language. Part of that means that the PHP parsing engine can dynamically juggle different types of variables for different needs. So, it can do math on the number value of 5 or the string of "5".

 

$product = 5 * 5;
echo $product; //Output: 25

$product = "5" * "5";
echo $product; //Output: 25

 

However, if the parser cannot interpret a string as a number that process would obviously fail. Based upon the image you provided I will make the assumption that the string you are getting from your supplier's site is something such as "$11.55". It is the dollar sign that would be causing the problem. Therefore, you could solve the problem by stripping out all non-numeric and non periods from the string. You will then have a string that can be interpreted as a number by PHP

$price_per_item = preg_replace("#[^\d\.]#", $html->find('#price-per-item'));
$total_price = preg_replace("#[^\d\.]#",$html->find('#total-price'));

 

NOTE: If there are more than 1 periods in a value it will not be a valid number after that conversion.

Well, I have tried just about every variation that I can think of and keep getting errors, here is what I tried, everything fails:

 

$price_per_item = ("$html->find('#price-per-item')" * "1.2");

This was an attempt to define the markup as a string, did not work.

 

$price_per_item = $html->find('#price-per-item');
$retail_per_item = "$price_per_item" * "1.2";

echo "<td>".$retail_per_item[0]."</td>".

 

This is where I tried to define the Retail variable with the Price * 1.2 - - still did not work.

 

$price_per_item = preg_replace("#[^\d\.]#", $html->find('#price-per-item'), * '1.5');

 

Another fail.

 

 

I have tried many more, but am at a total loss. It seems like every time I try to us a * it give me an unexpected operand, so I am not sure how to get this to work.

 

Any advice what I am doing wrong?

Do a var_dump ($html->find('#price-per-item')) and see what the function returns, once you know that the way forward should be a bit clearer.

Also, no need to define the number you're multiplying with as a string. That just adds an unnecessary conversion for PHP, as it'll have to cast it to a number anyway. Not to mention that using *1.5 as the third parameter to preg_replace () doesn't make any sense. You really aught to read up in the PHP manual on how to use that function, and any other function/operation you're uncertain about.

Edited by Christian F.

Sorry for my ignorance, I realize I do not have a clue what I am doing, I tried to find the details on the function, but since I am prettly clueless, I feel like a brain surgeon with a soldering iron, not goona work too well. But a person has to learn somewhere and this is a big project for a newbie, I admit. So thank you for your patience.

 

I have tried other variations that do not (I think) define it as a string but I get a errors. for example:

$price_per_item = $html->find('#price-per-item');
$retail_per_item = $price_per_item * 1.2;

echo "<td>".retail_per_item[0]."</td>".

 

I am also unsure where to place the var_dump function in the code?

Edited by mahogan

Did you event try the code I provided??? I don't see it listed as any of your attempts.

 

$price_per_item = preg_replace("#[^\d\.]#", $html->find('#price-per-item'));
$total_price = preg_replace("#[^\d\.]#",$html->find('#total-price'));

Honestly, I am very confused where to place the preg_replace() code exactly. Then I am not sure where to do the math markup. Here is what I have, with the preg_replace() in the existing code:

 

<?php
// Request parameters
$materialid = $_GET['materialid'];
$sizeid = $_GET['sizeid'];
$quantity = $_GET['quantity'];
$numcolors = $_GET['numcolors'];
// form URL for request
//$url = "http://www.suppliersite.com/pricing?materialid=104&sizeid=37&quantity=1&numcolors=1";
$url = "http://www.suppliersite.com/pricing?materialid=".$materialid."&sizeid=".$sizeid."&quantity=".$quantity."&numcolors=".$numcolors;
// Create DOM from URL or file
$html = file_get_html($url);
// Echo page
//echo $html->plaintext;
$price_per_item = $html->find('#price-per-item');
$total_price = $html->find('#total-price');
$description = $html->find('.description');
$name = $html->find('h3');
//echo $price_per_item[0]."<br />";
//echo $total_price[0]."<br />";
//echo $description[0]."<br />";
//echo $name[1]."<br />";
// Iterate through all combinations:
echo "<table id=\"products\">";
echo "<tr>";
//echo "<th>Price per item</th><th>Total Price</th><th>Product</th><th>Description</th><th>Size</th><th>Quantity</th>";
echo "<th width=\"25%\">Price per item</th><th width=\"15%\">Total Price</th><th width=\"30%\">Product</th><th width=\"10%\">Size</th><th width=\"10%\">Colors</th>";
echo "<tr>";
// ORIGINAL CODE $price_per_item = $html->find('#price-per-item');
// ORIGINAL CODE $total_price = $html->find('#total-price');
// ALTERED CODE next two lines only
$price_per_item = preg_replace("#[^\d\.]#",$html->find('#price-per-item'));
$total_price = preg_replace("#[^\d\.]#",$html->find('#total-price'));
$description = $html->find('.description');
$size = $html->find('select[id='.'top-sizes-select'.'] option[value='.$sizeid.']');
	 echo"<tr>";
echo "<td>".$price_per_item[0]."</td>".
"<td>".$total_price[0]."</td>".
"<td>".$name[1]->innertext."</td>".
// "<td>".$description[0]."</td>".
"<td>".$size[0]->innertext."</td>".
"<td>".$numcolors."</td>";
echo "</table>";
echo "<br />";
echo "<div style=\"width:40%\">";
echo $description[0];
echo "</div>";

?>

Edited by mahogan

Could this be partially why this doesn't work? I found this similar situation: located here: http://php.net/manua...reg-replace.php

 

 

If there's a chance your replacement text contains any strings such as "$0.95", you'll need to escape those $n backreferences:

<?php
function escape_backreference($x)
{
return preg_replace('/\$(\d)/', '\\\$$1', $x);
}
?>

 

If so, I would not know where to incorporate these either. I tried a few places, but without success.

Edited by mahogan

OK, I missed putting the empty string replacement in those lines. Try

$price_per_item = preg_replace("#[^\d\.]#", '', $html->find('#price-per-item'));
$total_price = preg_replace("#[^\d\.]#", '', $html->find('#total-price'));

Thank you so much for your patience and help!

 

Okay, I think that must work, as I now see in the table just the number 11.55 with no $. So that must mean that it is a value.

 

So where would I put the markup calculation, as no matter where I try to define it, it gives me an unexpected operand error?

 

Then how do I get the $ sign back after the math?

Edited by mahogan

So I have tried a few things for the math but keep getting this error: Fatal error: Unsupported operand types in Line 43

42   // Attempt to define Markup Value of retail_per_item
43	 $retail_per_item=$price_per_item*1.2;

 

Here is all my modified code from where I changed it to the end:

 

// ORIGINAL CODE $price_per_item = $html->find('#price-per-item');
// ORIGINAL CODE $total_price = $html->find('#total-price');
// ALTERED CODE next two lines only
$price_per_item = preg_replace("#[^\d\.]#", '', $html->find('#price-per-item'));
$total_price = preg_replace("#[^\d\.]#", '', $html->find('#total-price'));
$description = $html->find('.description');
$size = $html->find('select[id='.'top-sizes-select'.'] option[value='.$sizeid.']');
// Attempt to define Markup Value of retail_per_item
$retail_per_item=$price_per_item*1.2;

var_dump ('$retail_per_item');
echo $retail_per_item;

  echo"<tr>";
// (Original Line, Altered 1 line below)  echo "<td>".$price_per_item[0]."</td>".
echo "<td>".$retail_per_item[0]."</td>".
 "<td>".$total_price[0]."</td>".
 "<td>".$name[1]->innertext."</td>".
 // "<td>".$description[0]."</td>".
 "<td>".$size[0]->innertext."</td>".
 "<td>".$numcolors."</td>";
echo "</table>";
echo "<br />";
echo "<div style=\"width:40%\">";
echo $description[0];
echo "</div>";

?>

I just dont get why the math does not work like this:

 

$retail_per_item = $price_per_item * 1.2;

 

I have been reading all kinds of instruction and posts on doing math in php and it seems so simple. I have tried countless attempts in varing places, yet this is so excruciatingly difficult! Argh!

The math does work like that, if the number you're fetching looks like a number. Something we all have tried to get you to tell us if it does or not. In short: Show us exactly how the number looks like on the site, or (even better) after you've retrieved it, and we don't have to guess.

The var_dump () line I gave above could have gone just about everywhere in your code, but a logical place would be right below the line where you were fetching the price originally. Add it, and give us the exact output from it, and we'll be able to tell you exactly what's wrong. If you don't see it yourself first.

Edited by Christian F.

Sorry for my confusion with this, and again, thank you for your patience and response.

 

Unfortunately it does not seem to matter where I put the var_dump that you listed above, it seems to crash, where it just hangs with the message: "Getting price. Please wait...."

 

Here is the code and where I added it:

 

// Echo page
//echo $html->plaintext;

$price_per_item = $html->find('#price-per-item');
var_dump ($html->find('#price-per-item'));
$total_price = $html->find('#total-price');
$description = $html->find('.description');
$name = $html->find('h3');

 

After that did not work, I removed it and added it here:

 

echo "<tr>";
$price_per_item = $html->find('#price-per-item');
var_dump ($html->find('#price-per-item'));
$total_price = $html->find('#total-price');
$description = $html->find('.description');
$size = $html->find('select[id='.'top-sizes-select'.'] option[value='.$sizeid.']');	
 echo"<tr>";

 

It still does the same thing, just hangs.

 

When I view the source of my supplier site, it looks like this:

</div>
   <ul class="clear pricing-summary">
 <li>Price per Item <strong><span id="price-per-item">$11.55</span> each</strong></li>
 <li>Your total price<strong><span id="total-price">$11.55</span> total</strong></li>

 

If it is a number or a string? I would say a string.

Edited by mahogan

Make sure you have error reporting turned on, and that you're displaying the errors. Sounds very much like you have a fatal error in there somewhere, and not displaying it.

Check Jessica's signature above, for more information on how to proceed with error reporting.

Perhaps I must be misunderstanding this as well, but I added this to the top of the code just under <?php

 

error_reporting(-1);

 

I still do not get any results, just hangs as before. I checked my server settings in my phpinfo report they are as follows:

 

disable_functions        no value    no value
display_errors            On        On
display_startup_errors    Off    Off
error_append_string        no value    no value
error_log            no value    no value
error_prepend_string        no value    no value
error_reporting            no value    no value
html_errors            On        On
ignore_repeated_errors        Off        Off
log_errors            Off        Off
log_errors_max_len        1024        1024
track_errors            Off        Off
xmlrpc_error_number        0        0
xmlrpc_errors            Off        Off

 

 

Do I need to adjust some of these to get the error reporting working? If, so I need to call my host to adjust these settings.

 

Or is it something else?

 

On another note, not sure if this is relevant, but the php script in question does work along side another parser file: include('simple_html_dom.php'); However, I searched in this file for any relevant strings, etc and see nothing.

Edited by mahogan

Your code should look like:

//Code that creates $html
$price_per_item = $html->find('#price-per-item');
var_dump ($price_per_item);

 

I think your $html object is trying to find another item with that ID which is impossible.  I've used that dom parser however and not had that problem. But you need to take this down to the most minimal possible and then add stuff back in.

Ok, so I started fresh with my original file, added the var_dump

// Create DOM from URL or file
$html = file_get_html($url);
// Echo page
//echo $html->plaintext;
//Code that creates $html
$price_per_item = $html->find('#price-per-item');
var_dump ($price_per_item);
$total_price = $html->find('#total-price');
$description = $html->find('.description');
$name = $html->find('h3');

 

It still hangs. So I thought I would see if I get a something if I change the variable to this (what the supplier id is:

var_dump ($price-per-item);

 

and it returns this:

int(0)

 

 

At this point, I'm really feeling kinda brain dead about this and am on the verge of hoping someone here does some affordable freelancing, but I really need to know how to make this happen.

Edited by mahogan

In the above post I did not have the error reporting in line 2, so I added it and here is the result:

 

Notice: Use of undefined constant per - assumed 'per' in /homepages/mysite/price.php on line 26

 

Notice: Undefined variable: price in /homepages/mysite/price.php on line 26

 

Notice: Use of undefined constant item - assumed 'item' in /homepages/mysite/price.php on line 26

int(0)

 

Line 26 is this: var_dump ($price-per-item);

Maybe I misunderstood?

 

To reiterate, I first started with my original code, then added this to line 2:

error_reporting(-1);

 

 

Then added what you suggested:

//Code that creates $html
$price_per_item = $html->find('#price-per-item');
var_dump ($price_per_item);

 

The result is it will just hang at: Getting price. Please wait....

 

It does not show me any errors at this stage. So that is why I thought I would try something else, hence the errors shown in a previous post, but now that I know that is completly wrong, I won't go there again.

 

So, I am not sure why it just hangs, I am at a loss. So am I misunderstanding something else?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.