The data pulled from the database inserts into an RTF template without a problem as long as I only have 1 row. If I put my header("Content-type: text/rtf") inside the loop, however, I receive a headers already sent error. When removing the headers from the code, the raw data prints properly in the browser window.
How do I combine data from multiple records in the database to create multiple documents (or 1 document with page breaks)?
Code here:
$document='../docs/letter.rtf';
$qry="Select * from table";
$result=mysql_query($qry);
$num_row=mysql_num_rows($result);
while($row=mysql_fetch_assoc($result)){
$account_id=$row['pk_account];
$name=$row['first_name'];
$file_doc='01-'.$pk_account.'.rtf';
....
$body=file_get_contents($document);
$body=str_replace("NAME", $name, $body);
....
for($i=0; $i<$num_rows; $i++){
}
header("Content-type: text/rtf");
header("Content-Disposition:attachment; filename={$file_doc}");
echo $body;
}
You need to concatenate your $body. So instead of this:
$body=file_get_contents($document);
$body=str_replace("NAME", $name, $body);
try this:
$body.=str_replace("NAME", $name, file_get_contents($document));
Then place this outside your while loop:
header("Content-type: text/rtf");
header("Content-Disposition:attachment; filename={$file_doc}");
echo $body;
Related
Not able to pass array variable form controller to view below code
If, I print in controller it is showing "Undefined variable: data"
public function report(){
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
print_r($data); die();}
Try using compact like this.
public function report(){
$data=$this->Nodals->find()->toArray();
$this->set(compact('data'));
};
Now do dd in your view to check if the data variable sending to the view. I am assuming your view file is report.ctp, so in report.ctp file write this line
<?php
dd($data);
?>
I hope it will help.
$data is not defined in your code if you want to print the query result from
$this->Nodals->find()->toArray(); then you need to store this in $data variable or any other variable
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
//data will be sent to the template which wrap the $result and in template you can access $reseult by using $data
print_r($results);
die();
or
$data=$this->Nodals->find()->toArray();
$this->set('data',$data);
print_r($data);
die();
for web services , how to retrieve the data json format using Laravel, can anybody help
https://test.com/admin/users/apiLogin?Login={"email":test#test.com,"pwd":testing}
in Controller
public function apiLogin(Request $request)
{
//$data = $request->json()->all();
$data=json_decode($request['Login']);
echo $data['email'];
echo "<pre>";
print_r($data);
echo "</pre>";
echo "testsetet";
exit;
}
To get the URL parameter use this:
$login = $request->input('login'); // or $request['Login']
The goal is to turn login (which is a string) into appropriate JSON format so that you can use json_decode.
I'll break it down in steps:
Remove all \n characters:
$login = str_replace('\n', '', $login);
Remove last comma
$login = rtrim($login, ',');
Add brackets
$login = "[" . trim($login) . "]";
Turn it into a PHP array:
$json = json_decode($login, true);
The issue here is that your url parameter 'Login' isn't properly formatted json. I would revisit your front-end code persisting this data and ensure it is formatting properly. True json should look like:
{"email":"test#test.com","pwd":"testing"}
As a BIG aside, never persist passwords in the URL. It is dangerously foolish. Use a POST request to do this. On the plus side, the back-end code will be virtually the same with your json_decode() logic.
I'm facing a very wierd situation concerning a quite simple task.
I'm trying to create a very simple edit form in my application using
cakephp 3.x version.
In my ContentsController::edit($contentID) method I'm loading the content entity to be edited like this
$content = $this->Contents->findById($contentID)->first()
and then I'm simply creating the respective view variable using set() method like that:
$this->set('content', $content);
In the view file - named edit.ctp - all I'm doing is to simply create
a new form using FormHelper using the following piece of code:
<h2><?= __('Edit content: ') . $content->title; ?></h2>
<?php
echo $this->Form->create($content);
echo $this->Form->input('title', ['type' => 'text']);
echo $this->Form->input('alias', ['type' => 'text']);
echo $this->Form->input('body', ['type' => 'textarea']);
echo $this->Form->submit();
?>
The following code creates the form correctly but it does not load the default values in each input element from the $content entity. After doing some digging into the source code of the FormHelper I found out that when the FormHelper::create() method is called, it correctly loads the EntityContext interface using the $content entity. But for some reason, which I cannot explain, in each of the FormHelper::input() calls, internally the context interface is switching to NullContext so no data is loaded into the field.
Does anyone have an idea what am I doing wrong with that piece of code?
After some more digging I found the real cause of the issue.
FormHelper works correctly and so does my query.
The issue has to do with the view file and how it is rendered.
The whole picture is this.
My view (edit.ctp) was an extension of a common skeleton I created, namely edit_frm.ctp. So in my view file I was extending by calling $this->extend('/Common/edit_frm');
The structure of the edit_frm.ctp consists of three blocks, as shown below (I removed the html markup)
<?php
// Common/edit_frm.ctp
$this->fetch('formStart');
$this->fetch('formPrimaryOptions');
$this->fetch('formSecondaryOptions');
$this->fetch('formEnd');
?>
Now in my view file (edit.ctp) I was creating the blocks like that:
<?php
// Contents/edit.ctp
$this->extend('Common/edit_frm');
// The "formStart" block contains the opening of the form
$this->start('formStart');
echo $this->Form->create($content);
$this->end('formStart');
// The "formEnd" block contains the submit button and the form closing tag
$this->start('formEnd');
echo $this->Form->submit();
echo $this->Form->end();
$this->end('formEnd');
// "formPrimaryOptions" contains the main input fields
$this->start('formPrimaryOptions');
echo $this->Form->input('title', ['type' => 'text']);
echo $this->Form->input('alias', ['type' => 'text']);
echo $this->Form->input('body', ['type' => 'textarea']);
$this->end('formPrimaryOptions');
?>
As you see in my view file, I was building the formEnd block before the construction of the formPrimaryOptions block. Though in my skeleton the blocks are fetched in a different order.
Apparently in CakePHP when you extend a view combined with blocks of content in the actual view file you must create your blocks in the same order as they are fetched, otherwise you end up in weird situations like the one I was facing.
In any case, I had a very good lesson today!!
Maybe you can do this instead of findById:
$content = $this->Contents->find('all')->where(['id' => $contentID])->first();
I'm using a json-parser in Xcode to fetch a table from phpmyadmin. The parser gets (or should get) the json-formated document via a php-file uploaded on my ftp-server. The file is successfully parsed but it doesn't recognize any objects. I think this is because there are multiple arrays in the json-document.
When there's only one entry the document looks like this:
[{"id":"1","Name":"Eric","Message":"first from web"}]
but when i add an entry it looks like this:
[{"id":"1","Name":"Eric","Message":"first from web"}]
[{"id":"1","Name":"Eric","Message":"first from web"},{"id":"6","Name":"Claes","Message":"Hurrburr"}]
As you can see the old array (containing only the single entry) is still there in the second array with both entries.
I suspect the problem is that the old arrays are still there when i update the database because when i tried parsing the json document with only one entry (only one array) it worked.
So my question is first if thereĀ“s something i missed in my code, or if you know why the old arrays are still there when I update the database or how to remove all previous arrays when the document is updating.
Here is my .php-file:
<?php
$username = "perhaps not sharing this information";
$password = "or this";
$database = "nah";
mysql_connect("the server url",$username, $password);
#mysql_select_db($database) or die("Error here");
$query = "SELECT * FROM debug_db";
$result = mysql_query($query) or die(mysql_error());
$num = mysql_numrows($result);
mysql_close();
$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
echo json_encode($rows);
}
?>
And check out the json-document at: http://app.levinnovation.se/getjson.php
Thank you!
Hi m a bit confused that how to retrieve node title by using this code
node_load($nid);
$title=$nid->title;
i have done this coding in block and i wants to retrieve from node id for displaying image.that images are normally uploaded at the site by using filezilla and it has same name as the node title.i have tried many forms of node_load(),but i m failure.so please tell me right option for this.
Thanks all.-Pranoti
Here is the reference for node_load
http://api.drupal.org/api/function/node_load
It returns an object which is the node.
$node = node_load($nid); // $nid contains the node id
$title = $node->title;
Please get a good book on Drupal Module development to learn the fundamentals.
Your question is a little confusing. Could you clean it up and explain better what you are trying to accomplish? In all events:
Node load takes either an numeric argument or an array of parameters to query, and returns a single node object. (As already mentioned, here's the API documentation: http://api.drupal.org/api/function/node_load).
Load with a numeric node id:
$nid = 55;
$node = node_load($nid);
$title = $node->title;
Load by querying on title:
$title = 'How to serve man';
$node = node_load(array('title' => $title));
$body = $node->body;
you can also load multiple node load efficiently by using the following code
<?php
$type = "product_type";
$nodes = node_load_multiple(array(), array('type' => $type));
foreach($nodes as $products):
?>
<?php print $products->nid; ?>
<?php print $products->title; ?>
<?php endforeach; ?>
also you can query any thing in the node load for example we have used type in query but we can also use title as mentioned in the above post by
"David Eads"
NODE LOAD BEST PRACTICES
If you are loading a lot of nodes with node_load(), make sure to use the $reset parameter so that every node isn't kept in the function's static cache (and increasing memory usage):
$nid = 55;
$node = node_load($nid, NULL, TRUE);