Wednesday, September 21, 2011

Field Listing Section of First Part of Reporting Tool

The day quite went tough as I had to struggle a lot while outputting the Field list on each selection of table via AJAX in Reporting Tool Project. But as is said, "All Well that Ends Well". Same happened with me. After doing so much struggle, I was able to resolve the issues.

Continuing to the previous Post's code,

If user selects the table, (I am showing a code snippet)

index.php
<h2><span>Please Select the </span>Credentials</h2><br/>
<form>
<label>Select The Table</label>
<?php
include_once 'config.php';
if($op=='mysql'){

}
elseif ($op=='postgresql') {
$query=pg_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';");
echo '<select name="table" onchange="return showfield(this.value);">';
while($result=pg_fetch_array($query)){
echo'<option value="'.$result['table_name'].'">'.$result['table_name'].'</option>';
}
}
echo '</select>';
?>
</form>

After that is shown

ajax.js
function showfield(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("result").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","showfield.php?table="+str,true);
xmlhttp.send();
}

This above ajax.js script which is issuing an ajax call for every selection of new table from the list.

and after that is

showfield.php
<?php
include_once 'config.php';
$t=$t.$_GET['table'];
$query= pg_query("SELECT column_name FROM information_schema.columns WHERE table_name ='$t';");
echo '<table border=1>';
echo '<tr><th colspan="5">Select Field</th></tr><tr>';
$i=1;
while($result=pg_fetch_array($query)){
echo '<td><input type="checkbox" name="field[]" value="'.$result['column_name'].'"/>'.$result[column_name].'</td>';
$i=$i+1;
if($i>5){
echo '</tr><tr>';
$i=1;
}
}
echo '</tr><tr><td colspan="5"><input type="submit" value="Enter fields" onclick="enterfield();" /></td>';
echo '<td colspan="5"><input type="submit" value="Clear Selected Fields" onclick="clear();" /></td></tr>';
echo '</table>';
?>

Which will List the all fields for a table. On the Selection of which and after submission will added to another table and user will again allowed to check whether they are sure to opt for all these fields. So Next Task after this will be Selection of Fields and Confirmation and Submission for Query Generation.

No comments:

Post a Comment