how to convert php file to codeigniter [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need help to convert this code to codeigniter MVC i have 3 file php Openlink3.php for viewer class.php for class and databaseConnection.php for model
i have problem converting this php file to codeigniter. Please help me.
this my PHP code Openlink.php
<?php
include("DatabaseConnection.php");
include("class.php");
Global $AppCode;
//$AppCode='0001';
$user='0772';
$db = new dbconnection();
$conn = $db->dbOpen();
$misApp = new misApplication();
$sql="SELECT a.RAL_Code,a.RAL_App_Name,a.RAL_Remarks,b.RAL_APPIconLocation,b.RAL_ApplicationLink FROM R_Application_List as a ,R_Application_Links as b where a.RSC_Active=1 and a.RAL_Code=b.RAL_APPCODE";
$rs=odbc_exec($conn,$sql);
$appcount;
echo "<table align=center cellpadding=10>";
echo "<tr height=120>";
$appcount=0;
while (odbc_fetch_row($rs))
{
$appcode=odbc_result($rs,"RAL_Code");
$appname=odbc_result($rs,"RAL_App_Name");
$desc=odbc_result($rs,"RAL_Remarks");
$icon=odbc_result($rs,"RAL_APPIconLocation");
$applink=trim(odbc_result($rs,"RAL_ApplicationLink"));
if($appcount==3){
echo "<tr height=120>";
//echo "<td>";
$appcount=0;
}
//echo "<td>".$appcode."</td>";
//echo "<td>".$appname."</td>";
echo "<td valign=bottom><img src=".$icon."height=30 width=150 onclick='openApp(\"$appcode\",\"$user\",\"$applink\");' </img></td>";
//echo "<td><input type=\"button\" ></td>"
//echo "<td>".$desc."</td>";
echo '<script>openApp();</script>';
$appcount++;
//echo $icon."<br/>";
}
echo "</table>";
?>
<img src="dtr.png" height="30" width="150" onclick=openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $misApp->getAppLink($conn,"0001",$user); ?>") ></img></br>
<input type="button" id="btn.DTR"; name="btn.Dtr"; value="DTR"; onclick='openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $misApp->getAppLink($conn,"0001",$user); ?>")'/>
<input type="button" id="jose"; name="btn.OpenApp"; value="Approval System(test)"; background="dtr.png";onclick='openApp("0014","<?php echo $user; ?>","<?php echo $appLink = $misApp->getAppLink($conn,"0002",$user); ?>")'/>
<script language="javascript">
function openApp(Appcode, User, link){
var OpenLink;
OpenLink = link+"?A1="+User+"&A2="+Appcode; // Concatenating strings
//alert(OpenLink);
//document.write (OpenLink); // printing the Concatenated string
//window.open(OpenLink);
//window.open(OpenLink,'_blank', 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000, top=10000, width=10, height=10, visible=none', '');
window.location.href = OpenLink;
}
</script>
</html>
this my PHP code class.php
<?php
class misApplication{
public function getAppLink($actveConn,$appCode,$user){
$linktoOpen;
$sql="select * from R_Application_Links where RAL_APPCode='$appCode'";
$rs=odbc_exec($actveConn,$sql);
while (odbc_fetch_row($rs))
{
$a=odbc_result($rs,"RAL_APPCode");
$b=odbc_result($rs,"RAL_InstallationLink");
$link=odbc_result($rs,"RAL_ApplicationLink");
$d=odbc_result($rs,"RAL_APPIconLocation");
}
$linktoOpen = $link.$user.$appCode;
return trim($link);
odbc_close($actveConn);
}
}
?>
DatabaseConnection.php
<?php
class dbconnection{
public function dbOpen(){
$conn=odbc_connect("Global02","USER-00","USER00");
if (!$conn){
exit("Connection Failed: " . $conn);
}
else{
return $conn;
}
}
}
?>
when i convert it it always have an error.

Simple steps you have to follow, you can need to check user guide first, anyway try this
You have to follow these steps:
Step 1: For database connection
In database.php
set your database configuration inside $db['default']
Load your database using autoload.php
$autoload['libraries'] = array('database');
Step 2:
Create model Example Mdl_mis.php
Example:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mdl_mis extends CI_Model {
function getAllApplink(){
$this->db->select("SELECT a.RAL_Code,a.RAL_App_Name,a.RAL_Remarks,b.RAL_APPIconLocation,b.RAL_ApplicationLink");
$this->db->from("R_Application_List as a");
$this->db->join("R_Application_Links as b","a.RAL_Code=b.RAL_APPCODE",'INNER');
$this->db->where("a.RSC_Active",1);
$result = $this->db->get()->result_array();
return $result;
}
function getAppLink($appCode,$user){
$this->db->select("*");
$this->db->from("R_Application_Links");
$this->db->where("RAL_APPCode",$appCode);
$result = $this->db->get()->row_array();
$a = $result['RAL_APPCode'];
$b = $result['RAL_InstallationLink'];
$link = $result['RAL_ApplicationLink'];
$d = $result['RAL_APPIconLocation'];
$linktoOpen = $link.$user.$appCode;
return trim($link);
}
}
?>
Step 3:
Create Controller example MisApplication.php
Create one method also
Example:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MisApplication extends CI_Controller {
public function index()
{
/* Load you model first */
$this->load->model('Mdl_mis');
$data['all_link'] = $this->Mdl_mis->getAllApplink();
/* Create one view called index.php and load it and pass your data*/
$this->load->view('index',$data);
}
}
?>
Step 4: Put code in index.php view
<!DOCTYPE html>
<html>
<body>
<table align=center cellpadding=10>";
<tr height=120>";
<?php
$appcount=0;
foreach($all_link as $rs))
{
$appcode = $rs["RAL_Code"];
$appname = $rs["RAL_App_Name"];
$desc = $rs["RAL_Remarks"];
$icon = $rs["RAL_APPIconLocation"];
$applink = trim($rs["RAL_ApplicationLink"]);
if($appcount==3){
echo "<tr height=120>";
//echo "<td>";
$appcount=0;
}
//echo "<td>".$appcode."</td>";
//echo "<td>".$appname."</td>";
echo "<td valign=bottom><img src=".$icon."height=30 width=150 onclick='openApp(\"$appcode\",\"$user\",\"$applink\");' </img></td>";
//echo "<td><input type=\"button\" ></td>"
//echo "<td>".$desc."</td>";
echo '<script>openApp();</script>';
$appcount++;
}
echo "</table>";
?>
<img src="dtr.png" height="30" width="150" onclick=openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $this->Mdl_mis->getAppLink("0001",$user); ?>") ></img></br>
<input type="button" id="btn.DTR"; name="btn.Dtr"; value="DTR"; onclick='openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $this->Mdl_mis->getAppLink("0001",$user); ?>")'/>
<input type="button" id="jose"; name="btn.OpenApp"; value="Approval System(test)"; background="dtr.png";onclick='openApp("0014","<?php echo $user; ?>","<?php echo $appLink = $this->Mdl_mis->getAppLink("0002",$user); ?>")'/>
<script language="javascript">
function openApp(Appcode, User, link){
var OpenLink;
OpenLink = link+"?A1="+User+"&A2="+Appcode; // Concatenating strings
//alert(OpenLink);
//document.write (OpenLink); // printing the Concatenated string
//window.open(OpenLink);
//window.open(OpenLink,'_blank', 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000, top=10000, width=10, height=10, visible=none', '');
window.location.href = OpenLink;
}
</script>
</html>

