1
// 40 characters string combine by 5 different fields
$field1 = apple;
$field2 = orange;
$field3 = pineapple;
$field4 = banana;
$field5 = strawberry;

// fields will be separated with a comma
$string = implode(", ", array_filter(array($field1, $field2, $field3, $field4, $field5)));

// string will be cut off at about 15th characters then make a break line           
$stringList = explode("\n", wordwrap($string , 15));

// 1st rowField takes the 1st line of string
$rowField1 = array_shift($nutritionalList);
$string = implode(" ",$stringList );
$stringList = explode("\n", wordwrap($string , 25));
$rowField2 = "From 1st Row continued" . "\n" . implode("\n", $stringList) . "\n \n";} 

This Output will show:

$rowField1 = "apple, orange"
$rowField2 = "From 1st Row continued \n pineapple, banana, strawberry"

However, my problem is if $field3, $field4, and $field5 are NULL, I don't want to display $rowField2 including the text "From 1st Row continued"

I tried IF/ELSE and ISSET procedure:

if (isset($stringList)) {
   $rowField2 = "From 1st Row continued\n" . implode("\n", $stringList) . "\n\n";
}
else {
   $rowField2 = NULL;
}

But $rowField2 still shows "From 1st Row continued". I want it not to display that if the last 3 fields are NULL.

3
  • Is it deliberate that you wrote e.g. $field1 = apple; rather than $field1 = "apple";? I don't see those constants defined. Commented Jul 5, 2011 at 6:43
  • @Tomalak Geret'kal: sorry about the typo. was re-typing the code as fast as i can to get my question out quick. anyways, shashank's solution worked for me. so i'm happy :) thanks for viewing Commented Jul 5, 2011 at 6:52
  • You can click "edit" above to fix the code in your question. Commented Jul 5, 2011 at 9:37

3 Answers 3

3

Try this it will give output as "apple, orange".

is it fine?

<?php
$field1 = 'apple';

$field2 = 'orange';

$field3 = '';

$field4 = '';

$field5 = '';

// fields will be seperated with a comma

$string = implode(", ", array_filter(array($field1, $field2, $field3, $field4, $field5)));

// string will be cut off at about 15th characters then make a break line

$stringList = explode("\n", wordwrap($string , 15));

// 1st rowField takes the 1st line of string

$rowField1 = array_shift($stringList);

$string = implode(" ",$stringList );

$stringList = explode("\n", wordwrap($string , 25));

$rowField2 = ( isset( $stringList[0] ) && !empty($stringList[0]) ) ? "From 1st Row continued" . "\n" . implode("\n", $stringList) . "\n \n" : '';
echo $rowField1;
echo "<br />";
echo $rowField2;
exit;
?>
Sign up to request clarification or add additional context in comments.

4 Comments

-1: empty doesn't do what you think it does.
Actually I'll take back the -1 as I now notice he's working on an array... but I still wouldn't recommend empty.
yes you are write you can also put $stringList[0] != '' in place of empty.
@Shashank: count($stringList) is more canonical. What if there's an array with non-numeric keys? Or an array with non-contiguous numeric keys that start higher than 0? You're introducing criteria into your code that do not need to be there.
1

I would have used condition :

if( isset($stringList) && count($stringList) > 0 ){
    // blah blah
}

Comments

0

$stringList will always be set, but it won't always have content in it.

Don't use empty because it's not clear from reading it what it does — for one thing, empty("0") is TRUE! — , even though in this case, on an array, it would work.

My recommended approach:

if (count($stringList)) {
   $rowField2 = "From 1st Row continued\n" . implode("\n", $stringList) . "\n\n";
}
else {
   $rowField2 = NULL;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.