Log in

View Full Version : beginning php programmer ( Wamp , eclipse )



kc11
08-29-2010, 08:27 PM
Hi,

I am a beginning programmer in PHP. I am using WAMP and Eclipse - PDT

I'm running a script in eclipse which attempts to download articles from articlebase.com.

The script is attached. I'm getting syntax errors which I do not understand. The entire script is included.




<?php

$category="hello ";

function get_category_links($category,$page=1)
{
$page = intval($page);
$url = “http://www.articlebase.com/”.strtolower($category).”-articles/$page/”;

$html = file_get_contents($url);
if(!$html) return false;

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$elements = $xpath->query(“//div[@class='article_row']//h3/a”);

$links = array ( );

if (!is_null($elements)) {
foreach ($elements as $element) {
$links[]=$element->getAttribute(‘href’);
}
} else {
return false;
}

array_unique($links);

return $links;
}

?>



the error a line 8 is

" Parse error: parse error in C:\wamp\www\articlebase\ab1.php on line 8 "

a screenshot of the errors is attached

Could anyone explain what the problem is.

Thanks in advance,

KC

djr33
08-29-2010, 09:21 PM
Those are not quotation marks, at least not the standard ones. Those are angled and different for opening and closing, like in Microsoft Word. I don't know why your program is doing this, but I highly suggest getting a better program-- there are a lot out there and many are free. You can even use notepad if you want.
Also, the single quotes are wrong too.

If you want to fix this, you can cut and paste these symbols from my post:
"
'

Just replace all of them in your code above and it will fix that...

kc11
09-02-2010, 09:18 PM
Thanks Daniel,

that got it working. I'm now working on another function , but I'm stumped again. I'm getting a " Parse error: syntax error, unexpected T_FOR " error

My script is below




<?php



$aa = get_article("http://www.articlesbase.com/find-articles.php?q=beauty&page=1");


// echo($aa);




function get_article($url){





$html = file_get_contents($url);



if(!$html) return false;

$result = array();
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

//get title
$elements = $xpath->query("//div[@class='article_pg']//h1");
$element = $elements->item(0);
** $result['title']=strip_tags($element->nodeValue);

//get body
$elements = $xpath->query("//div[@class='article_cnt KonaBody']");
$element = $elements->item(0);
$result['body'] = $dom->saveXML($element);

//get author bio
$result['author_bio']=";
$xpath = new DOMXPath($dom);
$elements = $xpath->query("//div[@class='author_details']/p");


for ($i = 0; $i < $elements->length; $i++ ) { //$paras->length
$element = $elements->item($i);
$result['author_bio'] .= $dom->saveXml($element);



}


return $result;
}




?>



the error is at line 33 ( marked by ** )

does anyone see a problem?

Thanks again,

KC

djr33
09-02-2010, 10:35 PM
You have a lot of extra blank lines. Because of this, line 33 is not at **. Instead it is at the "for" loop, like you'd expect from the error. I don't know if all servers are configured to count like that, but it's worth knowing that your server does this if you plan to add blank lines.

The problem is this line:
$result['author_bio']=";

You don't close that quote or give it any value. Fix that, and this may work, or at least you will find the next problem.

kc11
09-07-2010, 02:54 AM
Hi again,

I am getting a " Trying to get property of non-object" error on line 27

[CODE]

<?php

// $link = "artcile_base_article_link";

// $html = file_get_contents($links);

$aa = get_article("http://www.articlesbase.com/find-articles.php?q=beauty&page=1");

function get_article($url){

$html = file_get_contents($url);
if(!$html) return false;

echo ($html);

$result = array();
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

//get title

$elements = $xpath->query("//div[@class='article_pg']//h1");

$element = $elements->item(0);
** $result['title']= strip_tags($element->nodeValue);



return $result;

}

?>

[CODE]

the problem line is marked by " ** "

Can anyone explain this?

thanks,

KC

fileserverdirect
09-10-2010, 11:26 PM
$element does not become an object when it is set to a value of an object, if item(0) does not return an object, then maybe try:


$result['title']= strip_tags($elements->item(0));

I do not know the class structure that you are using, so this is a guess.