1

I have an if statement here it works perfect for what I want it to do. It loops through some values e.g. sop_01 to sop_06 and writes their output separated with a br. I would like to know if this is the most efficient way of writing this code as to me it does not seem very efficient e.g. what happened if the values went from sop_01 to sop_1000 you would not write this out manually?

if (TRIM($row['sop_01']) <> null){
    $sop = TRIM($row['sop_01']);
    if (TRIM($row['sop_02']) <> ""){
        $sop = $sop . "<br>" . TRIM($row['sop_02']);
        if (TRIM($row['sop_03']) <> ""){
            $sop = $sop . "<br>" . TRIM($row['sop_03']);
            if (TRIM($row['sop_04']) <> ""){
                $sop = $sop . "<br>" . TRIM($row['sop_04']);
                if (TRIM($row['sop_05']) <> ""){
                    $sop = $sop . "<br>" . TRIM($row['sop_05']);
                    if (TRIM($row['sop_06']) <> ""){
                        $sop = $sop . "<br>" . TRIM($row['sop_06']);
                    }
                }
            }
        }
    }
} else { $sop = "hello world"; }

A little background info; if sop_01 is null then all the other values will be null

if sop_01 is <> null there is a possibility the other values are either “” or have a value

if sop_02 is empty it is not null it is "" (Due to how the data is stored in the database)

Please let me know if I can provide any further information

Bepster

3
  • What is the data type of $row['sop_01']? string? Commented Aug 26, 2016 at 8:24
  • 1
    It would be more efficient if your array was a nested array: $row['sop'][1], etc. If this is data from a datbase, then it's from a badly designed/unormalized database Commented Aug 26, 2016 at 8:34
  • Yes. That would be better to use nested arrays in this case. Commented Aug 26, 2016 at 8:35

4 Answers 4

2

First create a array containing all keys to use. Then manipulate the array this way:

 // create array with "allowed" keys, btw. you can convert it into a loop :)
 $keys = array_flip(array('sop_01', 'sop_02', 'sop_03', 'sop_04', 'sop_05', 'sop_06'));
 // take only items with keys of array $keys
 $sop = array_intersect_key($row, $keys);
 // will call the function `trim` on every item of the array
 $sop = array_map('trim', $sop);
 // will remove all empty strings
 $sop = array_filter($sop);
 // will join all items of the array with a `<br>` between them.
 $sop = implode('<br>', $sop);

if you need this Hello world string if row is "empty" you could add this line:

 $sop = empty($sop) ? 'Hello world' : $sop;

To create the $keys array with a loop use this

$keys = array();
$i = 1;
while(isset($row['sop_'.$i])){
  $keys['sop_'.$i++] = true;
}

It will create a array, depending on how many "fields" matching the pattern sop_%d (in a row starting from 1) part of the $row array. (Is this sentence correct?)

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to work perfectly in the smallest amount of code however the $row array contains other values and I just want to take the values for sop_01, sop_02 etc... Is there any way to only select these?
2

You can try this with some inbuilt PHP functions -

$row = array_map('trim', $row); // trim all the values present
if ($row['sop_01'] <> null) {

    $temp = array_filter($row); // will remove all the empty, null ...

    $sop = implode('<br>', $temp); // concatenate the values with <br>

} else { 
    $sop = "hello world"; 
}

assuming $row would contain those values (strings) only. If not you can store them in an array and do the rest.

array_filter(), array_map()

Comments

2

Use for loop and built index dynamically.

$start = 1;
$end = count($row);
$sop = '';
for ($i = $start; $i <= $end; $i++) {
    $num = ($i < 10) ? '0' . $i : $i;
    $index = 'sop_' . $num;
    if (TRIM($row[$index]) <> "") {
        $sop = $sop . "<br>" . TRIM($row[$index]);
    }
}

1 Comment

Make $end the value of count($row); to make it easier to add more rows
0

this code will work for 1000 or more values properly -

<?php
$rows = count($row);
$i  = 2;
$sop = "";
if (TRIM($row['sop_01']) <> null){
    $sop = $sop . TRIM($row['sop_01']);
    get_soap($rows, $i, $sop);
} else { $sop = "hello world"; }

function get_soap($rows, $i, $sop){

    if($i <= $rows){

        if (TRIM($row['sop_'.$i]) <> "")
            $sop = $sop . "<br>" . TRIM($row['sop_'.$i]);

        $i++;
        get_soap($rows ,$i, $sop);
    }  
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.