Thursday, 3 May 2018

File upload in a folder and save/write attributes in XML file in PHP.

When we develop a web application without using a database, so it becomes very difficult to save any attributes which can be used later for show any item on page. XML could be better option to save data and read data where database is not available. We can write values in XML file and retrieve from that file.

XML database is document oriented database and fall in NoSql category of database. We don't go into concept of this database. Just look a example of saving data in XML database using PHP as well as how we can upload file in a folder and save its some attribute value in XML file/database.

Let see by an example.

First create a form that can upload a file with some its value.

<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
    <input type="hidden" name="create_xml" value="true">
    <label for="name">Article Title: </label>&nbsp;&nbsp <input type="text" name="name"><br><br>
    <label for="textbox">Write something: </label>&nbsp;&nbsp <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
    <label for="fileToUpload">Upload a Article(PDF-file:) </label>&nbsp;&nbsp 
    <input type="file" name="file" id="file-select"><br><br>
    <label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">&nbsp;&nbsp <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>


Now write PHP code to receive the form data and upload file to folder. (I assume that you are aware of sending and receiving values from form.)

Create a folder "upload" in your project root directory.

To upload folder -

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
        $targetfolder = "upload/";

 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;

if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))

 {
                echo "<script>";
  echo "alert('Your file is uploaded succesfully');";
  echo " window.location.href='profile.php';";
  echo "</script>";
         echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
  }
 else {
  echo "<script>";
  echo "alert('Error !!!!! Please try again');";
  echo " window.location.href='profile.php';";
  echo "</script>";
  }

} 

Now PHP code which create XML file to write data in append mode.

$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);

$doc = new DOMDocument();
$doc->formatOutput = true;

$r = $doc->createElement( "books" );
$doc->appendChild( $r );

foreach( $books as $book )
{
$b = $doc->createElement( "book" );

$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );

$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );

$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );

$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );

$r->appendChild( $b );
}

 $handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);

Note that this code has a demerit that it always add XML start declaration tag, when we repeatedly write the data on same file. So what is the solution to parse or read this file? Solution is to read this file using regular expression. Read this post. 

Now complete working code -

<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
<br>
    <input type="hidden" name="create_xml" value="true">

    <label for="name">Article Title: </label>&nbsp;&nbsp <input type="text" name="name"><br><br>
    <label for="textbox">Write something: </label>&nbsp;&nbsp <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
    <label for="fileToUpload">Upload a Article(PDF-file:) </label>&nbsp;&nbsp 
    <input type="file" name="file" id="file-select"><br><br>
    <label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">&nbsp;&nbsp <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>


<?php

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
 $targetfolder = "upload/";
 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
 {
$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );

$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );
$r->appendChild( $b );
}
 $handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);

                echo "<script>";
  echo "alert('Your file is uploaded successfully');";
  echo " window.location.href='profile.php';";
  echo "</script>";
        echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
  }
 else {
          echo "<script>";
  echo "alert('Error !!!!! Please try again');";
  echo " window.location.href='profile.php';";
  echo "</script>";
  }

} 

?>

XML file -



Thursday, 5 April 2018

