prf
prf

Reputation: 47

explode then implode then show in table with checkbox as checked

I have a column in SQL which I uses odbc to connect with that has a data in a format like this

1| 2| 3| 4| 5

each number states the id of each worker. This question by the way is connected to my previous question. See link below

Editing an imploded string

So If I select that column, it would return an output like the one above. In editing, I need to explode it first and then implode it as a some sort of string like so

1,2,3,4,5

Now, the question is how will I create a loop which will select the firstname and lastname of the worker based on each sting inside the string(1,2,3,4,5) and then echo a table in which their respective checkbox will be checked if their ID is included on the list. Here's my code for this,

$que = "SELECT assignedto FROM PROJECTS where projectname = '$projname'";
$queres = odbc_exec($conn,$que);
$res = odbc_result($queres, 1);
$analysts_arr = explode("| ", $res);

$analysts = implode(",", $analysts_arr);
//die($analysts);
$que2 = "DECLARE @s VARCHAR(MAX)
         DECLARE @d VARCHAR(MAX)

         SET @s='$analysts'

         set @d = 'select id, firstname, lastname from Accounts where id in('+@s+')'
         exec (@d)";
//die($que2);
$query = odbc_exec($conn,$que2);
echo odbc_result_all($query);
$result = odbc_result_all($query);
echo "<table class='table table-striped table-bordered table-hover' id='dataTables-example' style='margin-top:10px;'>
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Last Name</th>
                                        <th>First Name</th>
                                    </tr>
                                </thead>;";
foreach($result as $val){
   $user_id = $val['id'];
   $users[$id] = $val;

        echo "<tbody>
            <tr class='odd gradeX'>
                <td><input type='checkbox' value='<?php echo odbc_result($queres, 1); ?>' name='analyst[]'/></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>";
}

My code returns an error show below

Warning: odbc_result_all(): No tuples available at this result index

Warning: odbc_result_all(): No tuples available at this result index ;

Warning: Invalid argument supplied for foreach()

Upvotes: 0

Views: 528

Answers (1)

prf
prf

Reputation: 47

Found a work around for my problem. Posting it for other that it may serve as a guide to them as well.

Here's the workaround I did.

$que = "SELECT assignedto FROM PROJECTS where projectname = '$projname'";
$queres = odbc_exec($conn,$que);
$res = odbc_result($queres, 1);
$analysts_arr = explode("| ", $res);
//print_r($analysts_arr);
echo "<table class='table table-striped table-bordered table-hover' id='dataTables-example' style='margin-top:10px;'>
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Last Name</th>
                                        <th>First Name</th>
                                    </tr>
                                </thead>";
foreach($analysts_arr as $analysts) {
    //foreach($analysts_arr as $idan){
        $quean = "SELECT lastname,firstname FROM ACCOUNTS where id = '$analysts'";
        $queresan = odbc_exec($conn,$quean);
        $resas = odbc_result($queresan, 1);
        $resasf = odbc_result($queresan, 2);
        echo "<tbody>
            <tr class='odd gradeX'>
                <td>";
                echo $analysts;
                echo "</td>
                <td>";
                echo $resas;
                echo "</td>
                <td>";
                echo $resasf;
                echo "</td>
            </tr>
        </tbody>";
    //}

}

Upvotes: 1

Related Questions