Link to home
Start Free TrialLog in
Avatar of twittoris
twittoris

asked on

IF Statement

I have a script that searches for a certain output from a cURL result.

If there is no link found how can I make it place the current search name in a seperate table and delete the entry from the table being used.
Something like this:

If ($i = NULL) //if no link found
Then
Insert $name[row] TABLE $table
Delete $name[row] TABLE $nametable



// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a[starts-with(@href, 'SEARCHRESULT')]");


for  ($i = 0; $i < $hrefs->length; $i++) 
{
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    $purl = substr($url, 30,300);

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You mean if the $hrefs variable is empty after line 3?  Not sure I understand the question.
Please post the rest of the script so we can see what else is going on, thanks. ~Ray
Avatar of twittoris
twittoris

ASKER

Correct if there is no link found.
Hey ray. The only other thing the code does is echo the result. And if there is no link then the script continues to the next name on the list.
Avatar of Marco Gasi
I think you could do this way:

for  ($i = 0; $i < $hrefs->length; $i++)
{
    $href = $hrefs->item($i);
    if ($href == ''){
        //insert query in  unmatched table
        //and delete search name from main table
    }
    $url = $href->getAttribute('href');
    $purl = substr($url, 30,300);

Hope this helps.

Cheers
The "next name on the list" is what I was looking for when I asked to see the rest of the script.  Without your variable names I cannot do a very good job of showing you the correct code, but maybe this is something close.
<?php // RAY_temp_twittoris.php
error_reporting(E_ALL);

// AN ARRAY OF EMPTIES
$empties = array();

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a[starts-with(@href, 'SEARCHRESULT')]");

// IF EMPTY, ADD IT TO THE COLLECTION OF EMPTIES
if (empty($hrefs)) $empties[] = $dom;


for  ($i = 0; $i < $hrefs->length; $i++) 
{
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    $purl = substr($url, 30,300);

Open in new window

Not working, Close though.

If I write :

for  ($i = 0; $i < $hrefs->length; $i++)
{
    $href = $hrefs->item($i);
    if ($href ->item== ''){
       $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";
            echo "Moved " . $row[name] . "from searchable database into broken search table.";
        //and delete search name from main table
    }
    $url = $href->getAttribute('href');
    $purl = substr($url, 30,300);

This seems to do the opposite then what im looking to accomplish as the output is:

1-9 JACOBUS LLC
Moved 1-9 JACOBUS LLC from searchable database into broken search table.

1-9 JACOBUS LLC
1-9 JACOBUS LLC 25 WEST 45TH STREET SUITE 505 NEW YORK, NEW YORK, 10036

Test 1 CORP
Moved Test 1 CORP from searchable database into broken search table.

Test 1 CORPORATION
COHEN, WEISS AND SIMON 330 WEST 42ND STREET NEW YORK, NEW YORK, 10036-6976

"Test 2, LLC"
Test 3 E
Test 4
Moved Test 4 from searchable database into broken search table.


As you can see Test 2 and 3 have no data in the $href section yet the if statement is being applied to test 1 and 4 which do have $href data.

Also i am not sure how to delete the row that is being used.
If you have a PHP script that parses correctly, would you please post it in the code snippet so we can stop guessing what the variable names and logic structure are?  Thank you, ~Ray
Hi Ray,

Here is the code.

//Display cURL POST Results
//echo curl_exec ($ch);

// make the cURL request to returned HTML
$html= curl_exec($ch);
//echo $html;
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a[starts-with(@href, 'CORPSEARCH.ENTITY_INFORMATION')]");


for  ($i = 0; $i < $hrefs->length; $i++) 
{
    $href = $hrefs->item($i);
	
	if ($href->item == ''){
        $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";
		echo "Moved " . $row[name] . "from searchable database into broken search table.";
		$result51 = mysql_query($sql51);
        //and delete search name from main table
		}
    $url = $href->getAttribute('href');
    $purl = substr($url, 30,300);

Open in new window

Do this:


for  ($i = 0; $i < $hrefs->length; $i++)
{
    $href = $hrefs->item($i);
    if ($href ->item== ''){
       $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";
            echo "Moved " . $row[name] . "from searchable database into broken search table.";
        //and delete search name from main table
    }else{
      $url = $href->getAttribute('href');
      $purl = substr($url, 30,300);
    }

Since you have omitted exit statement (because you need to process all records: I understand the error is mine), you have to create an alternative: if link is null move to broken table, else proceed to ususal operations.

Cheers
Sorry - that cannot be the entire script.

Parse error: syntax error, unexpected $end in /home/websitet/public_html/RAY_temp_twittoris.php on line 31

We need something we can cut-and-paste.  We need test data, so we need the ACTUAL URLs of your test data.  Practical stuff like that.  Thanks.
Sorry, only now I've seen an error in your code! You wrote:

    $href = $hrefs->item($i);
    if ($href ->item== ''){
       $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";
            echo "Moved " . $row[name] . "from searchable database into broken search table.";
        //and delete search name from main table
    }

Writing "if ($href ->item== '')" is absolutely wrong! $href has not items, $hrefs has items! You must write:

if ($hrefs->item== ''){

or

  if ($href== ''){

Please, foloow this suggestion. Sure I'm not so skilled as Ray is, but I think here you go.

For deleting purposes:

    $href = $hrefs->item($i);
    if ($href== ''){
       $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";
            echo "Moved " . $row[name] . "from searchable database into broken search table.";
        //and delete search name from main table
       $sql = "DELETE FROM maintable WHERE ENAME='$row[name]'";
    }


Cheers
Thanks MarqusG.

I changed the code a little bit based on your comments. It is now placing everything in the broken table.
I took your if statement and placed it outside the for statement to check for a value first.
However, it seems to treat '' as a wild card and not a blank space.
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a[starts-with(@href, 'CORPSEARCH.ENTITY_INFORMATION')]");
if ($hrefs->item == ''){
       $sql51="INSERT INTO broken (ename) VALUES('$row[name]')";
	   $result51 = mysql_query($sql51);
            echo "Moved " . $row[name] . "from searchable database into broken search table.";
        //and delete search name from main table
       //$sql52 = "DELETE FROM maintable WHERE ENAME='$row[name]'";
    } 

for  ($i = 0; $i < $hrefs->length; $i++) 
{
       $href = $hrefs->item($i);
    
      $url = $href->getAttribute('href');
      $purl = substr($url, 30,300);
	  
    
    echo '<p>Found:' . $row[name] . '<br />' . $purl. '<br />Retrieving Additional Information... ';
	}

Open in new window

I have to sign off on this now -- with no complete code example, it's kind of a fools errand.  If you want to post a new question with the complete code and the links to the test data, I will be glad to show you the solution.

Best of luck with your project, ~Ray
Thanks Ray.
The rest of the code doesn't matter because the page has been processed already and doesn't need testing,. But just to clarify for anyone else I have commented each line of code to try and explain what i am trying to accomplish. Thanks for any additional help i think the problem lies in :
______
//If there is no link then insert the name into the table named broken
if ($href ->item== ''){
       $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";
__________
// Put the page in a variable which only has 1 link
$html= curl_exec($ch);

//Create a DOMDocument
$dom = new DOMDocument();

// Parse the html into a DOMDocument
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);

//Filter Links to find only desired links
$hrefs = $xpath->evaluate("/html/body//a[starts-with(@href, 'CORPSEARCH.ENTITY_INFORMATION')]");

//For every match found create URL
for  ($i = 0; $i < $hrefs->length; $i++)
{
 //Place each link in a variable  
$href = $hrefs->item($i);

//If there is no link then insert the name into the table named broken
if ($href ->item== ''){
       $sql51="INSERT INTO broken(ename)VALUES('$row[name]')";

//Tell me the name has been removed
            echo "Moved " . $row[name] . "from searchable database into broken search table.";
//Get link        
        $url = $href->getAttribute('href');

//Take part of link you want      
$purl = substr($url, 30,300);
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial