Skip to content

Instantly share code, notes, and snippets.

@emyb
Created June 15, 2021 19:52
Show Gist options
  • Save emyb/6b3c6a1df14b1c2d0adfd025e44076e4 to your computer and use it in GitHub Desktop.
Save emyb/6b3c6a1df14b1c2d0adfd025e44076e4 to your computer and use it in GitHub Desktop.
<?php
// A simple file to do some php stuff then do some html stuff.
// The "array $records = []" means that the parameter $records has to be of data type array, and if there is no parameter
// passed, $records will default to an empty array. This will reduce the amount of error checking in the function. This
// is called an optional parameter. Order of optional and required parameters are important. Read some php docs about it.
// A required parameter would just be function stuff($parameter1, $parameter2) {}.
function div_the_things(array $records = []) {
$returnString = "<div class='outer-div'>";
for ($i = 0; $i < count($records); $i++) {
$returnString .= "<div class='inner-div'>Person id: {$records[$i]['id']}</div>";
$returnString .= "<div class='inner-div'>Person name: {$records[$i]['firstname']} {$records[$i]['firstname']}</div>";
}
$returnString .= "</div>";
return $returnString;
}
function div_the_things_advancedish(array $records = []) {
$returnString = "";
for ($i = 0; $i < count($records); $i++) {
$returnString .= make_div_with_stuff_in_it('Person id', $records[$i]['id'], 'inner-div');
$returnString .= make_div_with_stuff_in_it('Person name', "{$records[$i]['firstname']} {$records[$i]['firstname']}", 'inner-div');
}
return $returnString;
}
function make_div_with_stuff_in_it($title = '', $contents = '', $class = '') {
$contents = "<div class='{$class}'>{$title}: {$contents}</div>";
return $contents;
}
// Make connection to the database, query the database, get the results.
$records = [
['id' => 1, 'firstname' => 'Harry', 'lastname' => 'Potter'],
['id' => 2, 'firstname' => 'Hermione', 'lastname' => 'Granger']
['id' => 3, 'firstname' => 'Ronald', 'lastname' => 'Weasly'],
['id' => 3, 'firstname' => 'Tom', 'lastname' => 'Riddle']
];
echo div_the_things($records);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment