Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code will work:</p> <pre><code>&lt;?php $database = array( "item1" =&gt; "banana", "item2" =&gt; "apple", "item3" =&gt; "cherry", ); $output = '&lt;html&gt; &lt;body&gt;'; for($i=1;$i&lt;500;$i++) //change 500 with the desired number or setup a dynamic variable { if(isset($database['item'.$i])) //if the current item is set { $output .= '&lt;span class="item"&gt;Description for item '.$i.': &lt;/span&gt;&lt;span class="content"&gt;'.$database['item'.$i].'&lt;/span&gt;'; //append to $output $output .= '&lt;br /&gt;'; //Here I add a line break, remove if you don't want } } $output .= '&lt;/body&gt;&lt;/html&gt;'; echo $output; ?&gt; </code></pre> <p>You'll see that if an item is not present in the $database array, it won't be printed out. Of course you can remove the $database array at the beginning of the code (here use for test purposes) and use your own generated $database array.</p> <p>In your particular case: if you want to develop your own template engine, you could use special strings (that you invent) in the template (e.g. <em>{--ITEMS--}</em>) and then process those strings in a second PHP file that reads the template, finds the strings and replaces them with some output. To do this the main point you are missing in the code you posted here is the "construction" of the output that will replace your fixed strings in the template. You can construct the output by processing your database and appending the results ( using <strong>.=</strong> ) to a variable that <em>just at the end</em> is printed out or can be used to replace your fixed string.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload