Test 2 Flashcards
Creating a table in SQL with restricted choice
CREATE TABLE user (
username VARCHAR(20) NOT NULL PRIMARY KEY,
password VARCHAR(20) NOT NULL,
gender VARCHAR NOT NULL CHECK(gender = “male” or gender = “female”),
nickname VARCHAR(20) NOT NULL,
FOREIGN KEY nickname REFERENCES score(nickname)
);
SQL query in php with a established connection
leftarrow ?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(“Connection failed: “ . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql);
if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "br>"; } } else { echo "0 results"; } $conn->close();
Insert Query
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’, ‘Tom B. Erichsen’, ‘Skagen 21’, ‘Stavanger’, ‘4006’, ‘Norway’);
Update Query
UPDATE Customers
SET ContactName = ‘Alfred Schmidt’, City= ‘Frankfurt’
WHERE CustomerID = 1;
Delete Query
DELETE FROM Customers WHERE CustomerName=’Alfreds Futterkiste’;
What does a binary search look like?
def BinarySearch1(searchlist1,goal): found = False startpos = 0 endpos = len(searchlist) -1
#print (“Endpos at beginning = “,endpos)
while (startpos <= endpos) and found == False:
middle = (startpos+endpos)//2 #Integer Division
if searchlist[middle] == goal:
found = True
elif searchlist[middle] smaller than goal:
startpos = middle + 1
else:
endpos = middle -1
if found == True:
print(“Match has been found at position”,middle)
else:
print(“Goal not found”)
What does a bubble sort look like?
def bubbleSort(beforeName, beforeScore):
for outer in range (len(beforeScore) -1,0,-1): for inner in range(outer): if beforeScore[inner]>beforeScore[inner+1]: temp = beforeScore[inner] tempName = beforeName[inner] beforeScore[inner] = beforeScore[inner+1] beforeName[inner] = beforeName[inner+1] beforeScore[inner+1] = temp beforeName[inner+1] = tempName
print(beforeScore, beforeName)
What does an insertion sort look like?
def insertionSort(beforeName, beforeScore):
for index in range(1,len(beforeScore)): currentval = beforeScore[index] currentName = beforeName[index] position = index while position > 0 and (beforeScore [position-1] > currentval): beforeScore[position] = beforeScore[position-1] beforeName[position] = beforeName[position-1] position = position - 1 beforeScore[position] = currentval beforeName[position] = currentName
print(beforeScore, beforeName)
How to create a 2D array
map = [[’.’ for col in range(0,30)] for row in range(0,10)]
What does instantiation mean
An object that has been created which contains the methods and attributes of a particular class e.g. Aiden is an instantiation of a human class
What is a class
A template for a particular object e.g. a car class has information about wheels, colour, engine, seats, speed and methods that can manipulate these values.
What does encapsulation mean
Attributes of an instantiation of a class are private and can only be changed using the setter and getter methods. E.g. a car’s speed can only be changed using car.accelerate() and car.decelerate() methods
What does polymorphism mean
When a subclass has a method of the same name as its superclass but the functionality is different. For example car superclass has an accelerate method which increases speed by 10 but a toycar subclass has an accelerate method which plays a sound effect of a car. This means that program code doesn’t have to know which class of car is being passed into it.
What does inheritance mean
Where a class can be set up as a subclass of another class (superclass) and this automatically inherits the attributes and methods of it’s parent. For example toycar has all the attributes of car - colour, speed, engine, seats, wheels
What does a constructor do
Creates an instantiation of a class with default attribute values
Constraints within a project
Time, technology, labour, cost and if one of them is changed, they all change
Code to create a form with php action
html>
body>
h1> Tennis Form /h1>
form action=”/TennisSetSession.php” method=”get”>
label for=”Name”> Full Name: /label>br>
input type=”text” id=”Name” name=”Name”>br>br>
label for="ExperienceLevel"> Level Of Experience: /label>br> input type="radio" id="Colour" name="colour" value="Green"> label for="Colour"> Green /label>br> input type="radio" id="Colour" name="colour" value="Yellow"> label for="Colour"> Yellow /label>br> input type="radio" id="Colour" name="colour" value="Red"> label for="Colour"> Red /label>br>br> label for="Day">Choose A Day For Your Lesson: /label>br> select id="Day" name="Day"> option value="Tuesday">Tuesday /option> option value="Thursday">Thursday /option> option value="Saturday">Saturday /option> /select>br>br> label for="Time">Choose A Time For Your Lesson:/label>br> select id="Time" name="Time"> option value="9:00am">9:00am /option> option value="11:00am">11:00am /option> option value="1:00pm">1:00pm /option> /select>br>br> input type="submit" value="Submit"> /form>
/body>
/html>
Form Types
submit
radio
checkbox
button
Examples of session variables
?php
session_start(); ?> !DOCTYPE html> html> body>
?php
$_SESSION["Name"] = $_GET["Name"]; $_SESSION["Colour"] = $_GET["colour"]; $_SESSION["Day"] = $_GET["Day"]; $_SESSION["Time"] = $_GET["Time"]; echo "Thank You For Booking A Lesson With Mr Bringleberry.";
?>
/body>
/html>
Setting up a server and destroying it
?php session_start(); ?> !DOCTYPE html> html> body>
?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
/body>
/html>