cyber-s / blog

tipsや質問などをここにknowledgeとして残します。
0 stars 0 forks source link

🤓 e-Learning Journey - PHP #709

Open huirongcs opened 4 years ago

huirongcs commented 4 years ago

String Functions (Cont'd)

Explode 💥 & Implode

Explode: Split a string by a string, and returns an array of strings.

Example: Split a sentence by detecting a comma

$a = "Welcome, to PHP, explode!"; // There are 2 commas here
$myArr = explode(",", $a); // detects "," in $a and split the string
print_r($myArr);

↑ prints out ↓

Array
(
    [0] => Welcome
    [1] =>  to PHP
    [2] =>  explode!
)

Note: Notice that the commas are also gone.

If we split the sentence by spaces, it’d be:

$a = "Welcome, to PHP, explode!";
$myArr = explode(" ", $a);
print_r($myArr);

↑ prints out ↓

Array
(
    [0] => Welcome,
    [1] => to
    [2] => PHP,
    [3] => explode!
)

Implode: Join array elements with a string; returns a string

Continuing from example above…

$a = "Welcome, to PHP, explode!";
$myArr = explode(" ", $a);
print_r($myArr);

$myStr = implode(" ", $myArr); // Join $myArr back with spaces in between
echo $myStr;

↑ prints out ↓

Array
(
    [0] => Welcome,
    [1] => to
    [2] => PHP,
    [3] => explode!
)
Welcome, to PHP, explode!

If we add HTML in between, it works too.

$a = "Welcome, to PHP, explode!";
$myArr = explode(" ", $a);
$myStr = implode("<br>", $myArr);
echo $myStr;

↑ prints out ↓

Welcome,
to
PHP,
explode!

Setting up connection to database

1) Create a file core/settings.php which contains the host, user, password, and database name:

defined('DB_HOST') or define('DB_HOST', 'localhost');
defined('DB_USER') or define('DB_USER', 'root');
defined('DB_PASS') or define('DB_PASS', 'root');
defined('DB_NAME') or define('DB_NAME', 'elearning');

2) Create a file core/conn.php which will connect the server and database together, using the settings.php file we created.

<?php
require_once('settings.php');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if(!$link){
    die("ERROR: ".mysqli_connect_error());
}
?>

If $link has error, the codes will stop there and print out error mysqli_connect_error() in when we preview index.php

3) Create a file index.php which contains this:

<?php
require_once('core/conn.php');
?>

For the above, it will print out ↓

ERROR: Unknown database 'elearning'

There’s an error since this database has not been created.

4) Creating database with sql query. Create a new file setup.php (this only needs to be run 1 time)

<?php
require_once('core/conn.php'); // we need to temporarily remove DB_NAME from conn.php for this to work
$query = "CREATE DATABASE elearning"; // This is a SQL query
mysqli_query($link, $query); // ([connection object], [query])
?>

In this case, the [connection object] is $link, from the file core/conn.php

5) Go to http://localhost/my_project_folder/setup.php on the browser to run this page, and the database elearning will be automatically created.

justcallmehide commented 4 years ago

@huirongcs oh finally, connecting DB (゚∀゚)

What is the last level of these lesson?

huirongcs commented 4 years ago

elearning-php Hmm, this is just for PHP course. There are other courses inside this account. After this probably I will continue learning "Complete SQL Database Training Course" or "JSON AJAX Data Transfer To MySQL Database Using PHP"? 🤔

justcallmehide commented 4 years ago

ok~

@huirongcs After that, I recommend you to create a side project with MVC architecture. It could be good if there are some MVC architecture courses.