>Please fix the version conflict either by updating the version of the google-services plugins (information about the latest version is available at http://bintray.com/android/android-tools//com.google.gms.google-services/) or updating the version of com.google.android.gms to 12.0.1



This problem occurs when google play services  and its dependencies have different versions, however google play services require its all dependencies of same version.

So, to fix this -

Open build.gradle (Module: app) and go into dependencies section and check all dependencies that starts like com.google.xxx:zyx-zbc:12.0.x;. Now change its version to the version of google play service (generally it is last one).



Now sync the gradle.

Monday, 2 April 2018

How to generate Google Map API key for any Android Project ?

Google map API key is necessary to add Google map to your any application. So, why we require this key ?

This API key is required because you have to access the Google map server. You can restrict this key only for Android app. This is free and any numbers of user can use it.

Let see how you can generate Google map API key for your Android project.

Start a new project in Android Studio and select Google Map Activity. When project open in Android Studio it open on google_maps_api.xml file by default.


You can see the instruction to generate the API key as well as the link (Third line of comment) to generate that API key. Just copy this link and paste it on any browser.


It will ask your gmail username and password, just put your email and password to login into your gmail account.


Now register your project for Google Map API in Google API Console by clicking Continue.


 It will take some time to enable API, just wait for it.


After API is enabled, click on Create API Key .


It will also take some time to create API key, just wait for it.


Now your API key is ready to use just copy it and use it in your app.



You should paste this API key in your file google_maps_api.xml  of your Android app.



Error :- Google Map fragment/activity is blanked with Google logo and update google play services in Android Studio.

When we develop Google Map fragment in Android Studio, it shows only blank map with Google logo. We stuck here, why it is so ? However we have done everything fine.


 Before this we also find that we don't have updated Google Play Services as message shown by emulator like -


So, how this can be resolved ?

Let see, how these Google Map related problems can be resolved ?

First See how to update Google Play Services.

Go to -

Tools ----> Android ------> SDK Manager 

A dialogue window will appear. Click on SDK Tools  tab. Find the Google Play Services. You will see that it is unchecked and status is Not installed.


Check Google Pay Services then click Apply. A confirmation box will appear Click on Ok.
Download will be started if you are connected to the Internet. After download done click on Finish.









Now your Google Play Services is updated. And you can run your map fragment now.

After updating Google Play Services, if you again can not see the map or you only see the blank map with Google logo like screen shot given above then-

We know that for developing map activity in Android Studio, we require Google Map API key generated form google API console. If you using key that is not generated from developer machine, it show blank map. So make sure that you should generate API key from the machine from where you are developing the map activity.

How to generate Google Map API key ?

Monday, 19 March 2018

Androis Studio errors and solutions


Here are some list of Android Studio errors, I faced when trying to use Android Studio. This is not limited only to the listed below, it will be growing, when I get new one.
  1. Emulator: [1116]:ERROR:android/android-emu/android/qt/qt_setup.cpp:28:Qt library not found at C:\Users\user1\AppData\Local\Android\Sdk\emulator\lib64\qt\lib
  2. > Manifest merger failed: uses-sdkminSdkVersion 9 cannot be smaller than version 14 declared in library [com.android.support.apcompat-v7:26.0.0-alpha1]  Android Studio
  3. Error Loading Project: Cannot load 3 facets Details... Android Studio.
  4. Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0.
  5. Google Map fragment is blanked with Google logo and update google play services. 
  6. >Please fix the version conflict either by updating the version of the google-services plugins (information about the latest version is available at http://bintray.com/android/android-tools//com.google.gms.google-services/) or updating the version of com.google.android.gms to 12.0.1
  7. Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ. 
  8. Emulator: emulator: ERROR: x86 emulation currently require hardware acceleration!
  9. Error: Cannot fit requested classes in a single dex file (# methods: 87573 > 65536)
  10. KVM is required to run this AVD. /dev/kvm/ device:permission denied. Grant current user  access to /dvm/kvm

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0.

This kind of error is Gradle related error. If there is a problem in Gradle or it is working in offline mode, you get error like -


To resolve this kind of error either update your Gradle or put your Gradle in online mode.

To do so, go to -

File ----> Settings ------> Build,Execution, Deployment ------> Gradle ------> and uncheck the Offline mode

Note :- make sure you are connected to internet, because it is updated from there.


Then click Apply and Ok and wait till Gradle is being updated.



Monday, 5 March 2018

Emulator: [1116]:ERROR:android/android-emu/android/qt/qt_setup.cpp:28:Qt library not found at C:\Users\user1\AppData\Local\Android\Sdk\emulator\lib64\qt\lib

As a beginner of using Android Studio, it is very hard to rectify the Android Studio error. As I also a beginner of Android Studio and what I face during my experience, I just share it through my blog. Here I am sharing my experience regarding emulator error. See the error in screen shot -



So, why this problem occurs ? Answer is that, your SDK does not have emulator or partially installed.
To install emulator go to -

Tools -----> SDK Manager -------> Android SDK

 and tick the Android Emulator then Apply and Ok

Note :- make sure that you are connected to Internet.


Confirm it by clicking OK.


Then it will download the emulator from Google Android.



Click Finish.

Restart Android Studio.





Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...