Jump to content

[SOLVED] Do You See a Syntax Error in this One Line of Code?


Modernvox

Recommended Posts

Error reads unexpected T_STRING

 

Line causing the error

 echo "Link = " . $set['link'] ", E-mail = " . $set['email'] . "<br/>";

 

 

complete code

 <?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
   $sets[] = fetch_email($link);
}

foreach($sets as $set) {
   $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


if (!$dbx)
{
die('Could not connect: ' . mysql_error());
}

mysql_SELECT_db("craigslist", $dbx);
mysql_Query("INSERT INTO addresses (sale_items)
VALUES ('$set');

mysql_close($dbx);


echo "Link = " . $set['link'] ", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
   $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
   $page = file_get_contents($page_url);
   preg_match_all($pattern, $page, $matches);
   return $matches[1];
}

function fetch_email($page_link) {
   $pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
   $page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
   preg_match($pattern, $page, $out);
   return array('link'=>$page_link, 'email'=>$out[1]);
}
endforeach;
?>

the problem is here

mysql_Query("INSERT INTO addresses (sale_items)
VALUES ('$set');

 

And why is that?

 

You don't close the string or the function params.

 

mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

 

After fixing this the endforeach; is unexpected everywhere i try to put :shrug:

What's block does the endforeach; at the end relate to ?

 

to help you i have formatted the code (to make it more visible)

 <?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
	$sets[] = fetch_email($link);
}

foreach($sets as $set) {
	$dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


	if (!$dbx)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_SELECT_db("craigslist", $dbx);
	mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

	mysql_close($dbx);


	echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
	$pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
	$page = file_get_contents($page_url);
	preg_match_all($pattern, $page, $matches);
	return $matches[1];
}

function fetch_email($page_link) {
	$pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
	$page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
	preg_match($pattern, $page, $out);
	return array('link'=>$page_link, 'email'=>$out[1]);
}
endforeach;
?>

What's block does the endforeach; at the end relate to ?

 

to help you i have formatted the code (to make it more visible)

 <?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
	$sets[] = fetch_email($link);
}

foreach($sets as $set) {
	$dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


	if (!$dbx)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_SELECT_db("craigslist", $dbx);
	mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

	mysql_close($dbx);


	echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
	$pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
	$page = file_get_contents($page_url);
	preg_match_all($pattern, $page, $matches);
	return $matches[1];
}

function fetch_email($page_link) {
	$pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
	$page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
	preg_match($pattern, $page, $out);
	return array('link'=>$page_link, 'email'=>$out[1]);
}
endforeach;
?>

 

It relates to

foreach($sets as $set) {

But that block ends

foreach($sets as $set) {
    $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


    if (!$dbx)
    {
       die('Could not connect: ' . mysql_error());
    }

    mysql_SELECT_db("craigslist", $dbx);
    mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

    mysql_close($dbx);


    echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}//<----------HERE--------

But that block ends

foreach($sets as $set) {
    $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


    if (!$dbx)
    {
       die('Could not connect: ' . mysql_error());
    }

    mysql_SELECT_db("craigslist", $dbx);
    mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

    mysql_close($dbx);


    echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}//<----------HERE--------

 

Well for some reason beyond my knowledge the foreach is causing the script to load forever?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.