This topic is locked
[SOLVED]

 Iframe On Edit Page

2/4/2013 4:28:08 PM
PHPRunner General questions
J
jmlphp author

Trying to get some ideas on how to create an iFrame to display a pdf on the edit page.

What I would like to do is on the edit page, if the first attachment is a PDF file, (using phprunner 6.2 file upload fields like

$fileArray = my_json_decode($values["fieldname"]);
then display the pdf on the edit page using an iframe.
The reason I want to do this is some of the data for the record is from the PDF, and by opening the PDF on the same page I can make editing a little easier, instead of flipping back and forth between web pages.
Any ideas?

(I'm using PHPRunner 6.2 build 14670)

C
cgphp 2/4/2013

You can set the pdf file as source (src) of the iframe.

J
jmlphp author 2/5/2013



You can set the pdf file as source (src) of the iframe.


Thanks. If I hard code the source in the iframe, it works great. However, if I try to retrieve the value from a field and use ECHO , it doesn't work. According to this:

http://xlinesoft.com/blog/2011/04/28/taming-the-beast-events-buttons-and-code-snippets/
"Unlike other methods Code Snippet doesn't provide any access to page data."
So, how do I retrieve the data stored in a field to so that I can echo out the iframe source?

C
cgphp 2/5/2013

Post your code.

J
jmlphp author 2/5/2013



Post your code.


Here is the code:



// get our array of files that were uploaded

$jmlfileArray = my_json_decode($values['jmlfiles']);

//need to check if we have anything stored - just set to true for now

if (True) {
//get the file (displayed) name

$jmlfileusrName = $jmlfileArray[0]['usrName'];

//get the file extension

$jmluserfile_extn = strtolower(substr($jmlfileusrName, strrpos($jmlfileusrName, '.')+1));

//get the file

$jmlfileName = $jmlfileArray[0]['name'];

//if extension is a pdf then display it else display message

if ($jmluserfile_extn='pdf') {

// echo $jmluserfile_extn; - this does display 'pdf' on the page

echo '<IFRAME SRC="';

echo $jmlfileArray[0]['name']; //this is NOT echo anything, unless I hard code it

echo '" WIDTH="650" HEIGHT="800" SCROLLING="No">

If you can see this, your browser does not

understand IFRAME.</IFRAME>';

}

else

{

echo 'No PDF attached';

}

}

else

{

echo 'No Attachments';


The page displays all my fields, and the iframe with nothing for the source. However, if I hard code, the iframe works.

Strangely, if I echo jmluserfile_extn, it displays 'pdf'. So I know that part is working. However, if I echo any other $jmlxxxxx field, nothing shows up.
(edit: caught an error in the echo, added echo $jmlfileArray[0]['name'] to change the variable name. However, it still doesn't work.)

C
cgphp 2/5/2013

Try to run this code:

$jmlfileArray = my_json_decode($values['jmlfiles']);

echo 'File name is: ' . $jmlfileArray[0]['name'];

exit();


What do you see?

J
jmlphp author 2/5/2013



Try to run this code:

$jmlfileArray = my_json_decode($values['jmlfiles']);

echo 'File name is: ' . $jmlfileArray[0]['name'];

exit();


What do you see?


I commented out all my code and pasted the above. after full build, on the page all I get is one of my fields, and this:
File name is:
Nothing else shows

C
cgphp 2/5/2013

Run this code:

$jmlfileArray = my_json_decode($values['jmlfiles']);

print_r($jmlfileArray);

exit();


and check what are the right keys to access the name of the file.

J
jmlphp author 2/5/2013



Run this code:

$jmlfileArray = my_json_decode($values['jmlfiles']);

print_r($jmlfileArray);

exit();


and check what are the right keys to access the name of the file.


Thanks for your replies and help. I really appreciate it.
I ran the code and get:
Array()
I do have a pdf uploaded to the record. When I view the page, the small pdf icon shows and I can click and bring up the PDF just fine.
As a further test, on the view page I added this code snippet on the events page; Process record values:



$fileArray = my_json_decode($values['jmlfiles']);

// process each item in the array

for($i = 0; $i < count($fileArray); $i++)

{

//get the full path name of the file

$fileName = $fileArray[$i]['name'];

//get the file (displayed) name

$fileusrName = $fileArray[$i]['usrName'];

// store the info in our mycolumn. The last 'slash n' adds a linefeed for display

$values['mycolumn']=$values['mycolumn'] . $i . '=> ';

$values['mycolumn']=$values['mycolumn'] . 'Path (name) = '. $fileName;

$values['mycolumn']=$values['mycolumn'] . ' -> ';

$values['mycolumn']=$values['mycolumn'] . 'File Name (usrName) = ' . $fileusrName;

$values['mycolumn']=$values['mycolumn'] . "\n";

}


The page displays: 0=> Path (name) = files/articles_pics/CC_02nh4jae.pdf -> File Name (usrName) = CC.pdf

So, the field does contain the file and location properly.
I'm starting to think that the fields in the code snippet are not exposed. Like I mentioned in my original posts, the blog states "Unlike other methods Code Snippet doesn't provide any access to page data." Does this meant that I'm unable to read the page fields, so therefore, can't process the data stored in the array??
Thanks again!

J
jmlphp author 2/5/2013

After some trial and error, I have successfully solved my own question.
Code Snippets can't use field data from the page. If you want to use an iframe with source info that is contained in a field, you'll need to perform a separate query to retrieve the data. I've used the code listed below to accomplish what I needed:


// code snippets don't expose the page fields, so we need to do an SQL query to get field data
// perform an sql query to retrieve the data from the upload field

global $dal;

$tblMyTable = $dal->Table("tablename");

//editid1 is the session variable that contains the record number that we are on

$rs = $tblMyTable->Query("record_id=".$_REQUEST["editid1"],"");

$data = db_fetch_array($rs);

// get the data string from are field that contains the uploaded file names (if any)

$myUploadData = $data["fieldname"];

// load the field data into an array

$myFileArray = my_json_decode("$myUploadData");

//need to check if we have anything stored in the array

if (!empty($myFileArray)) {

//get the file (displayed) name of the first record [0]

$myFileusrName = $myFileArray[0]['usrName'];

//get the file extension

$myFile_extn = strtolower(substr($myFileusrName, strrpos($myFileusrName, '.')+1));

//get the file path and file name from the array

$myFileName = $myFileArray[0]['name'];

//if extension is a pdf then display it else display a message

if ($myFile_extn=='pdf') {

echo '<IFRAME SRC="';

echo $myFileArray[0]['name'];

echo '" WIDTH="650" HEIGHT="800" SCROLLING="No">

If you can see this, your browser does not

understand IFRAME.</IFRAME>';

}

else

{

echo 'No Primary PDF Attached';

}

}

else

{

echo 'No Attachments Uploaded For This Record';
}


Change tablename, record_id, fieldname to reflect your table and field names.

Hope this helps if someone needs to do something similar.