result should be obtained as comma separated - foreach

My code for the slide looks like this in the static form.
<div id="demo-1" data-zs-src='["<?php echo base_url();?>assets/images/s1.jpg", "<?php echo base_url();?>assets/images/sd1.jpg", "<?php echo base_url();?>assets/images/slide7.jpg", "<?php echo base_url();?>assets/images/sd3.jpg"]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
and when i change it into dynamic i changed my code like this
<?php foreach($slide1 as $ss){?>
data-zs-src='["<?php echo base_url();?>uploads/<?php echo $ss->image;?>" ]' alt=""
data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
My problem is the slide will work only when comma is added after each image for that i thought to add implode function but its not working properly and my code with implode looks like this
<div id="demo-1" data-zs-src='[<?php foreach( $slides as $slide ){ $images="<?php echo base_url();?>uploads/<?php echo $slide->image;?>";
?>"<?php echo implode(',',$images);?>"<?php }?>]'
data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>

So you have to think about what it is you are trying to achieve here...
You seem to be trying to perform an implode on each string inside the foreach loop... Apart from the errors that would have been generated, it doesn't make sense to perform that in each loop. Its something you do after you have created the array entries... Implode works on arrays, not strings. It generates a string from an array.
Basically you are wanting to take an array of objects and want to convert those into a comma delimited string.
I'm not sure why you insist of putting ALL the code inside the HTML code where you want it to appear... I'd separate it out like this...
<?php foreach ($slides as $slide) {
$images[] = base_url('uploads/') . $slide->image;
} ?>
<div id="demo-1" data-zs-src='["
<?= implode(',', $images); ?>
"]'
data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
Step 1: Create your Array of images
<?php foreach ($slides as $slide) {
$images[] = base_url('uploads/') . $slide->image;
} ?>
This can be done in your controller or in a helper but in your view is just as good. Depends on how you like to structure your code.
Step 2: Put the resulting string where you want it.
<div id="demo-1" data-zs-src='["
<?= implode(',', $images); ?>
"]'
data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
You can refactor this even more but I have taken the first step in refactoring it so it hopefully makes sense to you.
NOTE: I have used <?= instead of <?php echo ( available since PHP 5.4.0) so if you are using something older than 5.4.0 stick with <?php echo.

Related

How to wrap input element inside div in ZF2

I am following this link.
In view I have
echo $this->formRow($product->get('name'));
and in source it is showing like
<label>Name of the product<input name="product[name]" required="required" type="text" value=""></label>
But I want like this
<div><label>Name of the product</label></div>
<div><input name="product[name]" required="required" type="text" value=""></div>
Maybe this question asked somewhere but I am not able to find it.
I am using php 5.6.
EDIT:
According to the answer I was able to solve this issue.Following the documentation in this link. I have issue in this part
echo $this->formCollection($product->get('categories'));
I tried like this
echo "<div>".$this->formLabel($this->get('categories'))."</div>";
echo "<div>".$this->formInput($this->get('categories'))."</div>";
But it throws fatal error.
Catchable fatal error: Object of class Zend\Form\View\Helper\FormLabel could not be converted to string in /opt/lampp/htdocs/zend2/module/Test/view/test/index/testform.phtml on line 39
How can I fix this?
instead of $this->formRow, You can use the below code and can separate the label and input both.
echo $this->formLabel($form->get('name'));
echo $this->formInput($form->get('name'));
It will output this:
<label>Name of the product</label>
<input name="product[name]" required="required" type="text" value="">
And then you can use the div tag in echo. So here is what you want:
echo "<div>".$this->formLabel($form->get('name'))."</div>";
echo "<div>".$this->formInput($form->get('name'))."</div>";

Styling single jQuery UI controlgroup-item

