Viewed   89 times

I wanted to echo an image every after 3 post via XML here is my code :

// URL of the XML feed.
$feed = 'test.xml';
// How many items do we want to display?
//$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
  die('The XML file could not be found!');
// First, open the XML file.
$xml = simplexml_load_file($feed);
// Set the counter for counting how many items we've displayed.
$counter = 0;
// Start the loop to display each item.
foreach($xml->post as $post) {
  echo ' 
  <div style="float:left; width: 180px; margin-top:20px; margin-bottom:10px;">
 image file</a> <div class="design-sample-txt">'. $post->author.'</div></div>

  // Increase the counter by one.
  // Check to display all the items we want to.
  if($counter >= 3) {
    echo 'image file';
  //if($counter == $display) {
    // Yes. End the loop.
   // break;
  // No. Continue.

here is a sample first 3 are correct but now it doesn't loop



The easiest way is to use the modulus division operator.

if ($counter % 3 == 0) {
   echo 'image file';

How this works: Modulus division returns the remainder. The remainder is always equal to 0 when you are at an even multiple.

There is one catch: 0 % 3 is equal to 0. This could result in unexpected results if your counter starts at 0.

Friday, August 26, 2022
foreach ($x as $key=>$value)
  if (fmod($i,2)) echo '<tr>';
  echo '<td>',$value,'</td>';
  if (fmod($i,2)) echo '</tr>';

this will output TR (row) each second time

ps: i haven't tested the code, so maybe you will need to add ! sign before fmod, if it doesn't output TR on first iteration, but on second iteration in the beginning...

Tuesday, August 9, 2022
$small = substr($big, 0, 100);

For String Manipulation here is a page with a lot of function that might help you in your future work.

Tuesday, August 30, 2022

The following code will look through the two dimensions of the array and make them into a table. Regardless of what the key may be, you will get a visual representation of it. If they do have key name and not just an index, the values will be available in $key and $subkey respectively. So you have them if you need them.

The code:

$myarray = array("key1"=>array(1,2,3,4),
                 "key4"=>array(4,5,6,7)); //Having a key or not doesn't break it
$out  = "";
$out .= "<table>";
foreach($myarray as $key => $element){
    $out .= "<tr>";
    foreach($element as $subkey => $subelement){
        $out .= "<td>$subelement</td>";
    $out .= "</tr>";
$out .= "</table>";

echo $out;

The result:

If you want to see the keys as headings, you could add this code after the echo "<table>"; line:

echo "<tr>";
foreach($myarray as $key => $element) echo "<td>$key</td>";
echo "</tr>";

Resulting in this:

Sunday, October 23, 2022

You could use a different regex pattern delimiter character:

return preg_replace('#.*#',
    'Lorem Ipsum' .
    'More Lorem Ipsum'

EDIT: The delimiter character is a feature of PCRE (Perl Compatible Regular Expresssion). No PHP configuration is needed to use a different delimiter.

Regexp Quote-Like Operators can use any pair of non-alphanumeric, non-whitespace characters as delimiters. This is particularly useful for matching path names that contain "/", to avoid LTS (leaning toothpick syndrome).

Quote and Quote-like Operators

Non-bracketing delimiters use the same character fore and aft, but the four sorts of ASCII brackets (round, angle, square, curly) all nest

These are all valid:

'{.*}' /* Note that '{.*{' would be incorrect. */

Take a look at PHP's documentation on PCRE Patterns to see a really good overview.

Friday, December 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :