Category: Uncategorized

  • WordPress Template Hierarchy

    WordPress Template Hierarchy

    WordPress Template Hierarchy

    Every Template in wordpress has a corresponding php file. I have added the php files that each template uses. Below is a table with further explanations for each template.

  • PHP LOOPS: For, Foreach & While

    PHP LOOPS: For, Foreach & While

    PHP for beginners

    For loops

    This is a for loop

    <?php
    // FOR LOOP
    for($i = 0; $i<4; $i++) {
    	echo $i;
    }
    ?> 


    Let’s break it down
    $i=0;
    start at the value 0
    $i<4; run the loop as long as $i is less than 4
    $i++ Increment $i by 1 every time you run through the loop
    This loop will run 4 times because $i starts with the value 0, then after the first loop it’s 1, after the second loop 2, then 3 then 4. When $i > 4 the loop will stop.

    This loop will echo the value of $1 for each loop so it will echo “0123” and then it stops.

    For loop: Blogpost example

    Instead of $i<5;
    Try $i <count($blogposts);
    Now it will keep running through each blogpost and stop after post4

    <?php 
    // FOR LOOP
    $blogposts=['post1','post2','post3','post4'];
    for($i = 0; $i <count($blogposts); $i++) {
    	echo $i;
    	echo " --- ";
    	echo $blogposts[$i]." --- ";
    } 
    // This will echo 0 --- post1 --- 1 --- post2 --- 2 --- post3 --- 3 --- post4 ---
    ?> 

    Foreach ($blogposts as $post): So each time we cycle through the array $blogposts, each value in that array is stored in $post (you can call $blog whatever you want, it’s just a variable that stores each value in the array $blogposts). You can then echo out $post; 

    Foreach loop

    How the foreach loop works (look at example below)
    It’s especially used used when you don’t know the number of items in an array
    (you don’t set $i in this loop) .
    Foreach ($blogposts as $post): So each time we cycle through the array $blogposts, each value in that array is stored in $post (you can call $blog whatever you want, it’s just a variable that stores each value in the array $blogposts). You can then echo out $post;

     

    <?php 
    
    $blogposts=['post1','post2','post3','post4'];
    //FOREACH LOOP 
    foreach($blogposts as $post) {
    	echo $post; 
    // This echoes out post1post2post3post4
    // Try echo $post . '<br />'; to add linebreak between each loop. 
    }
    ?> 

    A foreach loop is easier when cycling through an array than the for() loop as you need to write less code. With the for loop you can specify how many loops you want to run, you cannot do that with the foreach loop.

    <?php 
    
    // ASSOCIATIVE ARRAY 
    $products = [
    ['name'=>'Tuna','price'=>10],
    ['name'=>'grapes','price'=>14],
    ['name'=>'advocado','price'=>17],
    ['name'=>'kiwi','price'=>2],
    ['name'=>'beef','price'=>32],
    ['name'=>'salmon','price'=>34]
    ['name'=>'Apples','price'=>12]
    ]; 
    
    //FOREACH LOOP 
    foreach($products as $product) {
    echo $product['name'] . ' - '. $product['price'];
    echo '<br />';
    }
    // ECHOES OUT product name and price of each product 
    
    ?> 

    While loop 

    $i=0 This must be declared outside the while loop function
    count($products) This will be the number 7, because we have 7 products in the array.
    while($i < count($products)) is the same as writing while(0 < 7) so the loop will run 7 times.
    $i++ Means increment $i by one each time the loop runs: you must increment $i inside the loop function or else the loop will never stop (infinite loop) because 7 is always less than zero.

    <?php 
    
    // ASSOCIATIVE ARRAY 
    $products = [
    ['name'=>'Tuna','price'=>10],
    ['name'=>'grapes','price'=>14],
    ['name'=>'advocado','price'=>17],
    ['name'=>'kiwi','price'=>2],
    ['name'=>'beef','price'=>32],
    ['name'=>'salmon','price'=>34]
    ['name'=>'Apples','price'=>12]
    ]; 
    
    
    //WHILE LOOP
    $i = 0; // $i must be delared outside the loop. 
    
    while($i < count($products)) {
    	echo $products[$i]['name'];
    	echo' <br />'; 
    	$i++; // you must increment i or else it's always =0. 
    //count($products) is = 7 (there are 7 products in the array)
    }
    ?>

  • MySQL Commands

    MySQL Commands

    MySQL Commands table

    In this example we create a Database Called Movies. So every time you see the word “Movies” it’s representing a Database.

    The first steps are to create a database and then select the database you created.

    1. Create a Database: CREATE DATABASE MOVIES;
    2. Chose the database you want to perform operation on: USE MOVIES;
    3. Check that MOVIES is now the selected Database: SELECT DATABASE(); (it should display movies)
    4. Now you are ready to perform operations on the Database.
    SUBJECT MYSQL COMMAND + EXAMPLE
    Create DB CREATE DATABASE MOVIES;
    Drop (delete) DBDROP DATABASE MOVIES;
    Show all DBSHOW DATABASES;
    use specific DB USE MOVIES
    # Selects a specific database
    Select the used databaseSELECT DATABASE();
    If you typed USE MOVIES, it will return that database. It returns the Database you decided to “USE”. When you type commands, they will apply to the database you use.
    Create a Table
    & insert Values into the table
    CREATE TABLE MOVIES (
    col 1,
    col 2,
    col 3
    col x
    )


    CREATE TABLE MOVIES (
        movie_titles VARCHAR(60),
        imdb_ratings INT
    year_release INT,
    genre VARCHAR(30),
    );

    INSERT INTO MOVIES (
    id INT PRIMARY KEY AUTO_INCREMENT,
    movie_titles,
    imdb_ratings,
    year_release,
    genre

    )
    VALUES ('WALL-E', 6,2008,'Animation'),
    ('Titanic',8, 1997, 'Historical'),
    ("Top Gun". 6, 1987, "action")



    INSERT DATA INTO TABLEWhen inserting data into a table you write 2 functions: First you specify which columns (col_name) you want to insert data into. Then below you Specify which Values you want to insert into those columns)

    INSERT INTO MOVIES (
    id INT PRIMARY KEY AUTO_INCREMENT,
    movie_titles,
    imdb_ratings,
    year_release,
    genre

    )
    VALUES ('WALL-E', 6,2008,'Animation'),
    ('Titanic',8, 1997, 'Historical'),
    ("Top Gun". 6, 1987, "action")

    Show Columns from a TABLE_NAME. Show all columns from movies table
    SHOW COLUMNS FROM MOVIES;

    This does the same as above
    DESC MOVIES;
    DESCRIBE MOVIES;
    Select sth from a tableSELECT movie_titles FROM movies;

    SELECT * FROM movies;

    SELECT movie_titles FROM movies WHERE imdb_ratings > 6;

    SEÆECT movie_titles, imdb_ratings, year_release FROM movies WHERE imdb_ratings > 6;
    SELECT EVERYTHING WHERE
    ALIAS: select a column from table and give the column header an alias when you output, SELECT movie_title AS movie_name FROM MOVIES;
    So this selects the column called “movie_title” but in the displayed result the column header gets named “movie_name” instead of “movie_title”

    EXAMPLE 2
    SELECT id AS movie_id FROM MOVIES;

    EXAMPLE 3
    SELECT imdb_ratings AS movie_ratings, title AS movie_name FROM MOVIES WHERE imdb_ratings <6;
    DROP TABLE TABLE_NAMEDeletes the table
    DROP TABLE MOVIES;
    Update a table Update the table called Movies

    UPDATE MOVIES
    SET imdb_ratings = 8
    WHERE movie_titles = “WALL-E”

    NOTE! Make sure you didn’t leave an empty space in the name when you inserted it into the TABLE. If you did, you have to write “WALL-E ” instead of “WALL-E”

    Example: Update password for users
    UPDATE users
    SET password = “New_Password”
    Where ID = 102;

    (ID is better than name, because 2 people could have the same name, but each user only has only 1 unique ID)
    Delete DELETE FROM movies
    WHERE id = 1

    This deletes the movie whose id = 1.

    DELETE * FROM movies
    This deletes the entire table (careful!)
    CHAR(10) vs VARCHAR(10) If you create a table
    CREATE TABLE users (
    name CHAR(10)
    username VARCHAR(10)
    )
    and you insert the values into

    INSERT INTO users(name, username)
    VALUES (“Thom”,”Thom123″);

    VARCHAR(10) means “up to 10 characters”
    CHAR(10) means “exactly 10 characters” so if a user types a username less that 10 characters e.g Thom123 it will be stored with 3 blank spaces “Thom123 “
    So use VARCHAR when possible!
    TIMESTAMPS If a user makes a comment or updates the comment, you need to set a timestamp for when the comment was created and when it was updated
    CREATE TABLE user_comments (
      id INT AUTO_INCREMENT PRIMARY KEY,
      username VARCHAR(25),
      comment_content VARCHAR(255),
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    );

    So the Default value for the Timestamps is the Current time.

    Table will be updated.

    REPLACE SELECT REPLACE (“Big dog”, “Big”, “Small”

    Result
    “Small dog”

    SELECT REPLACE(username, “Willie”, “James”) FROM USERS;

    This replaces any username that is Willie, with James in the table USERS.
  • Hello world!

    Welcome to WordPress. This is your first post. Edit or delete it, then start writing!