Saturday, April 26, 2025
Wednesday, April 23, 2025
MySQLi Last Insert ID
last_insert_id.php
<?php
//Config
$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";
//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);
//Check Connection
if (!$connection) {
die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
echo "Connection Successfull <br>";
}
//SQL Command
$sql_command = "INSERT INTO people (id, name, lastname, telephon, email, address)
VALUES (NULL, 'John', 'Smith', 555777, 'mailx@server.net', 'Main Road 12a')";
//Check SQL Command
if (mysqli_query($connection, $sql_command)) {
$last_entry = mysqli_insert_id($connection);
echo "SQL Command OK, Last ID: " . $last_entry . "<hr>" ;
} else {
echo "SQL ERROR" . mysqli_error($connection);
}
?>
After we run ti multiple times:
Connection Successfull
SQL Command OK, Last ID: 11
JavaScript Redirect to Another Site
We can use JavaScript to redirect the user to another site by changing the window.location
property to the URL of the new site:
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
function redirect() {
window.location.assign("https://google.com");
}
</script>
<input type="button" value="To Google"
onclick="redirect();">
</body>
</html>
This script defines a JavaScript function called redirect()
that uses the window.location.assign()
method to redirect the user to the Google homepage.
The window.location.assign()
method navigates the browser to the URL specified in its argument, effectively redirecting the user to a new page.
The script also creates an HTML input
element with a button type and the text "To Google". When the button is clicked, the onclick
attribute calls the redirect()
function, which redirects the user to the Google homepage.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body onload="redirect()">
<script>
function redirect() {
window.location.assign("https://google.com");
}
</script>
</body>
</html>
This code uses the onload
event to automatically redirect the user to the Google homepage when the page loads.
The onload
event fires when the page has finished loading, so when the user opens the page, the redirect()
function is called automatically.
The redirect()
function uses the window.location.assign()
method to navigate the browser to the URL specified in its argument, which in this case is "https://google.com".
JavaScript Get Window Size
You can get the size of the current window using the window.innerWidth
and window.innerHeight
properties. These properties return the width and height of the viewport, which is the visible area of the browser window.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var wid = window.innerWidth;
var hei = window.innerHeight;
document.write("<h1>" + wid + "</h1>");
document.write("<h1>" + hei + "</h1>");
</script>
</body>
</html>
This script gets the width and height of the current window using the window.innerWidth
and window.innerHeight
properties and then displays them as headings using the document.write()
method.
When you run this script in a browser, it will display the width and height of the window as headings on the page.
The window.innerWidth
and window.innerHeight
properties are useful for many purposes in JavaScript, such as:
-
Responsive web design: You can use these properties to create responsive web designs that adjust to the size of the user's screen.
-
DOM manipulation: You can use the window size to dynamically adjust the size and position of HTML elements on a page based on the size of the viewport.
-
Game development: You can use the window size to set the boundaries of a game's playing area or to adjust the game's graphics and controls based on the size of the user's screen.
-
User experience: You can use the window size to optimize the layout and design of a website or application to provide the best user experience on different devices and screen sizes.
The window.innerWidth
and window.innerHeight
properties are essential tools for creating dynamic, responsive, and user-friendly web applications in JavaScript.
JavaScript Random Numbers
Random numbers are useful in programming whenever you need to generate unpredictable or random events or data. They can be used to add variability and unpredictability to your applications, as well as to test the robustness of your code.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
//One random number:
document.write(Math.random() + "<br>");
//Whole Number Generator
var random_num = (Math.random() * 10);
var whole_num = Math.round(random_num);
document.write(whole_num);
</script>
</body>
</html>
This script generates random numbers using the Math.random()
method.
The first line of code generates a random number between 0 and 1 using the Math.random()
method and outputs it to the webpage using the document.write()
method, followed by a line break using the <br>
tag.
The second part of the script generates a random whole number between 0 and 10 using the Math.random()
method. First, a random number between 0 and 1 is generated using Math.random()
, then it is multiplied by 10 to get a random number between 0 and 10. The resulting number is not guaranteed to be a whole number, so the Math.round()
method is used to round the number to the nearest integer. Finally, the resulting whole number is output to the webpage using the document.write()
method.
When the script is run, it outputs a random number between 0 and 1 and a random whole number between 0 and 10 to the webpage. The Math.random()
method is useful for generating unpredictable and random data in JavaScript applications.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
//Generate 10 Whole Random Number
for (x = 0; x < 10; x = x + 1) {
document.write(Math.round(Math.random() * 10) + "<br>");
}
</script>
</body>
</html>
This script generates 10 random whole numbers between 0 and 10 using a for
loop and the Math.random()
and Math.round()
methods.
Inside the loop, the Math.random()
method is used to generate a random number between 0 and 1, which is then multiplied by 10 to get a random number between 0 and 10. The resulting number is not guaranteed to be a whole number, so the Math.round()
method is used to round the number to the nearest integer. Finally, each resulting whole number is output to the webpage using the document.write()
method, followed by a line break using the <br>
tag.
When the script is run, it generates 10 random whole numbers between 0 and 10 and outputs them to the webpage. This script can be useful for generating a random set of data for testing or simulation purposes.
Many programming languages have similar functionality to JavaScript's Math
module and random()
method for generating random numbers.
For example, Python has a random
module which provides functions for generating random numbers, including random()
which returns a random floating-point number between 0 and 1. The module also includes functions for generating random integers within a range, shuffling lists, and more.
Similarly, Java has a java.util.Random
class which can be used to generate random numbers. The class has methods for generating random integers, floating-point numbers, and more.
Other programming languages such as C++, C#, Ruby, and PHP also provide functionality for generating random numbers and performing mathematical operations.
While the exact syntax and available methods may differ between programming languages, the concepts and functionality are similar across many languages.
JavaScript Prompt Box, User Input
The prompt box displays a message to the user and provides a text field for the user to enter a response.
Prompt boxes are useful in JavaScript when you need to prompt the user for input data, such as asking for their name, age, or other personal information. Prompt boxes are also commonly used to confirm an action or prompt the user for a yes or no response.
Here are a few examples of where prompt boxes can be used in JavaScript:
-
Collecting user input: You can use a prompt box to ask the user for input data, such as their name, age, or email address.
-
Confirming an action: You can use a prompt box to confirm an action before proceeding, such as deleting a file or closing a window.
-
Validating input: You can use a prompt box to validate user input, such as checking if the user has entered a valid email address or phone number.
-
Customizing user experience: You can use a prompt box to customize the user experience, such as asking the user to choose their preferred language or theme.
Prompt boxes are a simple and effective way to prompt the user for input data and improve the interactivity of your JavaScript applications.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var grab_stuff = prompt("Your Name: ");
document.write(grab_stuff + "<br>");
</script>
</body>
</html>
This script displays a prompt box that asks the user to enter their name using the prompt()
method. The entered value is stored in the grab_stuff
variable.
Then, the entered value is output to the webpage using the document.write()
method, followed by a line break using the <br>
tag.
When the script is run, it displays a prompt box with a message "Your Name: ", and the user can enter their name into the text field. Once the user clicks "OK", the entered value is stored in the grab_stuff
variable and output to the webpage.
<script>
var grab_stuff = prompt("Your Name: ");
for (x = 0; x < 5; x = x + 1) {
document.write(grab_stuff + "<br>");
}
</script>
This script displays a prompt box that asks the user to enter their name using the prompt()
method. The entered value is stored in the grab_stuff
variable.
Then, a for
loop is used to output the entered value to the webpage five times using the document.write()
method, followed by a line break using the <br>
tag. The loop increments the variable x
by 1 on each iteration until it reaches a value of 5.
When the script is run, it displays a prompt box with a message "Your Name: ", and the user can enter their name into the text field. Once the user clicks "OK", the entered value is stored in the grab_stuff
variable and output to the webpage five times using the for
loop. Each time the name is output, it is followed by a line break using the <br>
tag.
Tkinter Introduction - Top Widget, Method, Button
First, let's make shure that our tkinter module is working ok with simple for loop that will spawn 5 instances of blank Tk window . ...