I am using jQuery UI controlgroup to style checkboxes in an HTML form. After the input values are processed by a PHP script, the results are displayed on the the same page along with the form itself, so that the user can adjust the filters. What I am trying to do, is to have the boxes that were checked previously remain checked after the form has been processed, so that the user sees what selection criteria were used. To achieve that I store all the PHP $_POST data in a JS variable using json_encode, which I'd like to use to iterate through the labels and mark those that were checked previously. The problem is that the only option of the controlgroup widget that I can use is classes with ui-controlgroup-item which shows every single label within the group as active, and for the life of me I cannot figure out how to make it conditional, e.g. so that I can use if(label[for=' + var.value +'])', var being <?php echo json_encode($_POST) ?> or something similar. Will appreciate any suggestions.
Here is the HTML:
<div id="currencyList">
<label for="gbp">GBP</label>
<input type="checkbox" value="gbp" name="currency[]" id="gbp" >
<label for="usd">USD</label>
<input type="checkbox" value="usd" name="currency[]" id="usd">
<label for="eur">EUR</label>
<input type="checkbox" value="eur" name="currency[]" id="eur">
</div>
And this is the JavaScript bit:
$( "#currencyList" ).controlgroup({
classes: {
"ui-controlgroup-item": "ui-checkboxradio-checked ui-state-active"
}
});
After trying to find a solution for several days I decided to skip trying via the classes option and instead to move outside the controlgroup widget. So here is my not-so-pretty-but-working solution:
var postData = <?php echo json_encode($_POST) ?>;
$( "#currencyList" ).controlgroup();
$('#currencyList').children('label').each(function () {
if(postData.currency.indexOf($(this).attr("for")) >= 0){
$(this).addClass( "ui-checkboxradio-checked ui-state-active");
}
});

Jquery mobile How to add a defualt value to a filter in list view?

I need to add a default value to a filter in Jquery mobile 1.3.0.
But the example below do not work.
<ul data-role="listview" data-autodividers="false" data-filter="true" data-inset="true" id="listviewID">
<?php foreach($countryList as $country): ?>
<li value="<?php echo trim(country); ?>"><?php echo trim(country); ?></li>
<?php endforeach; ?>
</ul>
$(document).ready(function() {
$(".ui-input-text.ui-body-c").val('Canada');
$('#listviewID').listview('refresh');
});
Refreshing the listview wont work, as it has already been rendered. The hack is modifying the input and firing an change event.
$(document).ready(function(){
$("#listviewID").prev("form").find("input[data-type=search]").val('Canada').trigger("change");
});
Note that the selector looks for the inmediate previous form (automatically created by jQM when you specify filterable) and the input with [data-type=search], thats more specific than .ui-input-text.ui-body-c.
FIDDLE

Using a str_replace within a php tag

Can someone please help me with what I am doing wrong here. Quite a novice when it comes to PHP
Here is my code:
<?php echo $breadcrumb['text']; str_replace("<br />",""); ?>
I think the correct way of executing that is this.:
echo str_replace("<br />", "", $breadcrumb['text']);
In this case, you want to replace all br-tags with "" in the $breadcrumb['text'] variable.
Look at the documentation for that function here: http://php.net/manual/en/function.str-replace.php
If I understand:
<?php
$breadcrumb['text'] = "Something <br /> Next line but now on first <br />";
str_replace("<br />","", $breadcrumb['text']);
echo $bread;
?>

Get Textarea Value with Simpe HTML Dom

i using simple_html_dom.php
how to get textarea value if the website has used bad tag.
the textarea tag already closed before </textarea> like input tag.
Textarea HTML like below:
<textarea name="xxx" id="xxx" />this is value</textarea>
When i use this function, i dont get anything
$textarea = $html->find("textarea[name=xxx]");
$contents = $textarea->innertext;
echo $contents;
how to get 'this is value' using simple_html_dom.php or other alternative?
Thank you
Well, my previous comment won't work in this case, I'll leave it for info though...
Another approach is to clean it up before parsing it with simple_html_dom using Tidy extension. But it seems not to be working here either...
A last approach I can think of, and if this is your only problematic case, is to use regex to get what you want:
Using <textarea.*?name="xxx".*?id="xxx".*?\/>([^<]+)<\/textarea> ==> RegEx DEMO
The output will be in group one of the resulting array $match. Check this working code:
$input = <<<_DATA_
<textarea name="xxx" id="xxx" />this is value</textarea>
_DATA_;
$pattern = '/<textarea.*?name="xxx".*?id="xxx".*?\/>([^<]+)<\/textarea>/';
preg_match($pattern, $input, $match);
var_dump($match)
Working DEMO
It is easy to get the value of a Teaxtarea in javascript:
<script type=text/javascript>
function getValueTextarea()
{
var vl=document.getElementById("tx").value;
alert(vl);
}
</script>
<body>
<textarea id="tx">Value Of Textarea</textarea>
<input id="button" value="Get Value" onclick="getValueTextarea()">
</body>

Resources