Java scripting

Circuitcode

New Member
Messages
7
Reaction score
0
Points
0
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.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
I can't do Java yet, but I can certainly help you using PHP.
PHP:
<?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!
 

Circuitcode

New Member
Messages
7
Reaction score
0
Points
0
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.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
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:
<?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:

Circuitcode

New Member
Messages
7
Reaction score
0
Points
0
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.
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
wait. is we talking abouts java. or javascript? lol
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
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 :)
 

Circuitcode

New Member
Messages
7
Reaction score
0
Points
0
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:

mattura

Member
Messages
570
Reaction score
2
Points
18
give us an outline of you sql database with this info, then maybe we can help.
 

Circuitcode

New Member
Messages
7
Reaction score
0
Points
0
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
 
Top