Related

Passing PHP array from external php file to script tags on the main page

Am trying to pass a variable from an external php script to scripts tag in the main page but the main_page.php is returning an undefined variable which is the array that i intend to pass from PHP to js. Its a university units registration system, any help will be greatly appreciated.
my javascript is
<script>
var m=<?php echo json_encode($ar) ?>;
alert(m[0]);
</script>
And my PHP file with the array variable ($ar) is this
<?php
$course=$semester="";
$course_err=$semester_err="";
$link = mysqli_connect("localhost","root","","courses");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["name"]))){
$course_err= "course is not set";
} else {
$course=($_POST["name"]);
}
if(empty(trim($_POST["email"]))){
$semester_err= "semester is not set";
} else {
$semester=($_POST["email"]);
}
}
if(empty($course_err || $semester_err)){
$sql="SELECT `$semester` FROM `$course`";
$retval=mysqli_query($link,$sql);
if(mysqli_num_rows($retval)>0){
while ($row= mysqli_fetch_assoc($retval)){
echo "<form method='post' name='units'>";
echo "<li class='list-group-item' style='background:#663300;color:#fff'>
<input type='checkbox' id='firstnit'><p style='color:#fff;font-
size:1.2em;'>{$row[$semester]}</p></li><br>";
echo "</form>";
$ar=array($row);
$_SESSION["tempArray"]=$ar;
echo json_encode($ar);
}
}else{
echo "<li class='list-group-item' style='color:#111;'>0 units
registered</li>";
}
echo "<script>alert('The units have been added to the iframe with success');
</script>";
echo "<br/>";
echo "<button onclick='Array()' class='btn'>Submit Units</button>";
}
?>

