1

What I want to do is store the value that is not empty into the database, my database connection is correct, the problem here is that when I store into the database only the Id increments and that is because the Id is set on auto-increment.

this is the code that I have:

include("connect.php");
mysql_select_db("scrapper",$conec);

for($i = $0; $i < $3; $i++){

    foreach($html->find('span.lists',$i) as $e){
        if(!empty($e->plaintext)){
            $list[$i] = $e->plaintext;
            echo $list[$i];
        }        
    }
}

inside the foreach there are multiple blank strings "" and those are what im trying to avoid thats why I only want to store the plaintext when empty is negated, but I still keep getting nothing inside the database only two periods which are inside the '".$list[$i]."'. Also the echo above does work echo $list[$i]; It prints out what I want it to store in the database.

for($i = $0; $i < $3; $i++){

      $res=mysql_query("insert into data (list) values('".$list[$i]."')");


}

here is more code:

$url = "http://www.seccionamarilla.com.mx/resultados/hospitales/distrito-federal/"; $html = file_get_html($url);

for($i = $0; $i < $5; $i++){

foreach($html->find('span.listado_destacado',$i) as $e){
  if(!empty($e->plaintext)){
    $list = $e->plaintext;
    echo $list;
  }
}

foreach($html->find('span.street-address',$i) as $e){
  if(!empty($e->plaintext)){
    $addr = $e->plaintext;
    echo $addr;
  }
}

echo '<br>';

}//end for

for($i = $0; $i < $5; $i++){
  $res=mysql_query("insert into data (list,addr) values('{$list}','{$addr}')");


}

This Is what I get after I run the program:

ANESTESIA Y CONTROL DEL DOLOR MEDICOS ASOCIADOS SC RIO CHURUBUSCO 601 421, XOCO, BENITO JUAREZ, C.P 03330, DF 
CENTRO HOSPITALARIO SANATORIO DURANGO DURANGO 296, ROMA, CUAUHTEMOC, C.P 06700, DF 
CLISEM NICOLAS SAN JUAN 351, DEL VALLE CENTRO, BENITO JUAREZ, C.P 03100, DF 
HOSPITAL MEDICA SAN LUIS SAN LUIS POTOSI 122, ROMA NORTE, CUAUHTEMOC, C.P 06700, DF 
MEDICA PALMAS LAS PALMAS 10, GRANJAS CABRERA, TLAHUAC, C.P 13230, DF 
1
  • 1
    Clarify your question and give more code information: your HTML for example. What you trying to save? What type of information? Etc. And you can the sample value of $e->plaintext plesae? Commented Mar 20, 2014 at 16:24

3 Answers 3

1

Try this(remove foreach loops):

$e = $html->find('span.listado_destacado',$i);
  if(!empty($e->plaintext)){
    $list[$i] = $e->plaintext;
    echo $list[$i];
  }


$e = $html->find('span.street-address',$i);
  if(!empty($e->plaintext)){
    $addr[$i] = $e->plaintext;
    echo $addr[$i];
  }


echo '<br>';

}//end for

for($i = $0; $i < $5; $i++){
  $res=mysql_query("insert into data (list,addr) values('{$list[$i]}','{$addr[$i]}')");
}

http://simplehtmldom.sourceforge.net/manual.htm

Sign up to request clarification or add additional context in comments.

5 Comments

Fatal error: Can't use function return value in write context
In yout las code I think that "$list = $e->plaintext;" doesn't has index "$list[$i] = $e->plaintext;" same for addr
the word "Array" gets stored in the database when I do put an index
In the last loop(for) you need put index too
after I do that I get "..." stored in the database.
0

Try this.

for($i = $0; $i < $3; $i++){    
    $data = $list[$i];
    $res=mysql_query("insert into data (list) values('{$data}')");
}

In response to your comment: Maybe I misunderstood. Are the two code snippets above separate for loops in your program? Or are you trying to merge them into one?

You could do what your asking simply by replacing your echo statement in the first code block with the insert query.

Im also unsure as to why youre storing your data into an array like that if youre just inserting it into a database.

Example:

include("connect.php");
mysql_select_db("scrapper",$conec);

for($i = $0; $i < $3; $i++){

    foreach($html->find('span.lists',$i) as $e){
        if(!empty($e->plaintext)){
            $data = $e->plaintext;
            $res=mysql_query("insert into data (list) values('{$data}')");
        }        
    }
}

3 Comments

Responded to your comment above
there are actualy more than two foreach, that is why the insertion of the database is outside the foreach loop, so I can store both of them.
I only see one foreach above, if there is more code it needs to be posted so we can help diagnose the issue. Is there more code between the two snippets posted above? If you need to insert exactly what is being echoed, as you said above, why not put the insert statement right after the echo using the exact same data. I don't see a reason to do it in a separate loop if you really do need exactly what is being echoed.
0

CS2032 C PASS CS2041 D PASS CS2401 C PASS CS2402 C PASS CS2403 E PASS CS2405 S PASS CS2406 S PASS MG2452 B PASS i have plaintext got from inside foreach i need to get set of three values like

CS2032 C PASS CS2041 D PASS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.