Hi all am am trying to reduce duplicated elements in a php page. How can I create a function that will pass the different values in the array.

Thanks in advance

D

       for ($i=$index-$x+1; $i<=$index; $i++)
           {
               echo "<div class='partner_description' id='partner_description_" .$partners_id[$i]. "'>";
                echo "<p>".$description_value[$i]. "</p>";
                echo "<div class='url'>";
                echo "<a href='http://".$url_value[$i]."' target='blank'>".$url_value[$i]." </a></div>";
                echo "</div>";
            }

Can you explain a little more about the values you want to pass?

Hi Pritaeas The echo statments are from the db but where the value differ is in the duplicatation side is passing data for loop($i=$index-$x+1; $i<=$index; $i++)

This is the first call to the for loop

   for ($i=$index-4; $i<=$index; $i++)
       {
          echo "<div class='partner_description' id='partner_description_" .$partners_id[$i]. "'>";
          echo "<p>".$description_value[$i]. "</p>";
          echo "<div class='url'>";
          echo "<a href='http://".$url_value[$i]."' target='blank'>".$url_value[$i]." </a></div>";
          echo "</div>";
       }

and this is the second use of the for loop

for ($i=$index-$x+1; $i<=$index; $i++)
           {
               echo "<div class='partner_description' id='partner_description_" .$partners_id[$i]. "'>";
                echo "<p>".$description_value[$i]. "</p>";
                echo "<div class='url'>";
                echo "<a href='http://".$url_value[$i]."' target='blank'>".$url_value[$i]." </a></div>";
                echo "</div>";
            }

As you can see the two are identical except for the for
loop syntax for ($i=$index-$x+1; $i<=$index; $i++)

I am looking to get away from the repetive use of code.

Thanks in advance

D

Oh, just put that code in a function, and turn the variables into parameters:

function partnerDescription($partner, $description, $url)
{
    $result = "<div class='partner_description' id='partner_description_{$partner}'>";
    $result .= "<p>{$description}</p>";
    $result .= "<div class='url'>";
    $result .= "<a href='http://{$url}' target='blank'>{$url}</a></div>";
    $result .= "</div>";
    return $result;
}

Call it inside the loop like this:

echo partnerDescription($partners_id[$i], $description_value[$i], $url_value[$i]);

Hi Pritaeas thanks for thew great solution it was very much appreiciated. Just one other question on functions based on the following validation method:

If I were to collect the following data via request and pass this through a function would I need to declare this as a global variable? or change this if (!isset($_REQUEST['partner_name']) to if (!isset(partner_name)

Thanks in advance

D

if (isset($_REQUEST['partner_name']))
             $partner_name_error=($_REQUEST['partner_name'])


<?php
    function partnererror($partner_error)
        {
           if (isset($partner_error) && isset($_REQUEST['submit']))
              {
                 if (!isset($_REQUEST['partner_name']) ||  empty($_REQUEST['partner_name']))
                          {
                              $partner_error = "Please enter a partner name";
                              echo "<span class='mandatory'>".$partner_error."</span>";
                           }
              }
       }
 ?>

REQUEST already is a global variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.