I'm cobbling together a website for a committee and i've taken an iso box template and now I want to generate the profile cards from a json file. However when I load the page the php fails to produce the cards but there are no obvious errors in it as far as I am aware.
<?php
$json = json_decode(file_get_contents("data/committee.json"), true);
if ($json !== false) {
foreach ($json as $officer => $content) {
$role = $content["role"];
$name = $content["name"];
$tags = $content["tags"];
echo "<div class='iso-box $tags col-md-3 col-sm-3'>";
echo "<div class='portfolio-thumb'>";
echo "<img src=\"files/committee_pictures/$role.jpg\" class='img-responsive' alt='Portfolio'>";
echo "<div class='portfolio-overlay'>";
echo "<div class='portfolio-item'>";
echo "<a href=mailto:$role.@website.co.uk><i class='fa fa-envelope'></i></a>";
echo "<h2>$officer</h2>";
echo "<p> $name </p>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
It would be very difficult for Javascript and PHP to interact unless you were intentionally making them do so.
PHP is always going to be executed on the server side, and Javascript will typically be processed on the client side, after your PHP has finished.
If you're not seeing any results from your PHP here, then it's definitely an error with your PHP code.
I'd recommend you render $json
and $json !== false
as text to troubleshoot. This variable is definitely either empty, false, or not formatted in the way you think it is, causing an error when you try to loop over it.
You should also figure out where your PHP/server logs are saved, because that's where the output of any errors would be.
but there are no obvious errors
- yeah, because your code actively tries to hide/ignore errors. try instead of hiding errors, throw exceptions whenever an error occur, such code is much easier to debug, errors does not silently propagate (as is happening with your current code), and errors are caught as early as possible, like this:
<?php
$json = json_decode ( file_get_contents ( "data/committee.json" ), true );
if (empty ( $json )) {
throw new \RuntimeException ( 'failed to parse the json!: ' . json_last_error () . ': ' . json_last_error_msg () );
}
foreach ( $json as $officer => $content ) {
foreach ( array (
'role',
'name',
'tags'
) as $required ) {
if (! array_key_exists ( $required, $content )) {
throw new \Exception ( 'invalid data! officer missing ' . $required );
}
${$required} = $content [$required];
}
echo "<div class='iso-box $tags col-md-3 col-sm-3'>";
echo "<div class='portfolio-thumb'>";
echo "<img src=\"files/committee_pictures/$role.jpg\" class='img-responsive' alt='Portfolio'>";
echo "<div class='portfolio-overlay'>";
echo "<div class='portfolio-item'>";
echo "<a href=mailto:$role.@website.co.uk><i class='fa fa-envelope'></i></a>";
echo "<h2>$officer</h2>";
echo "<p> $name </p>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
edit: you're obviously constructing HTML here, and unless role, name, and tags, are already pre-html-encoded inside that json, you should html encode them before putting them into html, this will protects against hackers trying to inject code via $name (or any other variable), and protect against tags accidentally including characters with special meaning in HTML, for example:
function tohtml(string $str): string {
return htmlentities ( $str, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true );
}
...
${$required}=tohtml($content[$required]);