Function to convert data retrieved from Zend_table to html entities - php

i'm using zend_tables for my model and i'd like to create a function to convert all data from a single row (retireved like this: $row = $model->find($id)) to htmlentities before outputting it with the usual code:
echo ($row->current()->nameOfField);
I need something like this function i use on arrays:
public static function convertArrayToHtmlEntities($data){
$keys = array_keys($data);
foreach ($keys as $k) {
$d = $data[$k];
$clean = htmlentities($d, ENT_QUOTES, "UTF-8");
$data[$k] = $clean;
}
return $data;
}
Thx in advance!

If you're using Zend View you can just escape the output in your view script. See Escaping Output.
If you're not using it in a view script you can use Zend Filter.

Related

Laravel 5 how to manipulate my variable in an for each in my controller and then pass it to my view

I have a database table named 'services'. With the function below I get all the records from that table. This function is located in my ServicesController.php.
public function index() {
$roadmap = Roadmap::all();
return view('services', compact('roadmap'));
}
Now in my view I also do the following:
<?php
foreach($roadmap as $roadmap_item) {
$new = array();
$splitted = explode("|", $roadmap_item->steps);
foreach($splitted as $split) {
$new = explode(":", $split);
}
}
?>
Lets say I get the string 'step1:hello|step2:bye' back from '$roadmap-item->steps'. I split them in substrings with explode etc. This is working by the way.
But is there a way I can manipulate the string in the Controller so my view will be nice and clean without many php code and still remain the variable $roadmap with all the database records.
Kind regards,
Dylan
I think you can do very similar things in your controller. Do foreach in your controller first then make a changes in every item in your collection and pass the new collection.
For example:
$roadmaps = Roadmap::all();
foreach($roadmaps as $roadmap){
$roadmap->something = " the changes you want to do as string " . $roadmap->something;
$roadmap->save();
}
return view('services', compact('roadmaps'));
or
$roadmaps = Roadmap::all();
$new = new Collection();
foreach($roadmaps as $roadmap){
$new[] = array('roadmap' => $roadmap, 'something' => $roadmap->string . " etcetc ")
$new->save();
}
return view('services', compact('new'));

PHP template engine for loops

I have a simple template engine that works fine the simple template, but I don't know how to adapt it to make it work with loops :
class Template {
public $template;
function getFile($file) {
$this->template = file_get_contents($file);
}
function set($tag, $content) {
$this->template = str_replace("{".$tag."}", $content, $this->template);
}
function ouput() {
eval("?>".$this->template."<?");
}
}
That's the loop I want to parse and display:
{{#each Stuff}}
{{Thing}} are {{Desc}}
{{/each}}
I dont want use any SMARTY or Twig engine.
Any idea please?
Ok, keep in mind this is just for learning purposes. You can't ask on SO for the whole code, you need to try and post question about your tries.
This code parse a string for a foreach and then executes it:
<?php
$var = array(2, 4);
$str = 'for i in var';
$a = explode(' ', $str);
foreach (${$a[3]} as $i => $value)
{
echo $value;
}
Read this part from PHP docs to understand what i did.

How to use array values outside foreach loop?

How can I use an array values outside a foreach loop in Laravel 5.4?
Here is the code :
public function index(Request $request)
{
$name = $request->input('keyword');
$category = $request->input('category');
$catkeywords = array(DB::table('keywords')->pluck($category));
foreach ($catkeywords as $catkeyword) {
$string = implode(',',$catkeyword);
}
echo $string;
}
I don't know why it doesn't work !
I just want the returned keywords from database to combine them with some text and to use for some API queries.
In other words, I want the list of keywords outside of the loop.
For using in an API query like this :
http://api-url/query?id=domain1.com,domain2.com,domain3.com
$catkeywords returns a json formatted list of keywords.
Now I want to combine these keywords with a user-inputted value and add a ".com" suffix,
then separate them using commas and use them on query url as a variable.
P.S : I'm using guzzlehttp for sending requests to the API. So it should be placed on :
'DomainList' => $domainlist
How can I do that ?
If you're using laravel, you should consider taking advantages of their collections:
https://laravel.com/docs/5.4/collections#method-implode
public function index(Request $request)
{
$name = $request->input('keyword');
$category = $request->input('category');
$catkeywords = DB::table('keywords')->implode($category, ',');
echo $catkeywords;
}
Laravel collections have a command for imploding the array, so there's no need to use pluck and loop through the array unless you plan on doing other operations on the data too.
EDIT: Based on updated question, it sounds like you're looking for something like this:
public function index(Request $request)
{
$name = $request->input('keyword');
$category = $request->input('category');
$catkeywords = DB::table('keywords')->pluck($category); //You don't need to wrap this in an array()
$keywords = []; //Create a holding array
foreach ($catkeywords as $catkeyword) {
$keywords[] = $catkeyword . '.com'; //Push the value to the array
}
echo implode(',', $keywords); //Then implode the edited values at the end
}
when you use pluck() method then it return array of given name
so you dont have need to use foreach loop
just use
$catkeywords = array(DB::table('keywords')->pluck($category));
echo implode(',',$catkeyword);
You try do that
public function index(Request $request)
{
$name = $request->input('keyword');
$string = '';
$category = $request->input('category');
$catkeywords = array(DB::table('keywords')->pluck($category));
foreach ($catkeywords as $catkeyword) {
$string .= implode(',',$catkeyword);
}
echo $string;
}

Object to String in PHP

One of my class' function outputs a text, something from my mysql db. But i couldn't explode() it since explode needs a string parameter. How to change it to a string.
class admin{
//constructor code
public function department_retrieve(){
//some code to retreive $this->old_department
echo $this->old_department;
}
}
$obj = new admin();
$obj->department_retrieve();
$obj->department_retrieve() outputs a text. I want something to explode the text by spaces. What i miss? How to make it a string? Please help.
You can use the casting operators:
$department_string = (string)$obj->department_retrieve();
You can try as like following . If got problem you can inform me...
<?php
class admin{
//constructor code
public function department_retrieve(){
//some code to retreive $this->old_department
return $this->old_department;
}
}
$obj = new admin();
$value = $obj->department_retrieve();
$dataArray = explode(" ","$value");
print_r ($dataArray );
?>

Returning two Json or save it

I have a site developed in codeigniter and I have a model where I have to create two Json but I don't know if is better to save it into my server or pass it itno the controller and after in the view but how?
This is my model simplify:
public function calledFromController(){
//code...
$this->makeRequest();
}
public function makeRequest(){
//code...
foreach ($foo as $foo2){
$values[] = $foo2->value;
}
foreach ($hello as $hello2){
$values_hello[] = $hello2->value;
}
//save it into my server
$file = fopen('foo.json','w+');
fwrite($file, $foo);
fclose($file);
$file2 = fopen('hello.json','w+');
fwrite($file2, $file2);
fclose($file2);
}
Now I save it, but can be a very lot of data and after I have to open each json and parse it into javascript (backbone).
Is better this mode or return this two json (I have to mantain separate Json)?
Is I have to return this two Json how Can I do?
From my controller I call the function calledFromController in the model.
Try by creating an array and returning the array if you want to return both the json:
$data = array();
$data['foo'] = $foo;
$data['hello'] = $hello;
return $data;
In the controller:
$return = $this->model->function();
$foo = $return['foo'];
$hello = $return['hello'];

Categories