Handy PHP Snippets

Posted by admin on April 28, 2019

PHP returns a special datatype for queries that can be converted in to an assoc. array of assoc. arrays with this:

$rows = array();
while($rows[] = mysqli_fetch_assoc($result));
array_pop($rows);

Display a query from the above as an HTML table:

$query = $your_query;
echo '';
echo '';
foreach($query[0] as $key => $value) {
    echo '';
}
echo '';
foreach($query as $row) {
    echo '';
        foreach($row as $column) {
        echo '';
    }
    echo '';
}
echo '
'; echo $key; echo '
'; echo $column; echo '
';

Get the last second and first second of any given month:

$timestamp = strtotime('last month');
$first_second_last_month = strtotime(date('Y-m-01 00:00:00', $timestamp));
$last_second_last_month  = strtotime(date('Y-m-t 23:59:59', $timestamp));

Flush to Browser

function flush_to_browser() {
    echo str_repeat(' ', 4096);
    flush();
}

More to come ...

blog comments powered by Disqus