0

simple question, I think.

I have the following pdo statements:

$sth = $db->prepare("SELECT person,job FROM orgstructure where department=:dept order by id asc"); 
$sth->bindParam(':dept', $departmentname, PDO::PARAM_STR); 
$sth->execute(); 
$dataArray = $sth->fetchAll();
echo $dataArray[0]['person'];
echo $dataArray[1]['job'];

the two echos are to test, which work 100%.

Later on in my form, I have a for loop in whcih I provide select box options, based on the outputs of the pdo, as below:

<table>
<?
    for ( $i = 1; $i <=50; $i++ ) { 

        ?>      
            <tr>
                <td> Job <? echo  $i; ?></td>
                <td>
                    <SELECT NAME=job<? echo $i; ?> id=job<? echo $i; ?> style="width:150px;border: 1px solid #2608c3;color:red"> 
                    <? echo  "<option selected>".$dataArray[$i]['job']."</option>"; ?>
                    <option>
                    <?=$optionjobs?> 
                    </option>
                    </SELECT>
                </td>
                <td> Person </td>
                <td>
                <? echo $i."person: ".$dataArray[$i]['person']."job: ".$dataArray[$i]['job']; ?>

                    <SELECT NAME=person<? echo $i; ?> id=person<? echo $i; ?> style="width:150px;border: 1px solid #2608c3;color:red">
                    <? echo  "<option selected>".$dataArray[$i]['person']."</option>"; ?>               
                    <option>
                    <?=$optionpersons?> 
                    </option>
                    </SELECT>
                </td>
            </tr>
        <?
            }
        ?>
</table>

This however does not work. the select statement is correct and works if I replast the $dataarray with text.

I am assuming the issue is with the $i counter being used inside the $dataArray? any suggestions?

Thanks as always,

8
  • 1
    Can you expand on "doesn't work"? Commented Nov 20, 2012 at 17:22
  • do you have up to 50 rows in that array? Commented Nov 20, 2012 at 17:44
  • @codingbiz, yes, 50 rows always returned in mysql query result, it must be said though that some of the values are null, but 50 rows in mysql output. Commented Nov 20, 2012 at 18:31
  • @BenGriffiths, thanks for your time. <? echo "<option selected>".$dataArray[$i]['person']."</option>"; doesnt output anything, it is blank. same goes for jobs field. Thanks again, Commented Nov 20, 2012 at 18:32
  • and what is the error? have you looked at the generated html if there is any error or malformation? Commented Nov 20, 2012 at 18:32

1 Answer 1

1

Try this code, worked on my machine

<?php
    for ( $i = 1; $i <=50; $i++ ) { 

        ?>      
            <tr>
                <td> Job <?php echo  $i; ?></td>
                <td>
                    <SELECT NAME=job<?php echo $i; ?> id=job<?php echo $i; ?> style="width:150px;border: 1px solid #2608c3;color:red"> 
                    <?php echo  "<option selected>".$dataArray[$i]['job']."</option>"; ?>
                    <option>
                    <?php echo $optionjobs ?> 
                    </option>
                    </SELECT>
                </td>
                <td> Person </td>
                <td>
                <?php echo $i."person: ".$dataArray[$i]['person']."job: ".$dataArray[$i]['job']; ?>

                    <SELECT NAME=person<?php echo $i; ?> id=person<?php echo $i; ?> style="width:150px;border: 1px solid #2608c3;color:red">
                    <?php echo  "<option selected>".$dataArray[$i]['person']."</option>"; ?>               
                    <option>
                    <?php echo $optionpersons ?> 
                    </option>
                    </SELECT>
                </td>
            </tr>
        <?php
            }
        ?>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks codingbiz, 100%. appreciate your time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.