Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Monday, 29 February 2016

How to Create an Array in PHPIn Feburary 2016 29,

In Feburary 2016 29,
Imagine that you have to write a movie catalog. One of the variables that you will use in your program is the movie title. But if you have thousands of movies, using a separate variable to store each title is not ideal. Instead, you should use one variable (the title) which has many values ('One Flew Over the Cuckoo's Nest,' 'The Graduate' and so on). Such data is an ideal candidate for an array.
Check to see if you already have a list of values, so that you can create the array with the array function instead of populating it manually.
Create the Array
Declare the array and assign values:
$titles = array('Hair', 'The Office', 'Troy', 'Tarzan', 'American Pie', 'Adam and Eve', 'Mystery', 'E.T.', 'Star Wars');
Enter as many movie titles as you have. If your values are strings, as in the above example, don't forget the quotes around them. If your values are integers, you can forgo the quotes.
Appreciate that this array is created with numeric indexing. In the above example, the array has nine elements (movie titles) and the indexes are from 0 ('Hair') to 8 ('Star Wars'). However, you can also create associative arrays.
Create an associative array. An associative array uses textual keys instead of numbers, and the indexes are more descriptive. This is especially useful when the values are not strings. The general syntax is the following:
$salary['John Smith'] = 3000;
This will assign the value 3000 to the array element, which has the 'John Smith' index.
Use the array function to create the array.
$salary = array('John Smith' => 3000, 'Sally Jones' => 4000, 'Chris Steward' => 4900, 'Mary Roberts' => 6500, 'Sam Moses' => 5400, 'Alice Roberts' => 4200);
Notice the slight difference in the syntax: You use the => symbol to enter the value for the key.
Perform Simple Operations With the Array
Reference values from the array by their index. For instance, if you want to display the title 'Adam and Eve,' you would do the following:
echo $titles[5];
because 'Adam and Eve' is the sixth element in the array and its index is 5.
Assign values to array elements. If you want to set a new value for an array element, use the following:
$titles[6] = 'Midnight Express';
This will replace the 'Mystery' value with 'Midnight Express'.
In Feburary 2016 29,

Sunday, 28 February 2016

How to Change HTML Text With JavaScript VariablesIn Feburary 2016 28,

In Feburary 2016 28,
Open a text editor and create a new file named 'changeTextVars.html.' Type six HTML tags in the file:
Save 'changeTextVars.html.'
Place a JavaScript open script tag -- '' tag:
Add a JavaScript function between the '' tags named 'changeText().' The 'changeText()' function takes a variable named 'monsterName' as an argument:
Edit the 'changeText' function. Use the 'document.getElementById' function to change the 'innerHTML' property for the field named 'textToChange.' The 'textToChange' field holds the text updated with the variable 'monsterName':
Add a '' tag between the HTML '
' and '
' tags. Enter some text that displays the message -- such as 'My favorite monster is: ' -- and close the '' tag. Be sure to include a space after the colon and before the '' tag to separate the message from the dynamic text:
My favorite monster is:
Enter an open '' tag between the '' and '' tags after the 'My favorite monstor is: ' message. Assign an 'id' to the '' tag and set its value to 'textToChange.' For example, type the text 'Dracula' after the '' tag and close the '' tag:
My favorite monster is:
Dracula
Add an HTML '
' tag after the '' tag. Set the input type to 'button' and add an 'onclick()' event that calls the 'changeText' function and passes the value 'Frankenstein.' Set the input field 'value' attribute to 'Change Text.' Save and close 'changeTextVars.html.'
My favorite monster is:
Dracula
Open 'changeTextVars.html' in a Web browser. Click the 'Change Text' button to use the 'monsterName' variable to change the 'textToChange' field from 'Dracula' to 'Frankenstein.'
In Feburary 2016 28,

Wednesday, 24 February 2016

How to Write a Java Application Program That Prompts a User to Input One After the OtherIn Feburary 2016 24,

In Feburary 2016 24,
Start a new project in NetBeans by clicking on its icon, selecting 'File/New Project', and choosing 'Java Applicatoin.' A new Java project is created and a source code file appears in the NetBeans text editor. The source code file has a main function and little else.
Import the 'Console' class by writing this line at the top of the source code file:import java.io.Console;
Create a new Console object by writing the following line of code between the curly brackets of the main function:Console c = System.console();
Declare a couple of strings, one for a user's name, and one for the user's password. You can accomplish this by writing the following:String userName, password;
Prompt the user to enter his or her user name using the following line of code:userName = c.readLine('Enter your user name and press enter: ');
Repeat the last step for the user's password, like this:password = c.readLine('Enter your password and press enter: ');
Run the program by pressing F6. The program will prompt you to enter a name. After you enter your name, it will ask you for a password. You can add more prompts if you would like. All you have to do is repeat the previous step and add more strings to hold the data.
In Feburary 2016 24,

How to Convert String to Time in SQLiteIn Feburary 2016 24,

In Feburary 2016 24,
Run SQLite queries from the command prompt with the 'sqlite3' program by typing:$ sqlite3 my_db.dbThis will create a database with the name 'my_db.db' if it doesn't already exist. It also places you in the sqlite3 environment, which you can exit with the commands '.quit', '.q' or '.exit.'
Call the 'strftime (format, timestring, modifier, modifier)' function to return a formatted date from a time string. This is useful for comparing dates, displaying a date in a certain format to a user or to upload a date in a consistent matter. The format of a time string follows the rules from the C 'strftime' function. Some of the valid time string formats include 'YYYY-MM-DD,' 'YYYY-MM-DD HH:MM,' 'YYYY-MM-DD HH:MM:SS,' 'YYYY-MM-DD HH:MM:SS.SSS,' 'DDDDDDDDDD' and 'now.' The 'Y' character stands for year, 'M' for month, 'D' for day, 'H' for hour, 'M' for minute and 'S' for second. The 'DDDDDDDDDD' format represents a unix timestamp. For example, the following query will compute how many seconds have passed since a date in 2002:$ sqlite3 my_db.db SELECT strftime('%s','now') - strftime('%s','2002-05-11 01:56:23');
Call the 'date,' 'time,' 'datetime' or 'julianday' functions to use a pre-formatted version of the 'strftime' function. The 'date' function returns the date with the format 'YYYY-mm-dd,' the 'time' function returns it as 'HH:MM:SS,' the 'datetime' function returns it as 'YYYY-mm-dd HH:MM:SS' and the 'julianday' function returns the Julian day number. For example, the following query will return a date from 2009 as '2009-09-22,' removing the hour, minute and second information:$ sqlite3 my_db.db SELECT date('2009-09-22 02:57:13');
In Feburary 2016 24,

Thursday, 18 February 2016

How to Get TD Class Value in JqueryIn Feburary 2016 18,

In Feburary 2016 18,
Open the Web page in Notepad or a code editor, and look for the jQuery library file. If your code contains no reference to this file, add this code:Place this reference between the '
' tags of your Web page code or just above the closing '
' tag.
Add a pair of 'Write all jQuery code between the '
' tags.
Write a function to check for when the document finishes loading. This is also known as the 'document ready' function:$(function () {});The above code is short-hand for '$(document).ready()'.
Declare an array type variable that will hold the class values of your '' tags:var tdValue = [];Add this code between the curly braces of the 'document ready' function.
Iterate through each instance of '' in the Web page code by creating an 'each()' loop:$('td').each(function(i) {});This code goes after the array declaration. Note that 'each()' attaches to the 'td' selector, and a variable 'i' is passed as a parameter in the function. You need the 'i' variable to get values from the array.
Set your array equal to the results of the 'attr()' function inside the 'each()' loop:tdValue[i] = $(this).attr('class');The 'this' selector gets the parent selector, in this case 'td' from the selector in the line of code above it. Use 'class' inside 'attr()' to get the value of the class attribute for each '' tag.
Output the class name for each '' tag as text in each table cell by adding this line of code after the line containing your 'attr()' function:$(this).text(tdValue[i]);The finished script should look like this:$(function() {var tdValue = [];$('td').each(function(i) {
tdValue[i] = $(this).attr('class');$(this).text(tdValue[i]);
});});
In Feburary 2016 18,

How to Do Bubble Sorting in VB.netIn Feburary 2016 18,

In Feburary 2016 18,
Open Visual Basic and click 'File' and 'New project' to create a new project. Select 'ConsoleApplication.' When it comes time to enter your code in a real project with a Graphical User Interface (GUI), you can simply copy this function there without modification.
Paste the following code above the 'Main' function:
Sub BubbleSort(ByRef arr() As Integer)Dim tempDim switch = TrueWhile switchswitch = FalseFor x = 0 To arr.Length - 2If arr(x) > arr(x+1) Thentemp = arr(x)arr(x) = arr(x+1)arr(x+1) = tempswitch = TrueEnd IfNextEnd WhileEnd Sub
An important thing to recognize is that the arr is passed into the subroutine 'ByRef.' This allows the function to modify the contents of the array.
Paste the following into the 'Main' function to test the BubbleSort method:
Sub Main()Dim arr = {3, 4, 5232, 1, 232, 12, 34, 14, 21, 213, 213, 21, 321}Console.WriteLine('Unsorted')For Each x In arrConsole.Write(x & ' ')NextConsole.WriteLine()BubbleSort(arr)Console.WriteLine('Sorted')For Each x In arrConsole.Write(x & ' ')NextConsole.ReadKey()End Sub
End ModuleThis generates a simple, unsorted array of integers and tells BubbleSort to sort them, then prints the results.
In Feburary 2016 18,

Saturday, 6 February 2016

How to Check MySQL Null on PHPIn Feburary 2016 06,

In Feburary 2016 06,
Open your PHP source file in a text editor, such as Windows Notepad.
Use the 'mysql_query(query)' function to send a MySQL query to the active database. For example, '$result=mysql_query('Select my_var from my_table');'.
Use the 'mysql_fetch_assoc(result)' function inside a 'while' loop to fetch rows from the MySQL result. For example, 'while ($my_values=mysql_fetch_assoc($result)) {'.
Use the 'is_null(variable)' function inside the 'while' loop to determine if a field from a returned row has a NULL value. For example, if (is_null($my_values['my_var'])) { echo 'is NULL'; } }' will print 'is NULL' for any values of 'my_var' that are NULL.
Save the PHP file.
In Feburary 2016 06,