Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Reg: Displaying database result as per user query from smarty.

Madhu.149Oct 9 2013 — edited Oct 16 2013

Hello All,

I am developing an user interactive sql site, where user can input sql queries and fetch results in the application.

I need to get the result set from the database as per user query and display in the form of a table.

php:

<?php

include_once('tiki-setup.php');

$res_set = array();

  $conn=oci_connect("hr","hr","localhost/XE");

  $c_err = oci_error($conn);

  $c_err1 = htmlentities($c_err['message']);

  $smarty->assign('con_err', $c_err1);

  // check if the form has been submitted. If it has, process the form and save it to the database

  if (isset($_POST['submit']))

  {

$sqlid = $_POST['sqlid'];

$smarty->assign("sqltext", $sqlid);

  // get form data, making sure it is valid

  $sqlid = $_POST['sqlid'];

  $query=$sqlid;

  $stid = oci_parse($conn, $query);

  $res = oci_execute($stid);

  if (!$res) {

    $e = oci_error($stid);  // For oci_execute errors pass the statement handle

  $e1 = htmlentities($e['message']);

$smarty->assign('err', $e1);

}

else

{

  while ($rows = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

  $res_set[] = $rows;

  }

  $smarty->assign('result', $res_set);

 

  }

}

$smarty->assign('mid', 'hello_world.tpl');

$smarty->display('template_new.tpl');

  ?>

Smarty:

<table style="border-collapse:collapse;">

   {foreach from=$result item=row}

    <tr style="background-color: {cycle values="#eeeeee,#d0d0d0"}">

  {if $row.DEPARTMENT_ID and $row.FIRST_NAME and $row.SALARY}

  <td>{$row.DEPARTMENT_ID}</td>

  <td>{$row.FIRST_NAME}</td>

  <td>{$row.SALARY}</td>

  {else}

        <td>{$row.EMPLOYEE_ID}</td>

  <td>{$row.FIRST_NAME}</td>

  <td>{$row.LAST_NAME} </td>

  <td>{$row.EMAIL}</td>

  <td>{$row.PHONE_NUMBER}</td>

  <td>{$row.HIRE_DATE}</td>

  <td>{$row.JOB_ID}</td>

  <td>{$row.SALARY}</td>

  <td>{$row.COMMISSION_PCT}</td>

  <td>{$row.MANAGER_ID}</td>

  <td>{$row.DEPARTMENT_ID}</td>

  {/if}

    </tr>

    {/foreach}

  </table>

  <div style="color:red;">

  {$err}

  </div>

  <div style="color:red;">

  {$con_err}

  </div>

Without smarty I can get the result with the below code..

print '<table border="1">';

   while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

  print '<tr>';

    foreach ($row as $item) {

  print '<td>'.($item?htmlentities($item):' ').'</td>';

    }

    print '</tr>';

   }

   print '</table>';

But i need to do with smarty?

How to transfer the "print" code to smarty

How is this possible?

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 13 2013
Added on Oct 9 2013
1 comment
2,624 views