0

here is my code

<?php 
while($rowcall=mysql_fetch_array($qry_call))
{
?>
  <table>
  <tr>
  <td><?php echo $rowcall['caller_id'];?></td>
  <td><?php echo $rowcall['did']; ?></td>
  <td><?php echo $rowcall['start_time']; ?></td>
  <td><?php echo $rowcall['end_time']; ?></td>
  <td><?php echo $rowcall['call_duration']; ?></td>
  </tr>
 </table> 
 <?php } ?>

I need to store this table in php variable $result.

3
  • Did you tried something?
    – Konsole
    Commented Sep 6, 2013 at 5:59
  • do concatenation using . operator
    – Kalpit
    Commented Sep 6, 2013 at 6:01
  • Interesting that no Stop Using mysql_* comments here yet, but really you should :) Commented Sep 6, 2013 at 6:19

7 Answers 7

4
<?php

  $result = '';
  while($rowcall=mysql_fetch_array($qry_call))
  {
     $result .= '<table>';
     $result .= '<tr>';
     $result .= '<td>'.$rowcall['caller_id'].'</td>';
     $result .= '<td>'.$rowcall['did'].'</td>';
     $result .= '<td>'.$rowcall['start_time'].'</td>';
     $result .= '<td>'.$rowcall['end_time'].'</td>';
     $result .= '<td>'.$rowcall['call_duration'].'</td>';
     $result .= '</tr>';
     $result .= '</table>';
  }

  echo $result;

?>
5
  • With this code you would generate a seperate table for every element you get from the database Commented Sep 6, 2013 at 6:05
  • Yep, I know. But that's what @user2732605 want if I see his question Commented Sep 6, 2013 at 6:07
  • 1
    @BjornSmeets thats what the OP has asked, right? "To store mysql row result as table format in php variable"
    – rakeshjain
    Commented Sep 6, 2013 at 6:07
  • You are right, my bad. I misunderstood the question and thought he wanted 1 big table with all the data as rows inside that table Commented Sep 6, 2013 at 6:09
  • Exactly @rakeshjain . Commented Sep 6, 2013 at 6:09
1

Pretty basic PHP;

<?php 
$result="";
while($rowcall=mysql_fetch_array($qry_call))
{
    $result.="  <table>";
    $result.="  <tr>";
    $result.="  <td>".$rowcall['caller_id']."</td>";
    $result.="  <td>".$rowcall['did']."</td>";
    $result.="  <td>".$rowcall['start_time']."</td>";
    $result.="  <td>".$rowcall['end_time']."</td>";
    $result.="  <td>".$rowcall['call_duration']."</td>";
    $result.="  </tr>";
    $result.=" </table>"; 
}
?>
0
1
    *<?php

  $result = '';
  while($rowcall=mysql_fetch_array($qry_call))
  {
     $result .= '<table>';
     $result .= '<tr>';
     $result .= '<td>'.$rowcall['caller_id'].'</td>';
     $result .= '<td>'.$rowcall['did'].'</td>';
     $result .= '<td>'.$rowcall['start_time'].'</td>';
     $result .= '<td>'.$rowcall['end_time'].'</td>';
     $result .= '<td>'.$rowcall['call_duration'].'</td>';
     $result .= '</tr>';
     $result .= '</table>';
  }

  echo $result;

?>*
1

You could do it like this:

<?php 
    $result = "";
    while($rowcall=mysql_fetch_array($qry_call))
    {
        $result .= "<table>";
        $result .= "<tr>";
        $result .= "<td>".$rowcall['caller_id']."</td>";
        $result .= "<td>".$rowcall['did']."</td>";
        $result .= "<td>".$rowcall['start_time']."</td>";
        $result .= "<td>".$rowcall['end_time']."</td>";
        $result .= "<td>".$rowcall['call_duration']."</td>";
        $result .= "</tr>";
        $result .= "</table>";
    } 

    echo $result;
?>
0

Try on these lines. Also switch to mysqli_* rather than mysql_* as the one you are using is deprecated

<?php 
$str="";
while($rowcall=mysql_fetch_array($qry_call))
{
  $str .= "<table>
  <tr>
  <td>{$rowcall['caller_id']}</td>
  <td>{$rowcall['did']}</td>
  <td>{$rowcall['start_time']}</td>
  <td>{$rowcall['end_time']}</td>
  <td>{$rowcall['call_duration']}</td>
  </tr>
 </table>"; 
} 
?>
1
  • @BjornSmeets thats what the OP has asked, right? "To store mysql row result as table format in php variable"
    – rakeshjain
    Commented Sep 6, 2013 at 6:09
0

I strongly advise you to see docs at http://www.php.net/manual/en/language.types.string.php

0

start and end the <table></table> tag out side while loop no inside it will create a new table for every row but that's you don't want

Not the answer you're looking for? Browse other questions tagged or ask your own question.