0
$table="<table border='1'>
    <tr>
    <th>FIRST NAME</th>
    <th>LAST NAME</th>
    <th>EMAIL</th>
    <th>GENDER</th>".
    while($row = mysql_fetch_array($result3))
    {
        $action="Action";
        $var=$action.$count;
         ."<th>" .$var. "</th>".
        $count=$count+1;

    } 

    ."        </tr>".

    while($row = mysql_fetch_array($result2))
    {

         ."<tr>".
         "<td>".$row['firstname']."</td>".
         "<td>".$row['lastname']."</td>".
         "<td>".$row['email']."</td>".
        if($row['gender']=='m')
        {
            $gend='male';
        }
        else
        {
            $gend='female';
        }
         "<td>".$gend."</td>".

        $result3=$db->selectUserPermission($uid);
        $id=$row['userid'];
        $loginuser=$_SESSION['LOGINUSER'];
        while($row = mysql_fetch_array($result3))
        {

              ."<td>".
            if($row['permid']==1)
            {
                $permission="VIEW";
                $btnid=$id."_VIEW";
                 ."<button id=$btnid name=$btnid onclick='getFunctionView($id)'>".$permission."</button>".
            }
            else  if($row['permid']==2)
            {
                $permission="EDIT";
                $btnid=$id."_EDIT";
                 ."<button id=$btnid name=$btnid onclick='getFunctionEdit($id)';>".$permission."</button>".
            }
            else  if($row['permid']==3)
            {
                if($id!=1 || $id!=$loginuser)
                {
                $permission="DELETE";
                $btnid=$id."_DELETE";
                 ."<button id=$btnid name=$btnid onclick='getFunctionDelete($id)';>".$permission."</button>".
                }
            }
            else  if($row['permid']==4)
            {
                if($id!=1)
                {
                $permission="PERMISSION";
                $btnid=$id."_PERMISSION";
                 ."<button id=$btnid name=$btnid onclick='getFunctionPermission($id)';>".$permission."</button>".
                }
            }
            else  if($row['permid']==5 && $loginuser==$id)
            {
                $permission="ADD USER";
                 ."<button id=$btnid name=$btnid onclick='location.href=\"adduser.html\"'>".$permission."</button>".
            }
            else
            {
            }

             ."</td>".
        }
    }
     ."</tr>
     </table>";
     echo $table;

This is a code from a php page that is called by ajax.Here $result2 and $result3 are 2 result sets obtained using 2 sql queries.I want generate a table using theese two results and store that table into a php variable $table and in ajax ,i want to assign it directly as a html text,ajax code is shown below.

   $(document).ready(function(){

    var temp='getUser';
    $.ajax ({
                type:'GET',
                url: 'listdetails.php',
                data:{ud:temp},
                success:function(data)
                        {
                            document.getElementByID("userList").innerHTML=data;
                        }

           });

});

And my problem is in creating php variable $table.It is showing error in every concatination( "." ).this is the error message "Parse error: syntax error, unexpected T_WHILE in C:\wamp\www\listdetails.php on line 39".Can any one suggest a good method for making php variable $table.and remember that this $table is in php page called by ajax.

3 Answers 3

3

You're using the concatenations wrong.

  • Don't concatenate a variable assignment ($someVar = ) with a conditional (if) or loop (while) statement
  • Don't put a . after a ; or } because these characters terminate the previous command and you can't start the next command with .

Here's a reworked version of the first part of your code to get you started:

$table="<table border='1'>
<tr>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>EMAIL</th>
<th>GENDER</th>";

while($row = mysql_fetch_array($result3))
{
    $action="Action";
    $table.="<th>" .$action.$count. "</th>";
    $count++;
} 

$table .= "        </tr>";
Sign up to request clarification or add additional context in comments.

1 Comment

you are still using a . in the loop
0
$sTable = '
<table>
 <tr>
  <td></td>
 </tr>';

while( false ) {
 $sTable .= '';
}

$sTable .= '
 </table>';

echo $sTable;

Or as I would do

$sBuffer = '';
while( code goes here ) {
 $sBuffer .= '
 <tr>
  <td></td>
 </tr>';
}


echo '
<table>
 <tr>
  <td></td>
 </tr>
 ' . $sBuffer . '
</table>';

Comments

0

Considering all you want to do is echo the table, I would end/start PHP only when I need it. The cleaned code is below.

<table border='1'>
<tr>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>EMAIL</th>
<th>GENDER</th>
<?php
while($row = mysql_fetch_array($result3))
{
        $action="Action";
        $var=$action.$count;  //$count is not defined yet in your example, first time will be ''
        echo "<th>" .$var. "</th>";
        $count=$count+1;
}
?>
</tr>

<?php
while($row = mysql_fetch_array($result2))
{
    ?>
    <tr>
    <td><?php echo $row['firstname']; ?></td>
    <td><?php echo $row['lastname']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td><?php echo (($row['gender']=='m') ? 'male' : 'female'); ?></td>

    <?php
    $result3=$db->selectUserPermission($uid);
    $id=$row['userid'];
    $loginuser=$_SESSION['LOGINUSER'];
    while($row = mysql_fetch_array($result3))
    {
        echo "<td>";

        switch ($row['permid']) {
            case 1:
                $permission="VIEW";
                $btnid=$id."_VIEW";
                echo "<button id=$btnid name=$btnid onclick='getFunctionView($id)'>".$permission."</button>";
                break;
            case 2:
                $permission="EDIT";
                $btnid=$id."_EDIT";
                echo "<button id=$btnid name=$btnid onclick='getFunctionEdit($id)';>".$permission."</button>";
                break;
            case 3:
                if($id!=1 || $id!=$loginuser)
                {
                    $permission="DELETE";
                    $btnid=$id."_DELETE";
                     echo "<button id=$btnid name=$btnid onclick='getFunctionDelete($id)';>".$permission."</button>";
                }
                break;
            case 4:
                if($id!=1)
                {
                    $permission="PERMISSION";
                    $btnid=$id."_PERMISSION";
                    echo "<button id=$btnid name=$btnid onclick='getFunctionPermission($id)';>".$permission."</button>";
                }
                break;
            case 5:
                if($loginuser==$id)
                {
                    $permission="ADD USER";
                    echo "<button id=$btnid name=$btnid onclick='location.href=\"adduser.html\"'>".$permission."</button>";
                }
                break;
        }

        echo "</td>";
    }
}
?>
</tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.