Asked  7 Months ago    Answers:  5   Viewed   32 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.

Wednesday, March 31, 2021
answered 7 Months ago

Here's another way.

This function increments in base( [number of elements in array] )

and uses the strtr function to swap out the characters for strings.

function everyCombination($array) {

    $arrayCount      = count($array);
    $maxCombinations = pow($arrayCount, $arrayCount);
    $returnArray     = array();
    $conversionArray = array();

    if ($arrayCount >= 2 && $arrayCount <= 36)
        foreach ($array as $key => $value) {
            $conversionArray[base_convert($key, 10, $arrayCount)] = $value;

        for ($i = 0; $i < $maxCombinations; $i++) {
            $combination    = base_convert($i, 10, $arrayCount);
            $combination    = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
            $returnArray[]  = strtr($combination, $conversionArray);

        return $returnArray; 

    echo 'Input array must have between 2 and 36 elements';

Then ...

print_r(everyCombination(array('a', 'b', 'c', 'd')));

This also seems to be significantly faster than the recursive example below.

Using microtime() on my server this code runs in 0.072862863540649 seconds

The recursive example below takes 0.39673089981079 seconds.

138% faster!

Wednesday, March 31, 2021
answered 7 Months ago

In the end this is the solution I went for. This solution includes a statement to close the tags if there are a number of items not divisible by four, for example if there are five items in total. Thanks to everyone, I couldn't have done it without your input.

            foreach ($rows as $id => $row)
                    if ($count % 4 == 0)
                        echo "<ul>";

                    echo '<li>' . $row . '</li>';

                    if ($count % 4 == 3 || $count == count($rows)-1)
                        echo "</ul>";
Saturday, May 29, 2021
answered 5 Months ago
$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.

Thursday, June 17, 2021
answered 5 Months ago

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:

enter image description here

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:

enter image description here

Monday, August 2, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :