|
|
For a weekly status report, I need to determine the number of work requests that are approved and awaiting implementation. The list of requests in that state is contained in a webpage that contains other information, including requests that are in various other states, such as those awaiting approval. I normally download the webpage containing the information to run scripts against it to extract other information from the page, so I decided to create a PHP script that would display just the list of requests awaiting implementation and produce a count of those requests in that state. On the source webpage the line on the page that marks the start of the section of the page containing the requests that are approved and awaiting implementation contains the text "Requests Waiting Implementation". The HTML code on the page that marks the end of that section contains and ending div tag. So I created the two PHP variables below to hold the two strings I need to search for within the file.
$startString = "Requests Waiting Implementation"; $endString = "</div>";
[ More Info ]
I have an SQLite database that I use to track work requests. Some requests are time sensitive, i.e. they need to be completed within a certain number of days from the time they are approved. So I have an "Approved" and an "Implemented" column in the database to record the date I approved a request and the date it was implemented. All requests should be completed within 5 business days, so when I display the data on a webpage with PHP, I want to see the number of elapsed working days between the time I approved a request and the time it was implemented. To do so, I use some code provided by George John at Calculate business days with a slight modification. The code I use appears below:
function getWorkingDays($startDate, $endDate) { $begin = strtotime($startDate); $end = strtotime($endDate); if ($begin > $end) { return 0; } else { $no_days = 0; while ($begin <= $end) { $what_day = date("N", $begin); if (!in_array($what_day, [6,7]) ) // 6 and 7 are weekend $no_days++; $begin += 86400; // +1 day }; return $no_days - 1; } }
[ More Info ]
I have a PHP script that queries an SQLite database displaying the results on a web page. There are two fields, "EC Link" and "Configs Link" in the database that can contain URLs pointing to documentation on another server, but sometimes there is no URL in the database for those fields. If there is a URL, I want to display a clickable "EC" and "Configs" links. If there is no URL in either field, I want to simply display the text, but without it being clickable to indicate that there is no URL in the database table. I was using "is_null" for that purpose as shown below:
if (is_null($row['EC Link'])) { echo "EC | "; } else { echo "<a href=\"" . $row['EC Link'] . "\">EC</a> | "; } if (is_null($row['Configs Link'])) { echo "Configs | "; } else { echo "<a href=\"" . $row['Configs Link'] . "\">Configs</a> | "; }
But I found that sometimes I was seeing clickable links even though there
was no URL for the EC and/or Configs documentation for a particular
record in the database. When I checked the records in the relevant
table in the database, I found that the fields were blank, i.e., there
was no data in them, but they weren't marked as
null.
So, to allow for the values being either null or blank, I used an
empty
, instead of is_null
, test.
[ More Info ]
johndoe
with a password of ThePassword
. The database is named
Acme
and contains a table named Accounts
.<?php $con = mysql_connect("localhost","johndoe","ThePassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("Acme", $con); $accounts = mysql_query("SELECT * FROM Accounts"); ?>
[ More Info ]
-l
option; that's the letter "l", not the
number "1". E.g., when I tried accessing a web page I had created,
e.g., http://www.example.com/sompepage.php, I saw only a blank page. If
I examined the source code for the page in the browser from which I was
viewing the page, there was nothing there. Looking through the PHP code,
the cause of the error wasn't immediately obvious to me, but when I
issued the command php -l somepage.php
on the server where
the page resided, the line that was causing the problem was identified.
$ php -l somepage.php PHP Parse error: syntax error, unexpected '$download_url_description' (T_VARIAB LE) in somepage.php on line 79 Errors parsing somepage.php
Examining the code, I realized I had omitted a required semicolon from the prior line. When I added the semicolon at the end of the line and reran the check, I no longer saw any error messages and when I refreshed the webpage in the browser after making the update, it then displayed correctly.
whoami_here.php
, in the home folder for the WordPress installation
and then accessed the webpage for that script via a browser, e.g.
http://example.com/whoami_here.php
.
There are a variety of methods you can use to check on which account
PHP is running under, e.g., <?php passthru("whoami"); ?>
,
though some methods may not work on some systems.
[ More Info ]
<?php
$thisfile = pathinfo($_SERVER['PHP_SELF']);
echo "Last modified: ".date("l F j, Y g:i A",
filemtime($thisfile["basename"]));
?>
After I replaced the hard drive recently and installed
CentOS 7 as a fresh install, the times displayed have been ahead 5 hours, i.e.,
the PHP code was displaying a time stamp matching
Universal
Coordinated Time (UTC), also commonly referred to as
Greenwich Mean
Time, rather than
Eastern Standard Time (EST)
I verified the time zone was set correctly at the operating system level with the timedatectl command, so I realized the issue must be with PHP itself. I checked the location of the PHP configuration file, php.ini and found it was located at /etc/php.ini.
$ locate php.ini /etc/php.ini /usr/share/doc/php-common-5.4.16/php.ini-development /usr/share/doc/php-common-5.4.16/php.ini-production
I logged into the root account and checked the contents of the
/etc/php.ini
file. I found that the timezone was not
set in the file:
So I removed the semicolon which was making the date.timezone
line into a comment and set the time zone to be the appropriate one for
Eastern Time (ET).
I then restarted the Apache web server software with the
apachectl restart
command, which fixed the problem.
0
for "false" or "no" and 1
for "true" or
"yes", but want to display the value as "no" when a zero is stored in the
variable and "yes" when a one is stored in the variable, you can print
the value using the ternary operator, ?
, which is
described in the Ternary Operator section of the manual page
Comparison Operators.
E.g., suppose I have an array named swinfo
that has
information on various software packages that has an
array variable Free
that has a 0
stored in it
if the software is not free and a 1
stored in it if the
software is free. If I have a software package that isn't free, if I just
print the contents of the variable as in the first instance below, I get a
zero, but by using ther ternary operator, i.e., the ?
, I can
specify that I want "yes" or "no" displayed, instead as for the
second instance where it is displayed below.
echo "<table>\n" . "<tr>\n" . "<td><b>Free:</b></td><td>" . $swinfo['Free'] . "</td>" . "</tr>\n" . "<tr>\n" . "<td><b>Free:</b></td><td>" . ($swinfo['Free'] ? 'yes' : 'no') . "</td>" . "</tr>\n" . "</table>\n";
For an instance where the software isn't free, I would see the following displayed:
Free: | 0 |
Free: | no |
References:
print_r
.
E.g., suppose that the information to be displayed is stored in a database
in a table called tests
that has student test scores with
each student identified by a student id. The following PHP code could be
used to fetch the test information for a student whose id is stored in the
variable $num
and then print the entire array named
testinfo
.
$result1 = mysql_query("SELECT * FROM tests WHERE ID = $num");
$testinfo = mysql_fetch_array($result1);
print_r ($testinfo);
References: