+ Reply to Thread
Results 1 to 10 of 10

Thread: Java scripting

  1. #1
    Circuitcode is offline x10Hosting Member Circuitcode is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    7

    Java scripting

    I have a 50 lists of business and their contact information. These lists are in .txt format and are used keep a database current when updated.

    Is it possible to create a script to enter the text files into a table. I need to display this information in a table 3 entries wide and i do not want to edit through over 1,700 entries it there is a code that could do it for me. The entries look like this.


    Name of business
    Street address
    city state zip
    Phone number

    There are 2 line breaks between each entry

    I know that I can use

    <?php require_once('File name'); ?>

    to enter the text file but is there an easier way to apply the editing.

  2. #2
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Java scripting

    I can't do Java yet, but I can certainly help you using PHP.
    PHP Code:
    <?php
    $file 
    'location/of_file.txt';
    // enter the location of the text file above.
    // if it's in the same directory as you put this file, just enter 'filename.txt'

    $array file($file);
    $business = array('name' => array(),'address' => array(), 'zip' => array(), 'phone' => array());

    $line 1;
    $key 0;
    while (
    count($array) > 0)
      {
      switch (
    $line)
        {
        case 
    1:
          
    $business[$key]['name'] = array_shift($array);
          break;
        case 
    2:
          
    $business[$key]['address'] = array_shift($array);
          break;
        case 
    3:
          
    $business[$key]['zip'] = array_shift($array);
          break;
        case 
    4:
          
    $business[$key]['phone'] = array_shift($array);
          break;
        case 
    5:
          break;
        case 
    6:
          
    $key++;
          break;
        }
      if (
    $line >= 6)
        
    $line 1;
      else
        
    $line++;
      }

    foreach (
    $business as $newarray)
      {
      
    // enter $newarray['name'] in your table as business name
      // enter $newarray['address'] in your table as business address
      // enter $newarray['zip'] in your table as business zip code
      // enter $newarray['phone'] in your table as business phone number
      
    }
    ?>
    The above code is untested but it should work. All you need to do is to connect to MySQL and then write your inserts on those last four lines, and it *should* work perfectly

    NOTE: make sure the first line of the text file is a business name. Make sure there are no more than two line breaks after the last phone number, and that the layout is exactly as you mentioned!
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  3. #3
    Circuitcode is offline x10Hosting Member Circuitcode is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    7

    Re: Java scripting

    I have been trying to get this to work but I am unsure what I am doing. I am not well versed in working with databases or PHP I usually hire someone to do it for me but I am sort on time if you could explain a little more I would highly appreciate it.

  4. #4
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Java scripting

    ok. I've updated it. I've designed it to be used once and then deleted. If you keep it and then accidentally run it again, it will possibly add all the records into your database again but more likely (and hopefully!) it will cock up and not adjust anything

    All you need to do is to edit 5 things, I've put them all at the top. First of all, the value for the variable $file. This should point to your text file. EG: 'http://mysite.com/files/businesses.txt'. Make sure you leave the single quotes in there!
    Next, replace 'your MySQL host address', 'your name', 'your password' and 'database name' with the relevant information. I cannot find these out for you. Contact your administrator/support staff if you are unsure.
    You may be able to use 'localhost' for the MySQL host address.

    EDIT: Oh btw, I'm in the UK so I have no idea of the format of zip codes. I assume they are 5 digits, nothing else? If letters can go in zip codes or they can have more than 5 digits, then it will fail to save it correctly!
    In addition, I was unsure of the phone number format. Because (+414)1111-111-111 (for example) is commonplace, it can take characters as well as numbers, up to a maximum of 20 characters.

    If you need any of the script explaining or anything so you can understand it better, don't hesitate to ask
    PHP Code:
    <?php
    $file 
    'location/of_file.txt';
    $database  =  array(
    'url'      => 'your MySQL host address',
    'name'     => 'your name',
    'pass'     => 'your password',
    'database' => 'database name');

    mysql_connect($database['url'],$database['name'],$database['pass']) or die('Error connecting to MySQL: '.mysql_error());
    echo 
    'Connected to MySQL<br>';

    mysql_select_db($database['database']) or die('Error connecting to MySQL database: '.mysql_error());
    echo 
    'Connected to Database<br>';

    $query "CREATE TABLE businesses(
      id INT NOT NULL AUTO_INCREMENT, 
      PRIMARY KEY(id),
      name VARCHAR(100) NOT NULL, 
      address VARCHAR(300) NOT NULL,
      zip INT(5) NOT NULL,
      phone VARCHAR(20) NOT NULL)"
    ;
    mysql_query($query) or die('Error creating table: '.mysql_error());  
    echo 
    'Table Created<br>';

    $array file($file);
    $business = array('name' => array(),'address' => array(), 'zip' => array(), 'phone' => array());

    $line 1;
    $key 0;
    while (
    count($array) > 0)
      {
      switch (
    $line)
        {
        case 
    1:
          
    $business[$key]['name'] = array_shift($array);
          break;
        case 
    2:
          
    $business[$key]['address'] = array_shift($array);
          break;
        case 
    3:
          
    $business[$key]['zip'] = array_shift($array);
          break;
        case 
    4:
          
    $business[$key]['phone'] = array_shift($array);
          break;
        case 
    5:
          break;
        case 
    6:
          
    $key++;
          break;
        }
      if (
    $line >= 6)
        
    $line 1;
      else
        
    $line++;
      }

    foreach (
    $business as $newarray)
      {
      
    $query "INSERT INTO businesses
        (name,address,zip,phone) VALUES('
    {$newarray['name']}','{$newarray['address']}','{$newarray['zip']}','{$newarray['phone']}')";
      
    mysql_query($query) or die('Error adding row: '.mysql_error());  
      echo 
    'Row data added<br>';
      }

    echo 
    "Succesfully added all entries into the database. Be sure to delete both $file and this script to prevent duplicated entries in your database!";
    ?>
    You will now have all the data stored in your database. You can use other scripts or utilities such as phpMyAdmin to access the data.
    Last edited by Scoochi2; 09-16-2008 at 05:45 PM. Reason: zip code confusion!
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  5. #5
    Circuitcode is offline x10Hosting Member Circuitcode is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    7

    Re: Java scripting

    Ok this is why I was having problems understanding the coding. This code inserts the entries from the text file to the database. I have a code in java that does this for me already and it is easier to use.

    I dont think I was very clear on what I am trying to do.

    The 50 lists are one for each state. When a customer clicks on the state of their choice I want it to come up with a page that shows all the businesses that are listed in the corresponding txt file. I am simply wondering if it is possible to import txt file into the page as I asked above.

  6. #6
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: Java scripting

    wait. is we talking abouts java. or javascript? lol
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  7. #7
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: Java scripting

    I know nothing about Java, although it could be done using JavaScript if the text documents are all supplied to the script.
    So my guess is that any programming language can achieve this
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  8. #8
    Circuitcode is offline x10Hosting Member Circuitcode is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    7

    Re: Java scripting

    javascript or whatever coding language would work best
    Edit:
    I was just thinking would it be easier to query the database for all listings with the 2 letter abbreviation for the called state. I just have no idea how to do so.

    If anyone can help with doing so I am sure it is fairly easy. If there is someway that I can just change the abbreviation on each page for the specified state.

    Please use Wisconsin (WI) as the example if possible. I highly appreciate all the help.
    Last edited by Circuitcode; 09-16-2008 at 09:31 PM. Reason: Automerged Doublepost

  9. #9
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: Java scripting

    give us an outline of you sql database with this info, then maybe we can help.
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  10. #10
    Circuitcode is offline x10Hosting Member Circuitcode is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    7

    Re: Java scripting

    I have decided to go about this a little different and I am now pulling all the information from my database Everything is working and I think you for trying to help

    You can close this thread

+ Reply to Thread

Similar Threads

  1. Java
    By Rhianna in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 03-07-2009, 06:22 PM
  2. Anfy Java Applet Online problems
    By Tharu in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 08-02-2008, 09:05 PM
  3. creating a starfield with a java
    By intertec in forum Tutorials
    Replies: 0
    Last Post: 02-08-2008, 01:08 AM
  4. Java Tutorial for Beginners.
    By satheesh in forum Tutorials
    Replies: 0
    Last Post: 10-27-2007, 06:40 PM
  5. Java?
    By pensoftware in forum Free Hosting
    Replies: 2
    Last Post: 10-15-2005, 01:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers