Product name in 5 versions

hi all

how can i echo the product name

samsung galaxy j5 white 16gb

in 5 different versions like

samsung
samsung galaxy
samsung galaxy j5
samsung galaxy j5 white
samsung galaxy j5 white 16gb

vineet

Easy.

echo "samsung\n";
echo "samsung galaxy\n";
echo "samsung galaxy j5\n";
echo "samsung galaxy j5 white\n";
echo "samsung galaxy j5 white 16gb\n";
1 Like

Hi @vinpkl. Is there more to your question than this? Could you please clarify it?

In other words, is this just an example of something that is dynamically produced and you want a general php solution for echoing the five version types that you listed? Or was it just as simple as @ahundiak understood, and you just wanted that specific phrase echoed?

Hi WebMachine

i have over 1000 products in the database.

i want break all product names in 5 versions dynamically not manually

if product name is of 6 words then it should break up in 6 versions similar to above format of one to six words

vineet

Try this:

    $src  = 'samsung galaxy j5 white 16gb this is a test to see if it works';
    $aSrc = explode(' ', $src);
    $sLine = '';
    foreach($aSrc as $iNotUsed => $src):        
      $sLine .= $src .' '; 
      echo "<br>". $sLine;
    endforeach;    


**Output:** >samsung samsung galaxy samsung galaxy j5 samsung galaxy j5 white samsung galaxy j5 white 16gb samsung galaxy j5 white 16gb this samsung galaxy j5 white 16gb this is samsung galaxy j5 white 16gb this is a samsung galaxy j5 white 16gb this is a test samsung galaxy j5 white 16gb this is a test to samsung galaxy j5 white 16gb this is a test to see samsung galaxy j5 white 16gb this is a test to see if samsung galaxy j5 white 16gb this is a test to see if it samsung galaxy j5 white 16gb this is a test to see if it works
2 Likes

Thanks a lot John_Betong

its perfect

vineet

1 Like

hi John_Betong

is it possibe to return the result in reverse order also like below

full lengthy string at top
and last single string at bottom

samsung galaxy j5 white 16gb this is a test to see if it works
samsung galaxy j5 white 16gb this is a test to see if it
samsung galaxy j5 white 16gb this is a test to see if
samsung galaxy j5 white 16gb this is a test to see
samsung galaxy j5 white 16gb this is a test to
samsung galaxy j5 white 16gb this is a test
samsung galaxy j5 white 16gb this is a
samsung galaxy j5 white 16gb this is
samsung galaxy j5 white 16gb this
samsung galaxy j5 white 16gb
samsung galaxy j5 white
samsung galaxy j5
samsung galaxy
samsung

vineet

I am not at my computer at the moment and will have a go tomorrow.

Basically within the for next loop append the item to a $newArray = $src

After the array, create another for-next loop from count( $newArray ) down to zero.

Echo the item $src

Have fun and post your script :slight_smile:

Hi John_Betong

i tried the below code but everytime it go into infinite loops

<?php
$src  = 'samsung galaxy j5 white 16gb this is a test to see if it works';
    $aSrc = explode(' ', $src);
    $sLine = '';
    foreach($aSrc as $src): 
    $newArray[] = $src;
    $cntArray = count($newArray);
    for($i=$cntArray;$i >0;$i--) {       
      $sLine .= $src .' ';      
      echo "<br>". $sLine;
      }
    endforeach;

?>

and

<?php
$src  = 'samsung galaxy j5 white 16gb this is a test to see if it works';
    $aSrc = explode(' ', $src);
    $sLine = '';
    foreach($aSrc as $src): 
    $newArray[] = $src;
    $cntArray = count($newArray);
    for($i=14;$i >$cntArray;$i--) {       
      $sLine .= $src .' ';      
      echo "<br>". $sLine;
      }
    endforeach;

?>

1 Like

Try this:

  // IF AND ONLY IF PHP 7
     declare(strict_types=1);
  // OK FOR ALL PHP VERSIONS
     error_reporting(-1); 
     ini_set('display_errors', 'true');

  // SET VARIABLES
     $src   = 'samsung galaxy j5 white 16gb this is a test to see if it works';
     $aSrc  = explode(' ', $src);
     $sLine = '';

  echo '<br>FORWARDS';
      foreach($aSrc as $iNotUsed => $src):        
        $sLine .= $src .' '; 
        echo "<br>". $sLine;
        $newArray[] = $sLine; // ADDED
      endforeach;

  echo '<br><br>BACKWARDS';
        $cntArray = count($newArray) -1;
        for($i=$cntArray; $i >=0; $i--)
        {       
          $sLine = $newArray[$i];      
          echo "<br>". $sLine;
        }


**Output:** [quote]

FORWARDS
samsung
samsung galaxy
samsung galaxy j5
samsung galaxy j5 white
samsung galaxy j5 white 16gb
samsung galaxy j5 white 16gb this
samsung galaxy j5 white 16gb this is
samsung galaxy j5 white 16gb this is a
samsung galaxy j5 white 16gb this is a test
samsung galaxy j5 white 16gb this is a test to
samsung galaxy j5 white 16gb this is a test to see
samsung galaxy j5 white 16gb this is a test to see if
samsung galaxy j5 white 16gb this is a test to see if it
samsung galaxy j5 white 16gb this is a test to see if it works

BACKWARDS
samsung galaxy j5 white 16gb this is a test to see if it works
samsung galaxy j5 white 16gb this is a test to see if it
samsung galaxy j5 white 16gb this is a test to see if
samsung galaxy j5 white 16gb this is a test to see
samsung galaxy j5 white 16gb this is a test to
samsung galaxy j5 white 16gb this is a test
samsung galaxy j5 white 16gb this is a
samsung galaxy j5 white 16gb this is
samsung galaxy j5 white 16gb this
samsung galaxy j5 white 16gb
samsung galaxy j5 white
samsung galaxy j5
samsung galaxy
samsung
[/quote]

Hi John_Betong

thanks works perfect.

i was inserting forloop inside foreach but it needed to be outside.

thanks again.
work perfect now.
vineet

1 Like

Hi John_Betong

i would like to ask you a question regarding forloop

what should be done in the code that it should not go into infinite loop mode ??

when it goes into infinite loop mode my computer gets hangs ??

as i a not a expert, so sometimes while writing loops if may get wrong and get into infinite mode and then hangs my computer ??

vineet

No simple solution without a example of an infinite loop.

When developing, as shown in post #10, always set maximum error_reporting and display results to the screen.

Also ensure the start, finish and increment are correct. If there is a problem rem out the contents of the for-loop and test. Once OK, remove the rem statements.

If the web-page is taking longer than normal try stopping the page and put in some break-points to debug the problem.

Try using the following debug function:

function  fred($val)
{
  // prettify results
   echo '<pre>' .print_r($val, true) .'</pre>';
}

// example
for( $i=0; $i<42; $i+= 2 )
{
  if ( 20 < $i ) { fred($i); die; }
  echo '<br>' .$i;
}//endfor

echo '<br>should never reach here';

Edit:
My PHP knowledge and expertise is limited and you would be better to create another topic on debugging. Also describe what problems you would like to prevent.

why do you think you have limited knowledge.

atleast you have better knowledge than me.

i dont have any particular problem at the moment because you already have solved it.

i was just asking in general.

thanks for the suggestions.

vineet

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.