So I want to include this code many times on one page with different data:
if (!empty($faq[$fisf][$title]) ) {
echo '<div class="card card-body col-12"><h5 class="card-title">'.$faq[$fisf][$title].'</h5>';
if (!empty($faq[$fisf][$image]) ) {
echo '<img src="'.$faq[$fisf][$image].'" alt="'.$faq[$fisf][$title].'" class="img-fluid">';
}
if (!empty($faq[$fisf][$title]) ) {
echo $faq[$fisf][$text];
}
echo '</div>';
}
What I normally do is put this code in a separate file and then include it as many times as I need it and depending on where that code is in a loop or foreach it will show the relevant data!.
But I'm trying to figure out a better way of doing it, like using a function!
Kind of like this:
function faqmodal() {
if (!empty($faq[$fisf][$title])) {
echo '<div class="card card-body col-12"><h5 class="card-title">'.$faq[$fisf][$title].'</h5>';
if (!empty($faq[$fisf][$image])) {
echo '<img src="'.$faq[$fisf][$image].'" alt="'.$faq[$fisf][$title].'" class="img-fluid">';
}
if (!empty($faq[$fisf][$title])) {
echo $faq[$fisf][$text];
}
echo '</div>';
}
}
I want this function to run within different foreach's and loading the relevant data to where its run.
faqmodal();
So this part will depend on what the foreach telling it to load it with :
$faq[$fisf][$title]
But now the function does not convert the code above to be relevant whats in the foreach loop is telling it to fill it with!
But this might not be the smartest way of doing it, I'm open for any ideas!
I'm not sure I understood correctly, this should be solvable by passing the data as an argument of the function like this:
function faqmodal($title, $image, $text){
if (!empty($title) && !empty($image) && if (!empty($text)) {
echo '
<div class="card card-body col-12">
<h5 class="card-title">'.$title.'</h5>
<img src="'.$image.'" alt="'.$title.'" class="img-fluid">
'.$text.'</div>'; //you might wanna think about moving the text into e.g. a <span> tag
}
}
If you now want to call the function you'll do it like so:
faqmodal($faq[$fisf][$title], $faq[$fisf][$image], $faq[$fisf][$title]);
And the data you pass will vary according to the data you get via the foreach loop. If this isn't what you mean, please let me know via a comment and we'll work our way through this.