Exploding string and putting values in <select>

I have a column in my db, tip_sting:
1 row example(each one has the same format):
G1-11, G2-21, P2-50, P4-20, P100-2,
I'm using this when editing a item(I want to create(as many selects in the string) and put the values automaticaly in the select) :
https://jsfiddle.net/avrzwt6k/
So I was thinking of doing something like:
$pieces = explode(",", $tip_stingu);
$a=count($pieces); // piece1
echo $a;
echo '<br>';
echo $pieces[1]; // piece2
echo '<br>';
$tip_stinga=explode("-",$pieces);
I just dont know how could I continue?
Do you need something like this?
<?php
$items = "G1-11, G2-21, P2-50, P4-20, P100-2";
$pieces = explode(",", $items);
echo ("<select>");
foreach ($pieces as $piece) {
$tip_stinga = explode("-", $piece);
echo ('<option value="'.$tip_stinga[1].'">'.$tip_stinga[0].'</option>');
}
echo ("</select>");
?>
Using PHP & Javascript you can do:
<html>
<body>
<form id="example" name="example">
<select name="myName" id="myID" onchange="updateText()">
<?php
$tip_stingu = "G1-11, G2-21, P2-50, P4-20, P100-2,";
$pieces = explode(', ', $tip_sting);
$piece1 = explode('-', $pieces[0])[1];
foreach($pieces as $piece) {
$tip_stinga = explode("-", $piece);
echo ('<option value="'.$tip_stinga[1].'">'.$tip_stinga[0].'</option>');
}
echo '</select><input type="text" value="'.$piece1.'" id="quantity" /><br />'
?>
</form>
<script>
function updateText() {
document.getElementById("quantity").value = document.getElementById("myID").value;
}
</script>
</body>
</html>
You also have a trailing comma at the end of your example, if that's a problem you can use:
echo ('<option value="'.rtrim($tip_stinga[1], ',').'">'.$tip_stinga[0].'</option>');
instead of:
echo ('<option value="'.$tip_stinga[1].'">'.$tip_stinga[0].'</option>');

Passing javascript array via POST to PHP does not working

I need to pass a javascript array, via POST, to a PHP file.
I've tried to semplify my original business trying to explain my troubles ...
This is my first PHP file, in which I declare a javascript array and I set each element to 1 value (not a great business I know, but it doesn't matter, it's only to explain ....)
<?php
echo '<form action="testPhp-2.php" method="POST" target="testPhp-2">';
echo '<script type="text/javascript">';
echo 'var arr_selections = [];';
echo 'for(i=0; i < 10; i++)';
echo ' {';
echo ' arr_selections[i] = 1';
echo ' }';
echo 'arr_selections_json = JSON.stringify(arr_selections);';
echo 'alert (arr_selections[2]);';
echo 'document.write("<br/>");';
echo ' ';
// echo 'document.write("<input type="hidden" />");';
echo 'document.write("<input type=\"hidden\" name=\"arr_selections_json\" value=\"arr_selections_json\" />");';
echo ' ';
echo '</script>';
echo ' <input type="submit" value="Controlla">';
echo ' </form>';
?>
.... and here you are the code of testPhp-2 file ...
<?php
if(isset($_POST['arr_selections_json']))
{
echo "OK, array exist !! ";
echo '</br>';
echo '</br>';
$arr_selections = json_decode($_POST['arr_selections_json'], true);
echo $arr_selections[0];
}
else {
echo "NO; array does not exist !! ";
echo '</br>';
echo '</br>';
}
?>
Now, if you try to execute the code you'll see the OK, array exist !! message but no array value is printed about the echo $arr_selections[0]; line of code in testPhp-2.php file.
Any suggestion will be appreciated! Thank you in advance!
Cesare
Problem is that you're setting the value of the input to the litteral string "arr_selections_json" instead of to the contents of that variable.
Change
echo 'document.write("... value=\"arr_selections_json\" />");';
To
echo 'document.write("... value=\""+arr_selections_json+"\" />");';

I am trying to update records by php code. but having trouble in my code

I am trying to update records by php code. but having trouble in my code.
code is running without using functions (get40(),show()) but i want to use buttons to call these functions.
code is give below:
this is main code file:
<?php
$data;
$count=0;
$host='localhost';
$uname='root';
$pass="";
$dbname="testing";
$db=new mysqli($host,$uname,$pass,$dbname);
$result;
$query;
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
function get40($off='0', $rcd='2')
{
global $db,$result,$query;
$query="SELECT sword, tword, reviewed from TempDictionary LIMIT $off,$rcd";
$result = $db->query($query)or trigger_error($db->error);
}
function show()
{
global $db,$result,$query;
echo "<form action='testSave.php' method='POST'>";
echo "<table border=1 >";
while($row = mysqli_fetch_array($result))
{
global $count;
$count+=1;
echo "<tr>";
echo "<td><input type='text' name='s[]' value=" . $row['sword'] . "></td>";
echo "<td><input type='text' name='t[]' value=" . $row['tword'] . "></td>";
echo "<td><input type='text' name='r[]' size='1' value=" .$row['reviewed'] . "></td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='text' name='no' size='2' value=" .$count. ">";
echo "<input type='submit' value='Save'>";
echo "</form>";
}
//get40(1,2);
//show();
mysqli_close($db);
?>
<script type="text/javascript">
function get()
{
<?php
get40(0,3);
show();
?>
}
</script>
<form action="" method="POST">
<input type=button name=btnGet value=Get onclick="get()">
</form>
and savetest.php file is:
<?php
$no=$_POST['no'];
$i=0;
$s=$_POST['s'];
$t=$_POST['t'];
$r=$_POST['r'];
$host='localhost';
$uname='root';
$pass="";
$dbname="testing";
$db=new mysqli($host,$uname,$pass,$dbname);
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
// display
foreach ($s as $key=>$value)
{
print "$key = $value $t[$key] $r[$key]";
echo '<br>';
}
// save to DB
foreach ($s as $key=>$value)
{
// $query="UPDATE TempDictionary set sword=".$value.",tword=".$t[$key].",reviewed=".$r[$key]."
// WHERE sword=".$value;
$query="UPDATE TempDictionary set sword='$value',tword='$t[$key]',reviewed='$r[$key]'
WHERE sword='$value'";
$result = $db->query($query)or trigger_error($db->error);
}
mysqli_close($db);
?>
the problem is while i click button it does not works.
AFAIK you are trying to run PHP code with Javascript code.
Use Jquery/Ajax to solve your problem. You can Google for that or look at stackoverflow.
Step 1: Learn how a click on a button can run PHP
Run php function inside jQuery click
Step 2: Learn how to use data from a database and populate a form with it:
ajax call to populate form fields from database query when select value changes
That's the first step in solving your problem. (sorry it's no copy/paste solution, just a pointer in the right direction)

update_multiple_rows using ajax and php

please in need help in updating multiple rows based on dynamic data fetched from mysql database. I have implemented this using $_Post to udate.php file, but how can i do this using ajax.
//THIS SCRIPT FETCH FROM DATABASE
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("student") or die("Unable to select database");
$sql = "SELECT * FROM students ORDER BY id";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Address</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='update.php'>\n";
while ($students = mysql_fetch_array($result)) {
echo '<tr>';
echo "<td>{$students['id']}<input type='hidden' name='id[$i]'value='{$students['id']}' /></td>";
echo "<td>{$students['name']}</td>";
echo "<td><input type='text' size='40' name='address[$i]' value='{$students['address']}' /></td>";
echo '</tr>';
++$i;
}
echo '<tr>';
echo "<td><input type='submit' value='submit' /></td>";
echo '</tr>';
echo "</form>";
echo '</table>';
echo $i; ?>
</body>
// HERE IS THE UPDATE SCRIPT
// On clicking submit all rows are updated but i still cant achieve dis with ajax.
UPDATE.PHP
$size = count($_POST['address']);
$i = 0;
while ($i < $size) {
$address= $_POST['address'][$i];
$id = $_POST['id'][$i];
$query = "UPDATE students SET address = '$address' WHERE id = '$id' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
echo "$address<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
?>
HERE IS WHAT I HAVE TRIED ON AJAX
function update(){
var j=0;
while(j< <?php echo $i ; ?>){
var id=$("#id" + j);
var address=$("#address" + j);
$.ajax(
{
url:"update.php",
type:"post",
data:{id:id.val(),address:address.val()},
success:function(response)
{
}
});
}
ANY HELP WILL BE APPRECIATED
You shouldn't need to loop and execute multiple POSTs. Simply serialize the form data and send it to the script, e.g.:
$.ajax({
url: 'update.php',
type: 'post',
data: $('#your-form-id').serialize(),
success:function(response){
// do something
}
});
Also, you should consider revising your PHP to prevent SQL injection: How can I prevent SQL injection in PHP?

Categories

Resources