473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help with script

133 100+
Hi,

I think the foreach statement is correct but not to sure how to fix:

[PHP]<?php

for($i=1;$i<3000;$i++) {

$data = file_get_contents('http://www.website.com?id='.$i);
$data = strip_tags($data,"<tr>");
$data = explode("<tr>",$data);

foreach($data as $index => $value) {

mysql_query("INSERT INTO `table` (entry, entry2, entry3, entry4, entry5) VALUES ('" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "')");

}

}

?>[/PHP]

Table structure that i am extracting data from is:

[HTML]<table width="385" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top"><h1>Company Name</h1>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<th width="30%"><strong>Contact:</strong></th>
<td width="70%"><strong>Persons Name
</strong> </td>
</tr>

<tr>
<th><strong>Tel:</strong></th>
<td><strong>000 000 000</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="small">Information Here</span>.</td>

</tr>

<tr>
<th><strong>E-mail:</strong></th>
<td><strong><a href="info@website.com">info@website.com</a></strong></td>
</tr>

<tr>
<th><strong>Web site:</strong></th>
<td><strong><a href="http://www.website.com" target="_blank" id="451" onClick="return trackclick(this.id);" title="Visit Site">www.website.com</a></strong></td>
</tr>

</table>[/HTML]

Any ideas?

Cheers,
Adam
Mar 10 '08 #1
17 1269
TheServant
1,168 Expert 1GB
Please can you post exactly what the problem is (errors, display, etc) so we know what to look for. We are not public syntax correctors!
Mar 11 '08 #2
Markus
6,050 Expert 4TB
Please can you post exactly what the problem is (errors, display, etc) so we know what to look for. We are not public syntax correctors!
Furthermore, show us what gets inserted into your db and what it is that you want inserting.

We are not public syntax correctors!
Mar 11 '08 #3
adamjblakey
133 100+
Hi,

Sorry i was not that clear.

What i have done first is:

[PHP]<?php
$file = "http://www.web.co.uk/page.asp?id=22";
$data = file_get_contents($file);
$data = strip_tags($data,"<tr>");
$data =explode("<tr",$data);
print_r($data);
?>[/PHP]

This is produces:

Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => name [1] => > [2] => > description [3] => > [4] => > [5] => > [6] => > [7] => >   [8] => > [9] => > -->   [10] => > [11] => > [12] => > Company Name [13] => > Contact: John [14] => > Tel:0000 [15] => >   Text [16] => > E-mail: gstarmobilediscos@yahoo.co.uk [17] => > Web site: www.site.com
Then i want to run the first script on what is produced but it does not return anything.

Cheers,
Adam
Mar 12 '08 #4
satas
82
What exactly you want script to return?

Single call of mysql_query won't return anything.
Mar 12 '08 #5
adamjblakey
133 100+
I want to return the '" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "' and put them into my table.


mysql_query("INSERT INTO `table` (entry, entry2, entry3, entry4, entry5) VALUES ('" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "')");
Mar 12 '08 #6
satas
82
I want to return the '" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "' and put them into my table.


mysql_query("INSERT INTO `table` (entry, entry2, entry3, entry4, entry5) VALUES ('" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "')");
So $data array is empty while you expect it to be with some data?
Try to turn on debugging by including in your script this lines:
[PHP]ini_set('display_errors',1);
error_reporting(E_ALL);[/PHP]
And check are there any errors.
Mar 12 '08 #7
Markus
6,050 Expert 4TB
You're logic on the foreach is flawed ;)

Simple to fix though!

You should no longer assume $data[] is an array - you have now (through the foreach) passed it into new variables ($value / $index) so use these variables to work with it:
[php]
foreach($data as $index => $value) {
/**
* THIS WONT WORK BECAUSE YOU ARE CHANGING $DATA!
* mysql_query("
* INSERT INTO
* `table`
* (entry, entry2, entry3, entry4, entry5)
* VALUES
* ('" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "')");
*/
echo "$index is $value<br />";
}
[/php]

