JDV590
JDV590

Reputation: 651

PHP dynamically build table row

EDIT: I call the function writecol() further down the page inside <table></table> tags.

Example of data in trxtt.txt:

South East asia,2222,code1

winter break,3333,code2

I am fairly new to php. I am trying to dynamically build table rows based off the variables read from an array. When I call this function I do not receive and error message, but nothing happens. Any ideas what I am doing wrong?

$x = file_get_contents('textt.txt');
$y = explode("\r\n", $x);

function writecol(){
    foreach ($y as $value) {
        $z = explode(",", $value);
        echo "<tr class='visible'><td class='underlinecenter'>" . $z[0] . "</td> <td></td> <td colspan='3' class='underlinecenter'>" . $z[1] . "</td><td></td><td colspan='3' class='underlinecenter'>" . $z[2] . "</td></tr>";
    }   
}

Upvotes: 0

Views: 483

Answers (4)

mgraph
mgraph

Reputation: 15338

You have to call the writecol() function

Upvotes: 0

craniumonempty
craniumonempty

Reputation: 3535

first make sure you have these on (put them at the top under the php tag) for testing so you can see errors

ini_set('display_errors','On'); 
ini_set('error_reporting', -1);

Beyond errors, you aren't calling the function. change your function and add a call:

function writecol($y){ # <-- pass a variable and call it $y
    foreach ($y as $value) {
        $z = explode(",", $value);

        echo "<tr class='visible'><td class='underlinecenter'>" . $z[0] . "</td> <td></td> <td colspan='3' class='underlinecenter'>" . $z[1] . "</td><td></td><td colspan='3' class='underlinecenter'>" . $z[2] . "</td></tr>";
    }   
}
writecol($y); #<-- function call sending the variable $y

Upvotes: 0

Till Helge
Till Helge

Reputation: 9311

Well...you never actually call the function. Therefore PHP just doesn't know that you intend it to use the function on the array you created. Additionally, you should add a parameter to your function, because from within writecol() your variable $y will not be visible.

Try it like this:

$y = explode(...);
function writecol($array) {
    foreach ($array as $value) { // your code }
}
writecol($y);

Upvotes: 0

djdy
djdy

Reputation: 6919

You don't appear to be calling the function, nor is your function ready to receive the variable with data.

after $y = explode.... insert: writecol($y);

Then replace function writecol(){ with

function writecol($y){

Upvotes: 2

Related Questions