I have a longwinded PHP file (see gist) which looks through a specific array (declared in another PHP file) named day
.
The value of day
is a folder name and the value of reportArray
contains a subfolder of day
.
If the folder day
is not empty, and the reportArray
is also not empty, I made the code to dsiplay a div
tag in HTML
which contains some information about that report
.
reportArray
changes to reportArrayD2
on day[1]
for Day 2 etc.
The <style>
tag only gets called once at `day[0] in the if statements as I only need it once.
Could someone please show me how to refactor this code? The functionality is there but I feel my current solution is very messy and unnecessarily long.
Here's an example of one of my 5 if
statements:
if (empty($day[0]))
{
echo "
<section class=\"suite-wrap\">
<div class=\"suite has-suites\">
<div class=\"holder\">
<h1>Error: Folder Missing for Day 1</h1>
</div>
</<div>
</section>
";
}
else
{
echo "
<section class=\"suite-wrap\">
<div class=\"suite has-suites\">
<style>
h1
{
padding-left: 15px;
flex-grow: 1;
}
h2
{
flex-grow: 1;
text-align: right;
padding-right: 15px;
}
h5
{
flex-grow: 1;
padding-left: 15px;
width: 240px;
}
.holder
{
display: flex;
}
.failedReportsFilter
{
flex-grow: 1;
width:382.25px;
}
.passedPercentage
{
flex-grow: 1;
text-align: center;
}
.reportResults
{
text-align: center;
width: 80px;
}
.percentPassed
{
padding-left: 0px;
text-align: center;
width: 240px;
}
.report
{
flex-grow: 4;
}
.duration
{
flex-grow: 1;
text-align: center;
padding-right: 15px;
}
.testsRan
{
flex-grow: 1;
text-align: right;
width: 240px;
}
#passedBar$day[0]
{
flex-grow: 4;
padding-top: 15px;
}
</style>
<div class=\"holder\">
<h1>Day 1 - " . $day[0] . "</h1>
<div id=\"passedBar$day[0]\">
<div class=\"barWrapper\">
<span class=\"progressText\"><B id=\"passRate$day[0]\"></B></span>
<div class=\"progress\">
<div id=\"progress-bar$day[0]\" class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"\" aria-valuemin=\"0\" aria-valuemax=\"100\" >
<span class=\"popOver\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"85%\"> </span>
</div>
</div>
<div class=\"col-md-2 col-lg-2\"></div>
</div>
</div>
<h2 id=\"filterFailedTests$day[0]\"class=\"failedReportsFilter\"></h2>
</div>
<section class=\"suite-wrap\">
<div class=\"suite has-tests\">";
if (empty($reportArray))
{
echo "
<style>
#testError
{
padding-left: 15px
}
</style>
<div id=\"testError\">Error: No Tests Found</div>
";
}
else
{
for ($i = 0; $i < count($reportArray); $i++)
{
$htmlFilesArray = array();
foreach (glob("reports/".$day[0]."\\" . $reportArray[$i] . "\\*.html") as $htmlFile)
{
$htmlFilesArray[] = $htmlFile;
}
for ($x = 0; $x < count($htmlFilesArray); $x++)
{
echo "<div class=\"holder\">
<h5 class=\"report\"><a id=\"report".$i."\" href=\"" . $htmlFilesArray[$x] . "\">" . $report[$i] . "</a></h5>
<h5 id=\"testsRanResult$day[0].$i\"class=\"testsRan\"></h5>
<h5 id=\"percentResults$day[0].$i\" class=\"percentPassed\"></h5>
<h5 id=\"result$day[0].$i\" class=\"reportResults\"></h5>
<h5 id=\"durationResult$day[0].$i\"class=\"duration\"></h5>
</div>";
}
}
}
echo "
</div>
</section>
</div>
</section>
";
}