I want save some special symbols to save in database using cakephp's query() function.
Sanitize functions of cakephp is not working.
$data = Sanitize::escape($this->request->data['Post']['body']);
I want save posted data with any symbols typed in database and show it in my view page.
How make it possible?
//Editted//
For example something like this
'Hello ..',I am new to stackoverflow', etc
When I add like this it will not be saving to database. I used save() method also. But no effect..Is it possible to use mysql_real_escape function in cakephp
Related
i'm trying to use old helper with updateorcreate. I have a textarea for an about field. If the user has published something, the old text should be displayed when he wants to update. When I try to use the old helper {{old('about', $profile->about)}}
I get this error: Attempt to read property "about" on null. When I remove the old helper, write something then add the old helper, then it works. I understand that it is trying to retrieve something that isn't in the database. I tried making the about field nullable but it still doesn't work
Seems that $profile is null in your case. Maybe a create form instead of edit?
Try to :
{{old('about', $profile->about ?? '')}}
or
{{old('about', optional($profile->about))}}
So that it uses empty string when old does not exist and profile either.
I have yii language localization turned ON, so I use in config 'language'=>'en',
and write things like Yii::t('layouts_main','Home') and store translations of Home in php file.
But apart of this, I have tables, and there are data inside in different languages.
For example I have a list of countries which must be stored in mysql table in different languages.
Table structure is very simple: id, name_en,name_de,name_es etc...
I did it that way so if language change those 2 letters must be controlling from which sell to read the name.
In my controller I get my data from table to array
$tableCountry = Country::model()->findAll();
Then I'm making such variable which will containt "name+" language variable which is 'en' in config
$name_lang ="name_".Yii::app()->Language;
So now I made variable $name_lang which contains name_en
Then I choose the right sells using $name_lang variable
$list=CHtml::listData($modelCountry,'id',$name_lang);
Using $name_lang only name_en data will go to List. So later I can switch settings of language in config file to "de" and only name_de data will fgo to List.
So everything is fine here.
But what If I have complex table which contains country_name_en, city_name_en,region_name_en etc...
To put this data into list I have to make 3 or more variables
$country_name_lang ="country_name_".Yii::app()->Language;
$city_name_lang ="city_name_".Yii::app()->Language;
$region_name_lang ="region_name_".Yii::app()->Language;
So I wonder is there any other better way to do such things ?
Option 1: You can define in the model another variable called country_name (without any suffix). You can do this just in the model, you do not have to create a field in the db too. No need for validation or anything for this variable.
Then in your after find method for the model you can do
public function afterFind()
{
$this->country_name = $this->{"country_name_".Yii::app()->Language};
}
Feel free to add everything here.
Now everywhere you should be able to use $model->country_name without any care what the language is.
Option 2 would be to use magic methods. you can read more from them here: http://www.php.net/manual/en/language.oop5.magic.php
Define for your model such a method:
public function __call($name, $arguments)
{
if(isset($this->{$name. "_".Yii::app()->Language}))
return $this->{$name. "_".Yii::app()->Language}
elseif(isset($this->$name))
return $this->{$name}
else
throw new Exception(xxx, 'We couldn\'t find the variable');
}
Now you should be able to use $model->country_name() and you would again get the variable in the proper language. You can use also $model->id() and you would get the proper id too as it has a fallback if there is no id_en field.
I want to check if the variable $username is alphanumeric and less than 15 characters. Does CodeIgniter have this functionality built in? I don't want to rewrite these functions if they already exist. I know there's form validation, but that's not what I want.
Following #Sparky's comment, all the validation functions look like they are public functions inside of Form_validation.php
you can load form vlaidation and use the validation functions directly
so you could run it through max_length then alpha_numeric, as they are all public just look at source and call them with the correct params
I'm working on a way for users to be able to generate PDF copies of invoices and other tabular data. To do this, I've wrapped dompdf into a library that I can use with CI and created a method that will generate a PDF based on the return value of CI's output->get_output(). The wrapper is similar to this one on Github.
The problem is, I can't figure out a way to get the view (and HTML/CSS needed for the PDF) into CI's output class other than load->view(), which is going to write to the browser.
My only other choice would be to use curl to request the page, but that seems so silly to do since I can get it right from the output buffer. I just don't want the HTML sent to the browser, since I set headers telling the browser to expect a PDF.
To be clear, this is what I want to accomplish (in the order that I want to accomplish it):
Do everything I'd normally do to prepare the view for display
Load the view into the CI output class, but not display it
Pass the return value of output->get_output() to my dompdf library
Set the appropriate headers
Execute my dompdf method that will send the PDF to the browser
I don't see any way of doing step 2 based on the output class documentation.
Is it possible to get a view into the output class without displaying it? If so, how? I'm using CI 2.0.3.
Edit
The very helpful Anthony Sterling pointed out that I can just get what I want from the loader class by setting the third argument telling it to return a string rather than render the view to TRUE. E.g.:
$lotsaHtml = $this->load->view('fooview', $somearray, TRUE);
And that would be better in my particular instance since I don't need to load partials. However, this is still a valid and (I think) interesting question, it would also be handy to know if I could get the same from the OB, perhaps if I did have a bunch of partials. Those could be concatenated, but yuck.
It seems like I should be able to get the output class to not render anything (else, why does get_output() exist?) so I can do something else with everything it knows about. I just can't find a way to make that happen.
Edit 2
Some pseudo (but not far from reality) code illustrating what I hope to do, by showing what I did and then explaining what I actually wanted to do.
Let's say I have a public method genpdf($id) in a controller named invoice using a model named inv:
public function genpdf($invoiceId) {
$this->load->library('dompdflib');
$this->pagedata['invoice_data'] = $this->inv->getInvoice($invoiceId);
$html = $this->load->view('pdfgen', $this->pagedata, TRUE);
$this->dompdflib->sendPdf($html);
}
That is almost identical to code that I have that works right now. There, I ask the loader to parse and give me the results of the pdfgen view as a string, which I pass to the function in my dompdf wrapper that sets headers and sends the PDF to the browser.
It just seemed like this would be easy to do by just getting the output buffer itself (after setting headers correctly / etc).
Or do I just have to call the output class append_output() in succession with every partial I load?
Multiple methods loading a plethora of models need to work together to generate these (they're going in as an afterthought), so I was hoping to just collect it all and retrieve it directly from the output class. It could be that I just have to talk gradually to output->append_output() to make that happen.
...so - do I understand correctly - you want to get the whole final output (not just the view) as a string AND not display it to the user? Why dont you just overload the controllers _output() function?
class Your_controller extends CI_Controller
{
function stuff()
{
// do whatever - prep $data etc
$this->load->view('your_view', $data);
}
function _output($output)
{
// send $output to your library - get results blah blah
$result_pdf_file = $this->your_pdf_library_generator($output);
// Show something else to the user
echo "hi - I'm not what you expected - but here is your PDF";
echo $result_pdf_file; // or something like that
}
}
This means you can send ANYTHING you like to the output class - but nothing is displayed except what you want.
There are ways to improve this idea (i.e. hooks, variables to turn output on/off etc) - but the simplest would be to have this controller specifically for your pdf_generation command.
I don't see any way of doing step 2 based on the output class documentation. Is it possible to get a view into the output class without displaying it? If so, how? I'm using CI 2.0.3.
The controller _output() documentation is actually in the CI controller documentation, which is why it eluded you.
I have created a table in DB with name "member" having field "name", "college", "email", "zid" now I have created a class(in php) like
class member
{
private $name,$college,$email,$zid;
private function adduser
{
//function definition
}
public function autherise($id)
{
//function definition
}
}
now at index page I am taking these value as input from user using html form(validated by JS) now at action page of form I want to create object as obj=new member(); then calling the class function as obj->autherise($_post['zid']);
I want the defintion of autherise and adduser function like autherise check the uniqueness of zid and the calls adduser with remaining post variables and store them to object then add whole object in one query to DB.
I dont wan
insert into member(name,email,college,zid) values('$name,$email,$college,$zid')
I want to enter obj directly to the db
You can modify anything in functions
Thanks in Advance!!!
An "easy" solution to store a whole object somewhere, like in a database, is to serialize it – i.e. transform it to a string ; and, then, store that string in a single field in the database.
This can be done, in PHP, using the serialize function – and to de-serialize the string to an object, you'll use the unserialize function.
Another solution would be to serialize your object to the JSON format – nice thing with that is that JSON can be used directly from Javascript, and from lots of different programming languages, using the right libraries.
For JSON, see json_encode and json_decode
As a sidenote : note that if you store your data as serialized strings, it will be much harder to manipulate them in SQL : you will not be able to update them using a simple SQL query, for instance; and searching will be hard too.
This means you'll always have to fetch your data from the database, manipulate them with PHP, and send them back to the database – which might not always be such a good idea, depending on your needs.
I'm not sure what you're asking for. But maybe...just maybe you're looking for an object relational mapper (orm) , like e.g. doctrine.
If you were looking to store an object in a database you could serialize() or json_encode() the object into a String and then store it in a field in the table.
When you are ready to use it again you can unserialize() or json_decode() the String again.