I've built a REST webservice and I want to remove the slashes from my json result. I have some data stored in my DB, which one of the fields contains a websitelinks.I can access them through my normal-view which is html and through my json-view. The normal returns de links normal but the json ads slashes to it like:
http:\/\/mywebsite\/company\/upload\/siteman\/thumbnails\/carr\/
How can i remove this? Here is my cakephp code:
// De html view
<h2>View all posts</h2>
<table>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
<?php foreach($images as $image):?>
<tr>
<td><?php echo $image['Image']['id'];?></td>
<td><?php echo $image['Image']['plaatjes'];?></td>
</tr>
<?php endforeach; ?>
</table>
// The json view
<?php
echo json_encode($images);
?>
Use str_replace, like this:
<?php
echo str_replace('\/','/',json_encode($images));
?>
You can also try JSON_UNESCAPED_SLASHES, like this:
<?php
echo json_encode($images,JSON_UNESCAPED_SLASHES);
?>
Related
How do I make a replacement result html tag when using Gii without having to change the yii core direct itself to change the outcome ???
Gii By default : framework\gii\generators\crud\templates\default
and result :
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title'); ?>
<?php echo $form->error($model,'title'); ?>
</div>
But if I want to get HTML Tag like :
<table>
<tr>
<td><?php echo $form->labelEx($model,'title'); ?></td>
<td>:</td>
<td><?php echo $form->textField($model,'title'); ?></td>
<td><?php echo $form->error($model,'title'); ?></td>
</tr>
</table>
Or in any other form appropriate style css and html that I have.
What should I do to get the results of Gii code generated like that?
The The Definitive Guide to Yii has an manual on extending Gii. You can find it here:
http://www.yiiframework.com/doc/guide/1.1/en/topics.gii#extending-gii
trying to get my head around REST, I am following/copying a tutorial. The "$_get" is blank, I noticed that the URL that is being called is blank here is a copy,
http://localhost/RestClient/index.php?action=get_user&id=
but the href I am clicking looks ok to me.
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
here is my code I am new to PHP so figuring it all as I go!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
The Link
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
is incorrect, it must be:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
Watch the changes in ' signs.
In you example $_GET['id'] must have been always null.
There is definitely something wrong with you <a>.
You are using single quotes for the tag attribute and then for query string parameters too.
Any program having to interpret that will have no idea where the href= actually ends.
One solution would be to use double quotes (") for the attribute and single quotes for the value (if you need those at all).
Change
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
to
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>
I've always wondered: does having a large open and close PHP (i.e. a template) hurt?
To clarify:
<html>
<?php echo $test; ?>
<body <?php echo $test2; ?>>
<table>
<?php foreach ($rows as $row): ?>
<tr>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<td><?php echo $row['cell1']; ?></td>
<?php echo $row['added cells']; ?>
</tr>
<?php endforeach; ?>
</table>
<?php echo $someMorePhp; ?>
<div>
<?php /*do some more php stuff */ ?>
</div>
etc etc
etc
Or would it be advisable to i.e.
<html>
<php echo $test.'<body'.$test2; ?>>
<table>
<?php foreach ($rows as $row) {
echo '<tr>
<td>'.$row['cell1'].'</td>
<td>'.$row['cell2'].'</td>
<td>'.$row['cell3'].'</td>
<td>'.$row['cell4'].'</td>
<td>'.$row['cell1'].'</td>
'.$row['added cells'].'
<tr>';
}
</table>
// etc
I know this might seem like a micro optimization and such. To be clear i'm looking for a rule-of-thumb not a specific usecase... Will it hurt entering and exiting the php engine during a single script run...
No, that is no problem, you should always opt for better readability in this case.
Also think about either activating shorttags if possible (but beware, those might have some sideeffects / problems with XML sytnax as stated in the comments)
<? if ($bool) { ?>
<?= $myVarThatIWantToOutput ?>
<? } ?>
Or think about using a template engine like smarty or twig, which restrict you somehow to force splitting of concerns, make some stuff easier (and more readable) and allow caching of compiled templates to make sure you still get the right speed.
i am trying to implement jquery datatable, in my cakePHP based website, but it just wont load. this website is already half developed, and from the way i see it, the js' is loaded through a file called _head.inc.ctp located inside the views/layouts folder, i have added the datatables library inside the libs folder which is webroot/js/libs and load it inside the _head.inc.ctp file.
suppose i have this:
my controller:
var $helpers = array(
'Form',
'Html',
'Javascript'
);
//my method
function dataTable_example($id=null){
$details = $this->Detail->find("all");
$this->set('details', $details );
}
my view:
<div>
<?php echo $javascript->link('libs/jquery.dataTables.js'); ?>
<script>
$(document).ready(function(){
$('#js-datatable').dataTable();
});
</script>
<h2><?php echo __l('Tickets');?></h2>
<div>
<table id="js-datatable">
<tr>
<th>some heading 1</th>
<th>some heading 1</th>
<th>some heading 1</th>
</tr>
<?php
if (!empty($details)){
foreach ($details as $detail):
?>
<tr>
<td><?php echo $detail['Detail']['id'];?></td>
<td><?php echo $detail['Detail']['created'];?></td>
<td><?php echo $detail['Detail']['ticket_detail'];?></td>
</tr>
<?php
endforeach;
}else{
?>
<tr>
<td>No Data Found</td>
</tr>
<?php }?>
</table>
</div>
</div>
i even hard coded it using the usual call, and checked it using firebug to see if the script is loaded or not, and according to firebug, it is loaded, so i cant see whats making the script fail my table.
did i missed some steps ? please help
thanks
You don't have thead and tbody elements as required by the datatables script
You should use the find function in your controller and pass the array to the view and in the view write it.. don't just leave the table empty
I want to use a custom template system in my php application,
What I want is I want to keep away my php codes from design, I would like to use a tpl file for designs and a php file for php codes
I dont want to use any ready maid scripts. Can any one point out some links link or useful info how to build a php templating system to achieve this
Thank you
The way I do it is to create a template file(.tpl if you wish) and insert markers which will be replaced with str_replace in PHP. The code will look something like this:
For template.tpl file
<body>
<b>Something: </b> <!-- marker -->
</body>
For the PHP
$template = file_get_contents('template.tpl');
$some_data = 'Some Text'; //could be anything as long as the data is in a variable
$template = str_replace('<!-- marker -->', $some_data, $template);
echo $template;
That's it in a nutshell but it can get a lot more complex. The marker can be anything as long as it's unique.
I want to keep away my php codes from design, I would like to use a tpl file for designs
...and mix your tpl codes with "design"!
what's the difference then? :)
PHP itself is efficient templating system.
And nowadays most developers agreed that dividing your PHP code to business logic part and display logic part is most preferable way.
It can be very limited subset of PHP of course. You will need an output operator (<?=$var?>) one, a condition <? if(): ?>...<? endif ?>, a loop <? foreach(): ?>...<? endforeach ?> and include.
An example of such a template:
<table>
<? foreach ($data as $row): ?>
<tr>
<td><b><?=$row['name'] ?></td>
<td><?=$row['date'] ?></td>
</tr>
<tr>
<td colspan=2><?=$row['body'] ?></td>
</tr>
<? if ($row['answer']): ?>
<tr>
<td colspan=2 valign="top">
<table>
<tr>
<td valign="top"><b>Answer: </b></td>
<td><?=$row['answer'] ?></td>
</tr>
</table>
</td>
</tr>
<? endif ?>
<? if($admin): ?>
<tr>
<td colspan=2>
<? if($row['del']): ?>
show
<? else: ?>
hide
<? endif ?>
edit
</td>
</tr>
<? endif ?>
<? endforeach ?>
</table>