| | |
the value of foreach loop keeps increasing
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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:
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.
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:
PHP Syntax (Toggle Plain Text)
$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.
Last edited by khr2003; Jan 14th, 2009 at 10:47 am.
Try using an array:
This will put each result into the array.
php Syntax (Toggle Plain Text)
foreach( $raw as $key ){ $final[] = $key; }
This will put each result into the array.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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:
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.
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:
PHP Syntax (Toggle Plain Text)
$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:
You could also put the database query within the foreach so that it does it for each one.
php Syntax (Toggle Plain Text)
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.
Last edited by Will Gresham; Jan 14th, 2009 at 11:43 am.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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:
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.
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 Syntax (Toggle Plain Text)
<?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.
Last edited by khr2003; Jan 14th, 2009 at 7:34 pm.
•
•
Join Date: Jan 2009
Posts: 34
Reputation:
Solved Threads: 3
Tell me wether this gets you theoutput you need
PHP Syntax (Toggle Plain Text)
//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);
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
•
•
•
•
Tell me wether this gets you theoutput you need
PHP Syntax (Toggle Plain Text)
//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);
Thank you very very much
![]() |
Other Threads in the PHP Forum
- Previous Thread: trouble with logic
- Next Thread: php pop-up link pls help
Views: 857 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cookies cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube





