Can't get Xpath to only output some TD's - dom

It's probably really easy if you know how, but I don't, and after spending hours Googling it I have to ask some "real" programmers, as I'm obviously not one.
I can't seem to find a tutorial or a code example that'll work for me. Let's say I just wanted to output the "EuroDiesel 10" TR (scroll halfway down to find it) and then I only want data from TD number 1 and 9. How would I go about doing that?
I also want to add the output data to a SQL DB with a date stamp as well as update it once a day. I assume this can be done with a Cron Job, is this correct and should a make a job for each price list I want to harvest data from or could I do it in a single script (the sites are very different)?
First of all I just need the correct data. This is what I got so far.
<?php
$dom = new DOMDocument;
$date = date("j. F, Y");
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://www3.statoil.com/mar/kbh00438.nsf/UNID/8C81E46A6EC8BA3BC12578C0002FFF5A?OpenDocument');
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$aTag = $xpath->query('//p[#class="text"]');
foreach($aTag as $val) {
echo $date, '', $val->plaintext. "". utf8_decode(trim($val->nodeValue, "")) . "<br />\n";
}
?>
I hope you guys can help me out, just learning here...
Thanks!
Art

As for the XPath, I think /html/body/form/table/tbody/tr[normalize-space(td[1]) = 'EuroDiesel 10']/td[position() = 1 or position() = 9] should do. Then access $val->textContent instead of nodeValue.

Related

mysql query not working - it's not selecting the table information

I have a query that is working in all my other php files but it will not work here. Can someone please look at what I have and let me know why the query is not selecting the fields?
Please keep in mind that $res is my database connection that's open, and all the table fields are spelled correctly to the table. I have tried not having those single quotes that are sideways and as you can see in the code I have tried it with them. I have also tried not having ' ' and have tried it with them. I can remark out the lines and the code works, but put them back live and the code stops working with no error given.
I don't know if this helps, but my PHP version is 5.3.3-40.el6_6
$result2 = mysqli_query($res, "SELECT pre_sponsor_sponsor, slid FROM `pres` WHERE `pre_sponsor` = '$spospo' AND `slid` = '$slid' AND `pre_id` <= '$pre_id' LIMIT 1");
$row2 = mysqli_fetch_array($result2)
$spospo = $row2[pre_sponsor_sponsor];
$slid = $row2[slid];
First of all you missed semicolon at line 3.
But I think you should write
$row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
Regards.
I see my problem after posting it here. I forgot the semi-colon after the $row2 = mysqli_fetch_array($result2) - it should have a ; after it.
Sorry for posting this.

security for a simple php search form

