I'm new to PHP and am trying to scrape data from a website I'm using regular expressions, but locating content rental and details in the div is a problem here is my code. Could someone help me out?
<?php
header('content-type: text/plain');
$contents= file_get_contents('http://www.hassconsult.co.ke/index.php?option=com_content&view=article&id=22&Itemid=29');
$contents = preg_replace('/\s(1,)/','',$contents);
$contents = preg_replace('/ /','',$contents);
//print $contents."\n";
$records = preg_split('/<span class="style8"/',$contents);
for ($ix=1; $ix < count($records); $ix++){
$tmp = $records[$ix];
preg_match('/href="(.*?)"/',$tmp, $match_url);
preg_match('/>(.*?)<\/span>/',$tmp,$match_name);
preg_match('/<div[^>]+class ?= ?"style10"[^>]*>(\s*(<div.*(?2).*<\/div>\s*)*)<\/div>/Us',$tmp,$match_rental);//error is here
print_r($match_url);
print_r($match_name);
print_r($match_rental);
print $tmp."\n";
exit ();
}
//print count($records)."\n";
//print_r($records);
//if ($contents===false)
//print 'FALSE';
//print_r(htmlentities($contents));
?>
Here is a sample of the content
>HILLVIEW CROSSROADS4 BED HOUSE</span></div></td>
</tr>
<tr>
<td width="57%" style="padding-left:20px;"><div align="left" class="style10" style="color:#007AC7;">
<div align="left">
Rental;
USD 4,500
</div>
</div></td>
<td width="43%" align="right"><div align="right" class="style10" style="color:#007AC7;">
<div align="right">
No.
834
</div>
</div></td>
</tr>
<tr>
<td colspan="2" style="padding-left:20px;color:#000000;">
<div align="justify" style="font-family:Arial, Helvetica, sans-serif;font-size:11px;color:333300;">Artistically designed 4 bed (all
ensuite) house on half acre of well-tended gardens. Lounge with fireplace opening to terrace, opulent master suite, family room, study. Good finishes, SQ, carport, extra water storage
and generator. ....Details </div></td>
</tr>
</table></td>
</tr>
</table>
<br>
That website doesn't have good css selectors but it's still not to hard to get it with xpath:
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.hassconsult.co.ke/index.php?option=com_content&view=article&id=22&Itemid=29');
$xpath = new DOMXPath($dom);
foreach($xpath->query("//div[#id='ad']/table") as $table) {
// title
echo $xpath->query(".//span[#class='style8']", $table)->item(0)->nodeValue . "\n";
// price
echo $xpath->query(".//div[#class='style10']/div", $table)->item(0)->nodeValue . "\n";
// description
echo $xpath->query(".//div[#align='justify']", $table)->item(0)->nodeValue . "\n";
}
Related
Please, help me grab information from this structure:
<table id="id1" class="class1">
<tbody>
<tr id="id2">
<td>
<span class="class2">
"header text"
</span>
</td>
<td id="d" style="width:10px;">
<img style="width:10px;" src="/images/img1.gif">
</td>
<td id="r" style="width:40%;">
<span class="class2">
<nobr>Headings:</nobr>
</span>
</td>
</tr>
<tr>
<td>
<table class="class1" style="width:100%;">
<tbody>
<tr>
<td width="300" valign="top"></td>
</tr>
<tr>
<td style="padding:0px;">
<div>
<b>Address: </b>
Address text
</div>
<div>
<b>Tel.: </b>
250-1729
</div>
<br>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<img src="/images/img.gif">
</td>
<td>
heading1
<br>
heading2
<br>
heading3
<br>
</td>
</tr>
</tbody>
I want to get:
header text
Address text
Tel. number
but I don't understand, how can I get it with PowerShell.
Firstly, I get this table
$address = "http://address.com"
$page = Invoke-WebRequest $address
$table = $($page.parsedhtml.getElementsByTagName("table") | Where { $_.id -eq 'id1' })
What's next?
How can I call table's children and get their texts?
This is my decision:
$address = "http://address.com"
$page = Invoke-WebRequest $address
$table = $($page.parsedhtml.getElementById("id1")
$tr = $table.getElementsByTagName('tr') | Where { $_.id -eq 'id2' }
$name=($tr.getElementsByTagName('a') | select -First 1).innertext
$divs=$table.getElementsByTagName('div')
foreach ($div in $divs) {
if ($div.innertext -match "address: ") {$adr=$div.innertext -replace "Address: ",""}
if ($div.innertext -match "Tel.: ") {$tel=$div.innertext -replace "Tel.: ",""}
}
$resultmassive+=[string]::Join(";",$name,$adr,$tel)
p.s. Maybe, it is possible to use PowerShell switch-case construcion instead foreach, but it doesn't works for me.
First and foremost: if your elements have an ID use getElementById() instead of getElementsByTagName() with an additional filter. That will give you the correct table (or other element) right away.
When you have the (parent) element you can get nested elements by calling getElementById(), getElementsByTagName(), etc. on the parent:
$nestedTables = $table.getElementsByTagName('table')
In your case you want to get
the child element with the ID id2 and then the (grand)child <a> element (for the header text)
$tr = $table.getElementById('id2')
$tr.getElementsByTagName('a')
the <div> elements in the nested table (for the address and phone number):
$table.getElementsByTagName('div')
I have created a php file called search.php. I want to show the result on a table
I want a table structure like this.
__________________________
| |________________|
| |________________|
| |________________|
|________|________________|
|________________|
|________________|
On the left side I want to place a picture which will be obtained from database and to the right I will place information about the student photo.
Could you write a code. Can we separate photo and the information column by a vertical line only.(if yes,HOW ?? )
<table>
<tr>
<td rowspan="4" style="border-right: 1px solid black">
<img src"whatever.png" alt="" />img
</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td rowspan="2">
</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
</table>
but really: learn html an get used to the boxed model, that is: use divs
<?php
$sql = "Select FROM * `yourtablename` where `id` = 'theidofthestudent'";
if(!mysql_query($sql)){
die(mysql_error());
} else {
echo "<table border="1px" style="text-align:center;" align="justify">";
echo "<th>Photo of student</th>";
echo "<th>Name of the student</th>";
echo "<th>Student Detials</th>";
while($row = mysql_fetch_array($sql)){
echo "<tr>";
echo "<td style="float:left;">' . $row['student_pic'] . "</td>";
echo '<td style="float:right;">' . $row['student_name'] . "</td>";
echo '<td style="float:right;">' . $row['student_details'] . "</td>";
echo "</tr>"
} echo "</table>";
}
?>
I am working on a splash page which contains a form. When the form is Submitted my add.php writes the form input fields to a SQL server, then it is supposed to send an html email to the person who filled out the form.
Issue #1: The email is received, but it displays the html markup; not the formatted version of the email.
Issue #2: The "From" field is being populated with "text/html" and displays the webserver: tribeca.websitewelcome.com. It is not showing the specified email.
Here is the contents of add.php
<?php
$name=$_POST['Name'];
$email=$_POST['Email'];
$to = $_POST['Email'];
$subject = 'Company Name Webinar Contact';
$message = '<!DOCTYPE html>
<html>
<head>
<title>Email</title>
</head>
<body style="background-color: #e9e9e9;">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr> <td style="width:600px;"> </td> </tr>
<tr> <td> <table cellspacing="0" cellpadding="0" width="600px" border="0" style="background-color: #fff;margin:0 auto;border:solid 10px #fff;font-family: lucida grande, sans-serif;">
<tr> <td style="height:100px;"> <center> <img src="eq-logo.jpg" /> </center> </td> </tr>
<tr style="height:1px; background-color:#dcdcdc"> <td > </td> </tr>
<tr style="height:1px; background-color:#f1f5ee"> <td > </td> </tr>
<tr style="height:100px;"> <td > <h1 style="color:#e10621;">Thank you for your enquiry!</h1> <h3>Someone from our team will contact you shortly.</h3> <p style="line-height:28px;">We've helped hundreds achieve their financial dreams and look forward to showing YOU how to turn YOUR common "cents" into dollars. Please take a few moments to check out the other areas of our website. </p> <p><b>The Company Name Team</b></p> </td> </tr>
</table> </td> </tr>
<tr> <td> <center> <img src="bottom-shadow.png" /> </center> </td> </tr>
</table>
</body> </html>
';
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type:text/html;charset=iso-8859-1' . '\r\n';
$headers .= 'From: <donotreply#companyname.com.au>' . "\r\n";
// Connect
mysql_connect("localhost", "db_user", "user_password") or die(mysql_error()) ;
mysql_select_db("db_name") or die(mysql_error()) ;
// Write
mysql_query("INSERT INTO `table_name` VALUES ('$name', '$email')") ;
// Email
mail($to,$subject,$message,$headers);
header('Location: http://companyname.com.au');
exit();
?>
Additional Notes:
I have tried separating the 'message' out into line by line html as in chris coyiers html email tut with no luck. Here you see the html in its entirety in the message area.
It's your quotes! Use double quotes for your \r\n lines:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=iso-8859-1' . "\r\n";
$headers .= 'From: <donotreply#companyname.com.au>' . "\r\n";
I am not a trained programmer. Whatever I learned is by myself from the net. Please excuse if you find my code and question very amateurish.
I am trying to call matches of buyers against sellers data from the database and automatically mail them their matches.
When I run this code on my system with EasyPhp, it says 'Query was empty". And when I run this on my server, it says "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 " where am I going wrong ?
<?php
include('adminmaster.htm');
Echo "<center>Mail Matching Properties</center><br><br>";
mysql_connect("localhost", "username","password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$query= 'SELECT*FROM clientdata WHERE email != " " AND Transaction="Buyer"';
$result=mysql_query($query);
echo mysql_error();
if (!$result){ die("No Buy results");}
$matchrows=mysql_num_rows($result);
if($matchrows==0){ die("No Buy results");}
while($info = mysql_fetch_array($result))
$Price1= $info['Price']+($info['Price']*10/100);
$Price2 = $info['Price']-($info['Price']*10/100);
$una=$info['username'];
$query2='SELECT*FROM clientdata WHERE email!=" " AND Transaction="Seller"';
$result2=mysql_query($query2);
echo mysql_error();
$matchrows2=mysql_num_rows($result2);
if($matchrows2==0){ die("Sell Data Not Found");}
if(!$result2)
{die("Sell Data Not Found");}
while($info2=mysql_fetch_array($result2))
{
if ($info['Detail']==$info2['Detail'])
{
if ($info['Location']==$info2['Location'])
{
if ($Price1 >= $info2['Price'])
{
if ($Price2 <= $info2['Price'])
{
if ($info['username']!=$info2['username'])
{
echo mysql_error();
echo "<table width=600 border = 1 style='text-align:left';>
<tr>
<td><b>Clients</b></td>
<td><b><font color='maroon'>BUYER</font></b></td>
<td><b><font color='maroon'>SELLER</font></b></td>
</tr>
<tr>
<td><b>Date Posted:</b></td>
<td>$info[Date]</td>
<td>$info2[Date]</td>
</tr>
<tr>
<td><b>Title:</b></td>
<td>$info[propheading]</td>
<td>$info2[propheading]</td>
</tr>
<tr>
<td><b>Transaction:</b></td>
<td>$info[Transaction]</td>
<td>$info2[Transaction]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Detail:</b></td>
<td style='background-color:lightgrey'>$info[Detail]</td>
<td style='background-color:lightgrey'>$info2[Detail]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Price:</b></td>
<td style='background-color:lightgrey'>$info[Price]</td>
<td style='background-color:lightgrey'>$info2[Price]</td>
</tr>
<tr>
<td><b>Additional Data:</b></td>
<td>$info[AdditionalData]</td>
<td>$info2[AdditionalData]</td>
</tr>
<tr>
<td><b>Location:</b></td>
<td>$info[Location]</td>
<td>$info2[Location]</td>
</tr>
<tr>
<td><b>Agent/Individual:</b></td>
<td>$info[Agent_Ind]</td>
<td>$info2[Agent_Ind]</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td>$info[Title] $info[firstname] $info[lastname]</td>
<td>$info2[Title] $info2[firstname] $info2[lastname]</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$info[Phone]</td>
<td>$info2[Phone]</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$info[Email]</td>
<td>$info2[Email]</td></tr>
</table>";
$tomail=$info2['Email'];
if ($tomail=="anitgopinath#gmail.com"|$tomail=="anit#landshoppe.com"|$tomail=="info#landshoppe.com") {continue;}
$headers ="From:info#landshoppe.com"."\r\n";
$headers .='Bcc:anit#landshoppe.com'."\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$subject ="You have a Property Match !";
$message="<html></head><body>
<table width=600 border = 1 style='text-align:left';>
<caption><b><font color='maroon'>YOUR PROPERTY MATCH!</font></b></caption>
<tr>
<td><b>Date Posted:</b></td>
<td>$info[Date]</td>
</tr>
<tr>
<td><b>Title:</b></td>
<td>$info[propheading]</td>
</tr>
<tr>
<td><b>Transaction:</b></td>
<td>$info[Transaction]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Detail:</b></td>
<td style='background-color:lightgrey'>$info[Detail]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Price:</b></td>
<td style='background-color:lightgrey'>$info[Price]</td>
</tr>
<tr><td><b>Additional Data:</b></td><td>$info[AdditionalData]</td></tr>
<tr><td><b>Location:</b></td><td>$info[Location]</td></tr>
<tr><td><b>Agent/Individual:</b></td><td>$info[Agent_Ind]</td></tr>
<tr><td><b>Name:</b></td><td>$info[Title] $info[firstname] $info[lastname]</td></tr>
<tr><td><b>Phone:</b></td><td>$info[Phone]</td></tr>
<tr><td><b>Email:</b></td><td>$info[Email]</td></tr>
</table></body><html>";
echo $tomail."<br>";
echo "$message"."<br>";
$mailresult=mysql_query(mail($tomail,$subject,$message,$headers));
if ($mailresult){echo "Mailed<br>";}else{echo "Not Mailed !<br>";}
echo "**********************************Seller Match**************************************************<br>";
$tomail2=$info['Email'];
if ($tomail2=="anitgopinath#gmail.com"|$tomail2=="anit#landshoppe.com"|$tomail2=="info#landshoppe.com"){continue;}
$headers ="From:info#landshoppe.com"."\r\n";
$headers .='Bcc:anit#landshoppe.com'."\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$subject ="You have a Property Match !";
$message="<html></head><body>
<table width=600 border = 1 style='text-align:left';>
<caption><b><font color='maroon'>YOUR PROPERTY MATCH!</font></b></caption>
<tr>
<td><b>Date Posted:</b></td>
<td>$info2[Date]</td>
</tr>
<tr>
<td><b>Title:</b></td>
<td>$info2[propheading]</td>
</tr>
<tr>
<td><b>Transaction:</b></td>
<td>$info2[Transaction]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Detail:</b></td>
<td style='background-color:lightgrey'>$info2[Detail]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Price:</b></td>
<td style='background-color:lightgrey'>$info2[Price]</td>
</tr>
<tr>
<td><b>Additional Data:</b></td>
<td>$info2[AdditionalData]</td>
</tr>
<tr>
<td><b>Location:</b></td>
<td>$info2[Location]</td>
</tr>
<tr>
<td><b>Agent/Individual:</b></td>
<td>$info2[Agent_Ind]</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td>$info2[Title] $info2[firstname] $info2[lastname]</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$info2[Phone]</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$info2[Email]</td>
</tr>
</table></body><html>";
echo $tomail2."<br>";
echo "$message"."<br>";
$mailresult2=mysql_query(mail($tomail2,$subject,$message,$headers));
if ($mailresult2){echo "Mailed<br>";}else{echo "Not Mailed !<br>";}
echo "**********************************Buyer Match**************************************************<br>";
}
}
}
}
}
}
}
?>
First of all, when specifying string literals within SQL request you should use single qoutes:
$query= "SELECT*FROM clientdata WHERE email != ' ' AND Transaction='Buyer'";
Don't want to sound rude, but you better redesign your code, it's a mess right now. Start with a simple 'divide and conquer' method: mark each block of code that does ONE and only ONE thing - and separate into a single function (leaving only its call in the main script). You'll instantly notice the difference it'll make in both reading and updating your code.
The error you spoke about is caused by this line:
$mailresult=mysql_query(mail($tomail,$subject,$message,$headers));
... as the result of mail function is boolean. And when converted into string by mysql_query, it's either an empty string or '1'; both of them, of course, are not valid queries. If you need to check the result, it's just...
$mailresult=mail($tomail,$subject,$message,$headers);
And again, the error itself is simple - but it's hidden in your code so good that five or more SO users failed to find it. ) That's why the code needs restructuring.
I have a sortable table in html and looking to put an add.png inside each table header so if a user wants to add another url - they just click the add.png and redirects them to the addurl.php (if they click outside it will still sort the table just like normal). Right now it sorting when I click on the .png and/or outside it. If this something that can't be done, I've also thought about trying to add a final row with [add another site] but have no idea how to work it into the $j++ so it appears at the final row.
what it looks like
[PHP snippet]
<table class="datatable sortable selectable full">
<thead>
<tr class="theader">
<th width="100%">
<b><li><img src="images/logos/googlebuzz-2-icon.png" height="18" border="0" />Google <img src="img/icons/add.png" height="18" border="0" align="right"/></li></b>
</th>
<th>
<b>Coins</b>
</th>
<th>
<b>CPC</b>
</th>
</tr>
</thead>
<tbody>
<?
$site2 = mysql_query("SELECT * FROM `sites` WHERE `user`='{$data->login}' ORDER BY `cpc` DESC");
for($j=1; $site = mysql_fetch_object($site2); $j++)
{
?>
<tr>
<td>
<? if($site->banned == 1){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->title); ?></font><span class="edit"></span>
</td>
<td align="right">
<? if($site->points <= 10){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->points); ?></font><span class="add"></span>
</td>
<td>
<? echo $site->cpc;?>
</td>
</tr>
<?}?>
</tbody>
</table>
First of all, instead of a for loop, you should use while:
while($site = mysql_fetch_object($site2))
If you want to add a final row, just add it outside the iteration, before </tbody>:
<tbody>
<? while($site = mysql_fetch_object($site2)): ?>
<tr>
<td>
<? if($site->banned == 1){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->title); ?></font><span class="edit"></span>
</td>
<td align="right">
<? if($site->points <= 10){ ?><font color="red"><? }else{ ?><font color="green"><? } echo($site->points); ?></font><span class="add"></span>
</td>
<td>
<? echo $site->cpc;?>
</td>
</tr>
<? endwhile; ?>
<tr>
<td colspan="3">
[add another site]
</td>
</tr>
</tbody>
It seems you are using a javascript to do the sorting. I don't know if that can be changed so your add button in the header works.
You can check if it's the final row by checking the actual row against all rows:
$site2 = mysql_query("SELECT * FROM `sites` WHERE `user`='{$data->login}' ORDER BY `cpc` DESC");
$rows = mysql_num_rows($site2);
$i = 0;
And add this in your for loop:
$i++;
if($rows == $i) {
// do something only at the final row
}