Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Insert Array Output into Database


Hello Friends now a days i am working on php.A few days ago i got a task from my trainer and task was to uploading a txt file in new folder and output should be in an array format using loops and after that insert that output into database and the content in the text file was 100 email id's.At that time i was new in php and no idea how to start my task and how to use loops.For that i searched on net for many times but i couldn't find the required data to complete task on single site.
So,i had to serach many sites to collect or get idea how to complete my task.Now here in this post i am going to share that program with you so that if you get similar task like this task you can get help from this,i hope it will be helpful to you.

Here the program is:
1) Using this code you can upload your file from one folder to another folder.


<?php
if (($_FILES["file"]["type"] == 'text/plain') && ($_FILES["file"]["size"] < 20000))

  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";


    if (file_exists("nit/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. "."<br/>"."<br/>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "nit/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "nit/" . $_FILES["file"]["name"]."<br />";
 
      }
    }
  } 
  ?>


2) Use this code after 1st code as shown above, By using this code you will get your output in array format and after that output will be insert into database, see this code how its works,



<?php
        $file = fopen("file.txt", "r");
if ($file) {
  while (!feof($file))
  {
  $aa=fgets($file);
  $explode= explode(",",$aa);
  print_r(explode(',',$aa));
  foreach ($explode as $id =>$email_id)
 {
   $sql="insert into aa (id,email_id) values ('$id','$email_id')";
   $result=mysql_query($sql);
  }
 }
 
fclose($file);
      }
?>
</body>
</html>



Note:- In this above code we are using while loop as well as explode function after that we are using insert statement to insert output into database.

I hope you will find it informative.

In which situation Arrays and ArrarList should be used?


Arrays are the most commonly used data structure to store a collection of elements. Most programming languages provide methods to easily declare arrays and access elements in the arrays. An arraylist can be seen as a dynamic array, which can grow in size. Due to this reason, the programmer does not need to know the size of the arraylist when she is defining it.

What are Arrays?

Shown in figure 1, is a piece of code typically used to declare and assign values to an array. Figure 2 depicts how an array would look like in the memory.
int values[5];

values[0]=100;

values[1]=101;

values[2]=102;

values[3]=103;

values[4]=104;



Figure 1: Code for declaring and assigning values to an array

100 101 102 103 104
Index: A 0                             1                              2                              3                              4

Figure 2: Array stored in the memory

Above code, defines an array that can store 5 integers and they are accessed using indices 0 to 4. One important property of an array is that, entire array is allocated as a single block of memory and each element gets its own space in the array. Once an array is defined, its size is fixed. So if you are not sure about the size of the array at compile time, you would have to define a large enough array to be in the safe side. But, most of the times, we are actually going to use less number of elements than we have allocated. So a considerable amount of memory is actually wasted. On the other hand if the 'large enough array' is not actually large enough, the program would crash.

What are Arraylists?

An arraylist can be seen as a dynamic array, which can grow in size. Therefore arraylists are ideal to be used in situation in which you don't know the size of the elements required at the time of declaration. In Java, arraylists can only hold objects, they cannot hold primitive types directly (you can put the primitive types inside an object or use the wrapper classes of the primitive types). Generally arraylists are provided with methods to perform insertion, deletion and searching. Time complexity of accessing an element is o(1), while insertion and deletion has a time complexity of o(n). In Java, arraylists can be traversed using foreach loops, iterators or simply using the indexes.

What is the difference between Arrays and Arraylists

Even though the arrays and arraylists are similar in the sense that both of them are used to store collections of elements, they differ in how they are defined. The size of the array has to be given when an array is defined, but you can define an arraylist without knowing the actual size. You can add elements to an arraylist after it is defined and this is not possible with arrays. But in Java, arraylists cannot hold primitive types, but arrays can be used to hold primitive types. But if you need a data structure that can vary its size, arraylist would be the best choice.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More