hi
I am making a script to extract data from a table from a website using explode function and then insert the data into the database.

I have stuck with foreach loop, this is how it looks:

$table_data = explode('<table cellspacing="1" cellpadding=1" width="90%" border="0" bgcolor="#EFEEEE">', $data);
	$end_table= explode("</table>", $table_data[1]);

	$raw = explode('</tr><tr bgcolor=ffffff>
<td align="center" bgcolor=ffffff>', $end_table[0]);
foreach( $raw as $key ){

		 $final = $key;
}

when I try to print the value of $final outside the loop I get only the last raw. and when I add a dot (.) before the equal sign of the $final variable like this ($final .= $key) the data keeps increasing. How can I get the value of the loop outside it in order to insert in the database, or how can I get not to increase every time it loops.

Try using an array:

foreach( $raw as $key ){
  $final[] = $key;
}

This will put each result into the array.

thanks for the reply

I wonder how to do i print the value. is it echo $final[]; which did not work for me.

let me explain my problem with another example:

$a = array(1, 2, 3, 17);
foreach ($a as $v) {
 $aa = $v;
 echo "Current value is $aa <br>";
}

this code as is will give me the result:
Current value of 1
Current value of 2
Current value of 3
Current value of 17

but when I try to use the variable $aa to insert into a database i get only 17 (the last line).

when change the code to $aa .=$v; I get:
Current value of 123171
Current value of 1231712
Current value of 12317123
Current value of 1231712317

what I want the code to display is:
Current value of 1
Current value of 2
Current value of 3
Current value of 17

all in one variable that I can use to insert in a database.

To echo the values of the array you would need another foreach:

foreach ($final as $var) {
  echo "HTML code or other items " . $var;
}

You could also put the database query within the foreach so that it does it for each one.

thanks for the reply

I tried what you said but the data kept increasing as before, i want:
(1,
2,
3,
17)
and not
(1,
12,
123,
12317)

maybe I should put in the whole context:

<?php

$startc = "1";
$endc = "10";

for ($i=$startc; $i<=$endc; $i++)
{
	$url ="http://www.someurl.net?id=$i";

	$data = file_get_contents($url);
	$title_start= explode('<font class="data" color=cc0000>
', $data);
	$title = explode('</font>
</center>', $title_start[1]);
$final_title = str_replace("<br>", "", $title[0]);
	echo $final_title."<br>";
	
	$table_data = explode('<table cellspacing="1" cellpadding=1" width="90%" border="0" bgcolor="#EFEEEE">', $data);
	$end_table= explode("</table>", $table_data[1]);

	$raw = explode('</tr><tr bgcolor=ffffff>
<td align="center" bgcolor=ffffff> ', $end_table[0]);

	foreach( $raw as $key ){
		 $final = $key;
		 echo $final;
} 
 $result    = query("insert into database
(url,raw)
values( '$url','$final')");
	}
?>

As you can see I am trying to extract some data from number of web pages, and put that into a for loop, inside it I extract the title of each page. Then I want to extract the table data so I put in a loop so each raw of the table is extracted. Then I want the result of the this loop to be inserted in the database.

I hoped that I am making my self clear.

Tell me wether this gets you theoutput you need

//this will independently echo every raw input.
foreach($raw as $val){
	echo $val;
}

//you can just implode your raw data to a final variable.
$final = implode(",",$raw);

Tell me wether this gets you theoutput you need

//this will independently echo every raw input.
foreach($raw as $val){
	echo $val;
}

//you can just implode your raw data to a final variable.
$final = implode(",",$raw);

worked like a charm!
Thank you very very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.