mrpatg
mrpatg

Reputation: 10117

Looping through an array of arrays, changing output on a given line(s)

This is what im using to loop through an array of arrays.

$csvpre = explode("###", $data);

$i = 0;
$bgc = 0;

    foreach ( $csvpre AS $key => $value){
        $info = explode("%%", $value);
        $i++;
        if($i == "1"){
            echo "<tr bgcolor=#efefef><td></td>";
                foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
            echo "</tr>";

        } else {

            if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
            echo "<tr $bgcgo><td></td>";
                foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
            echo "</tr>";
            $bgc++;
        }       
    }

How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array. Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>

Upvotes: 0

Views: 73

Answers (1)

elviejo79
elviejo79

Reputation: 4600

You mean like this:

<?php 
.......
echo "<tr $bgcgo><td></td>";
$j = 0;  //you need a counter
foreach ( $info as $key => $value ) { 
    $j++;  
    if ($j != 30) {
        echo "<td>$value</td>"; 
    } else {
        echo "<textarea>$value</textarea>";
    }
}
echo "</tr>";

Upvotes: 1

Related Questions