the value of foreach loop keeps increasing

Thread Solved

Join Date: Dec 2008
Posts: 57
Reputation: khr2003 is an unknown quantity at this point 
Solved Threads: 0
khr2003 khr2003 is offline Offline
Junior Poster in Training

the value of foreach loop keeps increasing

 
0
  #1
Jan 14th, 2009
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:
  1. $table_data = explode('<table cellspacing="1" cellpadding=1" width="90%" border="0" bgcolor="#EFEEEE">', $data);
  2. $end_table= explode("</table>", $table_data[1]);
  3.  
  4. $raw = explode('</tr><tr bgcolor=ffffff>
  5. <td align="center" bgcolor=ffffff>', $end_table[0]);
  6. foreach( $raw as $key ){
  7.  
  8. $final = $key;
  9. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: the value of foreach loop keeps increasing

 
0
  #2
Jan 14th, 2009
Try using an array:
  1. foreach( $raw as $key ){
  2. $final[] = $key;
  3. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: khr2003 is an unknown quantity at this point 
Solved Threads: 0
khr2003 khr2003 is offline Offline
Junior Poster in Training

Re: the value of foreach loop keeps increasing

 
0
  #3
Jan 14th, 2009
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:
  1. $a = array(1, 2, 3, 17);
  2. foreach ($a as $v) {
  3. $aa = $v;
  4. echo "Current value is $aa <br>";
  5. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: the value of foreach loop keeps increasing

 
0
  #4
Jan 14th, 2009
To echo the values of the array you would need another foreach:
  1. foreach ($final as $var) {
  2. echo "HTML code or other items " . $var;
  3. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: khr2003 is an unknown quantity at this point 
Solved Threads: 0
khr2003 khr2003 is offline Offline
Junior Poster in Training

Re: the value of foreach loop keeps increasing

 
0
  #5
Jan 14th, 2009
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:
  1. <?php
  2.  
  3. $startc = "1";
  4. $endc = "10";
  5.  
  6. for ($i=$startc; $i<=$endc; $i++)
  7. {
  8. $url ="http://www.someurl.net?id=$i";
  9.  
  10. $data = file_get_contents($url);
  11. $title_start= explode('<font class="data" color=cc0000>
  12. ', $data);
  13. $title = explode('</font>
  14. </center>', $title_start[1]);
  15. $final_title = str_replace("<br>", "", $title[0]);
  16. echo $final_title."<br>";
  17.  
  18. $table_data = explode('<table cellspacing="1" cellpadding=1" width="90%" border="0" bgcolor="#EFEEEE">', $data);
  19. $end_table= explode("</table>", $table_data[1]);
  20.  
  21. $raw = explode('</tr><tr bgcolor=ffffff>
  22. <td align="center" bgcolor=ffffff> ', $end_table[0]);
  23.  
  24. foreach( $raw as $key ){
  25. $final = $key;
  26. echo $final;
  27. }
  28. $result = query("insert into database
  29. (url,raw)
  30. values( '$url','$final')");
  31. }
  32. ?>

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 34
Reputation: jrdark13 is an unknown quantity at this point 
Solved Threads: 3
jrdark13 jrdark13 is offline Offline
Light Poster

Re: the value of foreach loop keeps increasing

 
0
  #6
Jan 14th, 2009
Tell me wether this gets you theoutput you need
  1. //this will independently echo every raw input.
  2. foreach($raw as $val){
  3. echo $val;
  4. }
  5.  
  6. //you can just implode your raw data to a final variable.
  7. $final = implode(",",$raw);
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: khr2003 is an unknown quantity at this point 
Solved Threads: 0
khr2003 khr2003 is offline Offline
Junior Poster in Training

Re: the value of foreach loop keeps increasing

 
0
  #7
Jan 15th, 2009
Originally Posted by jrdark13 View Post
Tell me wether this gets you theoutput you need
  1. //this will independently echo every raw input.
  2. foreach($raw as $val){
  3. echo $val;
  4. }
  5.  
  6. //you can just implode your raw data to a final variable.
  7. $final = implode(",",$raw);
worked like a charm!
Thank you very very much
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 857 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC