Help with script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    #1

    Help with script

    Hi,

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

    [PHP]<?php

    for($i=1;$i<300 0;$i++) {

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

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

    mysql_query("IN SERT 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"><h 1>Company Name</h1>
    <table width="100%" border="0" cellspacing="0" cellpadding="3" >
    <tr>
    <th width="30%"><st rong>Contact:</strong></th>
    <td width="70%"><st rong>Persons Name
    </strong> </td>
    </tr>

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

    </tr>

    <tr>
    <th><strong>E-mail:</strong></th>
    <td><strong>< a href="info@webs ite.com">info@w ebsite.com</a></strong></td>
    </tr>

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

    </table>[/HTML]

    Any ideas?

    Cheers,
    Adam
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    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!

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by TheServant
      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!

      Comment

      • adamjblakey
        New Member
        • Jan 2008
        • 133

        #4
        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_conten ts($file);
        $data = strip_tags($dat a,"<tr>");
        $data =explode("<tr", $data);
        print_r($data);
        ?>[/PHP]

        This is produces:

        Code:
        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

        Comment

        • satas
          New Member
          • Nov 2007
          • 82

          #5
          What exactly you want script to return?

          Single call of mysql_query won't return anything.

          Comment

          • adamjblakey
            New Member
            • Jan 2008
            • 133

            #6
            I want to return the '" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "' and put them into my table.


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

            Comment

            • satas
              New Member
              • Nov 2007
              • 82

              #7
              Originally posted by adamjblakey
              I want to return the '" . $data[12] . "', '" . $data[13] . "', '" . $data[14] . "', '" . $data[15] . "', '" . $data[16] . "' and put them into my table.


              mysql_query("IN SERT 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('displa y_errors',1);
              error_reporting (E_ALL);[/PHP]
              And check are there any errors.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                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?

                Comment

                • adamjblakey
                  New Member
                  • Jan 2008
                  • 133

                  #9
                  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

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Something like this?
                    [php]
                    for($_i = 13; $_i < 17; ++$_i)
                    {
                    echo $data[$_i] . "<br />";
                    }
                    [/php]

                    Comment

                    • adamjblakey
                      New Member
                      • Jan 2008
                      • 133

                      #11
                      Hi,

                      I tried this:

                      [PHP]<?php

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

                      $data = file_get_conten ts('http://www.website.com/details.asp?ID= '.$i);
                      $data = strip_tags($dat a,"<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?

                      Comment

                      • satas
                        New Member
                        • Nov 2007
                        • 82

                        #12
                        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_conten ts('http://www.website.com/details.asp?ID= '.$i);
                        $data = strip_tags($dat a,"<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

                        Comment

                        • adamjblakey
                          New Member
                          • Jan 2008
                          • 133

                          #13
                          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.

                          Comment

                          • satas
                            New Member
                            • Nov 2007
                            • 82

                            #14
                            Just remove foreach statement.
                            No idea why do you need it.

                            Comment

                            • zorgi
                              Recognized Expert Contributor
                              • Mar 2008
                              • 431

                              #15
                              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]

                              Comment

                              Working...