I have a table that lists movies and I have incorporated a simple search function.
I have one text field in a form where a title or keyword can be entered and then the form is submitted.
php/mysql code that does the work is:
$find = $_POST['find'];
$find = mysql_real_escape_string($find);
$find = htmlspecialchars($find);
$sql = "SELECT * FROM tbl_buyerguide WHERE rel_date BETWEEN NOW() AND DATE_ADD(now(), INTERVAL 2 MONTH) AND title LIKE '%".$find."%' ORDER BY title";
where 'find' is the name of the text input in the search form.
This works well enough for the search functionality for the required purpose.
My question to all is:
Is the mysql_real_escape_string and htmlspecialchars enough to make my search form secure?
I have read all of the questions that I can find on stackoverflow about this, but I would really like someone in the know to just say to me "yes, that is all you need", or "no, you also need to take into account ...".
Thanks in Advance.
Cheers Al.
Remember the adage: Filter In, Escape Out.
You're not outputting the term there, so why are you escaping it for HTML purposes with htmlspecialchars()?
Instead, ONLY escape it for the database (you should be using prepared statements, but that's another point). So you should not be using htmlspecialchars there.
Instead, when you go to output the variable onto the HTML page, that's when you should escape it for HTML (again, using htmlspecialchars).
Right now, you're mixing database and html escaping, which is going to lead to neither being effective...
Yes it is enough to make it secure....you could always throw strip_tags() in there as well....
Although I would just do it in one line...instead of using three
$find = htmlspecialchars(mysql_real_escape_string($_POST['find']));
But to really make it secure and up to date, you should stop using mysql_* functions as they are deprecated, and will be removed in any future relases of PHP....
You should instead switch to either mysqli_* or PDO, and implement prepared statements which handles security for you.
Example...in PDO
$db = new PDO('mysql:server=localhost;dbname=test', 'username', 'password');
$find = $_POST['find'];
$query = $db->prepare('SELECT * FROM tbl_buyerguide WHERE rel_date BETWEEN NOW() AND DATE_ADD(now(), INTERVAL 2 MONTH) AND title LIKE :like ORDER BY title');
$query->bindValue(':like', '%' . $find . '%');
$query->execute();

Perl Net::Google::Calendar event occurrence

I have the following code ( perl ), but I"m not sure how to extract the occurrence events ?
use Net::Google::Calendar;
my #cals = $parent->get_calendars;
for my $cal_selected (#cals) {
printf "CALENDAR: %s\n", $cal_selected->title;
$parent->set_calendar( $cal_selected );
for ( $parent->get_events() ) {
my $title = $_->title || "";
my ($start, $end) = $_->when();
print "title: $title, start: $start, end: $end \n";
}
}
So, one of the events has a "start" var, but it jump every 4 days - and I don't see it in my current output.
Output looks like this:
title: "Event 1", start: 2012-08-06T00:00:00, end: 2012-08-13T00:00:00
title: "Event 2", start: 2012-07-05T00:00:00, end: 2012-07-05T00:00:00
"Event1" has a very clear start and end dates, 6 days ..
See that "Event 2" is at the same day, but the occurances are at 2012-07-05, 2012-07-09, 2012-07-13
But I don't see that in the output itself ...
You might need try the recurrence method. The problem is that if you use this and you don't have the Data::ICal module installed, you'll get an undefinstead of an error. Unfortunately, you'll also get an undef if you have Data::ICal installed, and there's no reoccurrence. That's just bad module programming.
The recurrence method returns an Data::Ical::Entry::Event object.
From there, I'm not entirely sure what to do. The Data::ICal::Entry::Event documentation is really sparse. I had to read through the iCalendar RFC which mentioned that this field is stored in something called rdate. There's a mention of multiple rdate properties in the Data::ICal::Entry::Event module, but nothing that says how to access it, or what format it's in.
I think you're suppose to use Data::ICal::DateTime to help parse this information. This module has a reoccurrence method that allows you to pull out the union of all rdates. Unfortunately, it uses an Data::ICal object, and you have an Data::ICal::Entry::Event object. Maybe that's a subclass of Data::ICal, so maybe it'll still work.
Unfortunately, I don't have Google Calendar setup, so I really have no way of playing around with this. Sorry about not being more definite about this. However, I hope this leads you the right way.
One more recommendation: Use Data::Dumper to dump out the various objects you're getting. This will help you understand what's going on and how information is stored in these objects. With a bit of luck, you'll be able to figure out exactly what methods and modules you need to get the information you want.
It's not polite to peek, but when you don't understand the question, looking up the answer in back of the book can sometimes help.

XML::LibXML parsing and choosing element a quick one

I always get stuck at some logic questions when it comes to programming. This is logical and easy, I think, but hard for me, as I am unable to get there. I am using XML::LibXML to parse XML files. Now in the following code
<CommentsCorrectionsList>
<CommentsCorrections RefType="Cites">
<RefSource>Brain Cogn. 2005 Jul;58(2):245</RefSource>
</CommentsCorrections>
<CommentsCorrections RefType="RepublishedIn">
<RefSource>Brain Cogn. 2005 Jul;58(2):246-8</RefSource>
<PMID Version="1">16044513</PMID>
</CommentsCorrections>
<CommentsCorrections RefType="PartialRetractionOf">
<RefSource>Curr Opin Organ Transplant. 2001 Mar;6(1):95-101</RefSource>
</CommentsCorrections>
</CommentsCorrectionsList>
I want to choose commentscorrections for all other RefType except for 'Cites'. How do I do it. I thought of doing it by putting all the wanted RefTypes in another variable and then using it to get the other data. Is it the right way, I tried with some dummy variables like the following
my $sam = "A" || "B" || "C";
print "test= ";
my $test = <>;
if ($test == $sam) {
print $test;
print "success";} else {
print "NO";}
I know this may be silly for some of you but I writing a program since a month or so and I sometime get frustrated because I don't know what to do. I try to learn many things. Please forgive me if this is really a silly question.
Also, I thought to do
if(!($foo->findnodes('CommentsCorrectionList/CommentsCorrections[#RefType="Cites"]'))){
do foreach and get the data
}
But in this case how do I avoid the RefType="Cites" in foreach and make it equal to the other RefType which I want. I mean I don't know if such boolean can be used in foreach statement. I tried finding and also did trial and error but nothing in hand. Any help is greatly appreciated.
Thank you.
CommentsCorrectionList/CommentsCorrections[#RefType != "Cites"]
I personally use the XPath spec as my reference, but there might be friendlier references out there.

ZenCart product_info.php Quantity Box

I'm hoping someone out there can help me with this. I tried posting it in the zencart forum, but no takers yet - so I'm hoping stackoverflow will be able to help.
In zencart, I've found that the product_info page can display minimum, maximum, and product quantity units. However, it displays it "side by side".
Click to see the image.
I would like to have it displayed as a list item instead, but I'm at a loss on how I can do this.
The furthest I've got is narrowing it down to the tpl_product_info_display.php and in it, I found that it's caused by
$the_button = zen_get_products_quantity_min_units_display((int)$_GET['products_id'])
Thank you in advance!
To those that were also wondering about this... someone managed to answer it in the zen-cart forum.
Head to includes -> Functions - Functions.php (in my case, I found it in functions_prices.php)
Then look for "zen_get_products_quantity_min_units_display" function.
Change the following:
if ($check_min != 1) {
$the_min_units .= PRODUCTS_QUANTITY_MIN_TEXT_LISTING . ' ' . $check_min;
}
To:
if ($check_min != 1) {
$the_min_units .= PRODUCTS_QUANTITY_MIN_TEXT_LISTING . ' ' . $check_min . '<br />';
}
Adding the end, simply creates a new line, getting rid of displaying the units "side by side".