Any questions?
Mar 12 '08 #8
adamjblakey
133 100+
Thanks for that but how do i go about only extracting certain fields as this displays all the data but i only need 13,14,15 and 16
Mar 12 '08 #9
Markus
6,050 Expert 4TB
Something like this?
[php]
for($_i = 13; $_i < 17; ++$_i)
{
echo $data[$_i] . "<br />";
}
[/php]
Mar 12 '08 #10
adamjblakey
133 100+
Hi,

I tried this:

[PHP]<?php

for($i=1;$i<10;$i++) {

$data = file_get_contents('http://www.website.com/details.asp?ID='.$i);
$data = strip_tags($data,"<tr>");
$data = explode("<tr>",$data);

foreach($data as $index => $value) {


for($_i = 13; $_i < 17; ++$_i) {

echo $data[$_i] . "<br />";

}

}

}

?>[/PHP]

But just returns the same value.. Am i doing something wrong?
Mar 12 '08 #11
satas
82
Let's have a look at this:
[PHP]<?php
// nine times we'll do the same things
for($i=1;$i<10;$i++) {

// get HTML-code into string
$data = file_get_contents('http://www.website.com/details.asp?ID='.$i);
$data = strip_tags($data,"<tr>");

// now string becomes an array
$data = explode("<tr>",$data);

// loop with as much iterations as items in array $data
foreach($data as $index => $value) {

// display items $data[14], $data[15], $data[16]
for($_i = 13; $_i < 17; ++$_i) {

echo $data[$_i] . "<br />";

}

}

}

?>[/PHP]
Say, $data looks like this :
[PHP]$data = array (
1 => 'first string',
...
16 => 'sixteenth string',
);[/PHP]

Finally, we'll see this in the browser for nine times:

forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
forthteenth string
fifteenth string
sixteenth string
Mar 12 '08 #12
adamjblakey
133 100+
There seems to be something wrong as it is not printing out 14, 15, 16 it is just printing 14 but the same data over and over about 50 times.
Mar 12 '08 #13
satas
82
Just remove foreach statement.
No idea why do you need it.
Mar 12 '08 #14
zorgi
431 Expert 256MB
How about instead of for loop having conditon:


[PHP]
foreach($data as $index => $value) {

// display items $data[14], $data[15], $data[16]

/* for($_i = 13; $_i < 17; ++$_i) {

echo $data[$_i] . "<br />";

} */

if (($index == 14) || ($index == 15) || ($index == 16)){
echo $value. "<br />";
}
}

[/PHP]
Mar 12 '08 #15
Markus
6,050 Expert 4TB
Yeh, you were supposed to remove the foreach statement as it serves no purpose.
[php]
<?php

$data = file_get_contents('http://thescripts.com');
$data = strip_tags($data,"<tr>");

// now string becomes an array
$data = explode("<tr>",$data);


for($_i = 13; $_i < 17; ++$_i)
{
echo $_i. " " . $data[$_i] . "<br />";
}



?>
[/php]
Mar 12 '08 #16
adamjblakey
133 100+
Hi,

Still does not work:

[PHP]

<?php

// nine times we'll do the same things
for($i=1;$i<10;$i++) {

$data = file_get_contents('http://www.website.co.uk/details.asp?OrgID='.$i);
$data = strip_tags($data,"<tr>");

// now string becomes an array
$data = explode("<tr>",$data);

for($_i = 14; $_i < 17; ++$_i)

{

echo $_i. " " . $data[$_i] . "<br />";

}
}

?>
[/PHP]

Any ideas?
Mar 12 '08 #17
Markus
6,050 Expert 4TB
It worked for me..

Do you get any output, and errors?
Mar 12 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
4
by: Derek | last post by:
I have the following script in a page and it gets an error in IE 6. Says something about an invalid argument but the line number doesn't help since I can't see the javascript code when viewing...
7
by: mike | last post by:
Hello, I am kind of new to this javascript stuff and I am constantly having problems trying to get my webpage validated. I have the following <script>printdate();</script> and when I validate it...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.