I am creating a few shortcodes In the functions.php file for our wordpress site and am working on how best to implement it.
A selection list of 240 countries that can be chosen from will be in a few different shortcodes and instead of placing the entire list 4 or 5 times I was wondering if we could implement it one time and make the call from within the function.
This is what we have right now:
function form_function() {
ob_start();
?>
...
<select id="fc" name="fc" title="From Country">
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select>
...
<select id="tc" name="tc" title="To Country">
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select>
...
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
and we would like to change it to something like:
function countrylist() {
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
}
function form_function() {
ob_start();
?>
...
<select id="fc" name="fc" title="From Country">
<? countrylist () ?>
</select>
...
<select id="tc" name="tc" title="To Country">
<? countrylist () ?>
</select>
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
Any thoughts on how best to do this? Thanks in advance
Maybe better change countrylist() function to
function countrylist()
{
$countries = array('USA', 'Canada', 'Ukraine');
foreach($countries as $country)
{
$html .= '<option value="'.$country.'">'.$country.'</option>'
}
return $html;
}
And in form_function() replace <? countrylist () ?> with <?= countrylist () ?>
Related
Hi i'm doing a shop for a university project. This is my first course in web development and I don't understand how to sort a shoplist page by an html dropdown select in laravel.
Frontend Blade
<div class="toolbar-sorter">
<span>Sort By</span>
<select name="sorter" class="sorter-options" style="width:150px; "data-role="sorter">
<option selected="selected" value='comic_name'>Titolo: A-Z </option>
<option value='comic_name'> Titolo: Z-A </option>
<option value='price'> Prezzo: Crescente </option>
<option value='price'> Prezzo: Decrescente </option>
<option value='created_at'> Ultimi Arrivati </option>
</select>
</div>
Route.php (This is not the only route of shoplist)
Route::get('/shoplist', 'ComicController#shoplistBase');
ComicController
public function shoplistBase()
{
$genres = Genre::all();
$comics = Comic::paginate(9);
return view('shoplist')->with(compact('genres'))->with(compact('comics'));
}
Assuming that you are using the same route for your sorting:
Wrap your drop-down in a form
Make sure to use unique values for each options
<form action="/shoplist" method="GET">
<div class="toolbar-sorter">
<span>Sort By</span>
<select name="sorter" class="sorter-options" style="width:150px; " data-role="sorter">
<option selected="selected" value='comic_name_asc'>Titolo: A-Z</option>
<option value='comic_name_desc'> Titolo: Z-A</option>
<option value='price_asc'> Prezzo: Crescente</option>
<option value='price_desc'> Prezzo: Decrescente</option>
<option value='created_at'> Ultimi Arrivati</option>
</select>
</div>
<button type="submit">Filter</button>
</form>
Handle the filter request in your Controller
public function shoplistBase(Request $request)
{
$genres = Genre::all();
if ($request->has('sorter')){
switch($request->get('sorter')){
case `comic_name_asc`:
$comics = Comic::orderBy('name', 'desc')->paginate(9);
break;
case `comic_name_desc`:
//..
break;
}
} else {
$comics = Comic::paginate(9);
}
return view('shoplist')->with(compact('genres'))->with(compact('comics'));
}
I have a form to add product to cart and inside it there is a link.
This is link
<?php
echo $this->Html->link('<div class="single-products">'.'<div class="productinfo text-center myimg">'.$this->Html->image("product/".$row["Product"]["photo"],array(/*"width"=>"2500px",*/"height"=>"250px")).'<h2> ₹ '.$row["Product"]["price"].'</h2>'.'<p>'.$row["Product"]["name"]."</p><a href='javascript:document.ff".($i++).".submit()' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>".'</div>'.'</div>',
array
(
'controller'=>'Public',
'action'=>'singleproduct?id='.$row["Product"]["id"],
),
array
(
'escape'=>false //NOTICE THIS
)
);
?>
And I want to use this code just above Add to cart button
<?php
if($row["Product"]["psize"]==1)
{
?>
Size<select name="psize">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<?php
}
elseif($row["Product"]["psize"]==2)
{
?>
Size<select name="psize">
<option value="28">28</option>
<option value="30">30</option>
<option value="32">32</option>
<option value="34">34</option>
</select>
<?php
}
?>
This code is working if I put it outside of this HTML helper link, but because of design problem and I want to display it just above the add to cart button
I have tried but couldn't figure out how to put this inside link.
<?php
if($row["Product"]["psize"]==1)
{
?>
Size<select name="psize">
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<?php
} else{
if($row["Product"]["psize"]==2){
?>
Size<select name="psize">
<option value="28">28</option>
<option value="30">30</option>
<option value="32">32</option>
<option value="34">34</option>
</select>
<?php
} else{
echo "TEY IT"
}
}
?>
May Be it gone help.
Bring out your codes in the function $this->Html->link(), assign it to a variable and use if condition. And i think you should use CakePHP Form Helper to output select box.
Example:
<?php
$select = $this->Form->input('psize', array(type => 'select', 'options' => $sizeOptions)); // you can use if conditions here
$link = $this->Html->link('<div>...</div>' . $select . '<div>...</div>', $yourUrlArr);
echo $link;
I want to generate select boxes based on the number of elements in the $chosen array and each of them will have the default selected options. The first one is A, and the second one is D. The following code is almost okay except I want to remove the duplicated options. Here's the output:
<select>
<option>Select</option>
<option value="A" selected="selected">A</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
<select>
<option>Select</option>
<option value="D" selected="selected">D</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
I tried array_merge with array_unique to merge $options and $chosen before foreach($options as $option) but it isn't working. Can anyone suggest a solution?
<?php
$options = array("A","B","C","D","E");
$chosens = array("A","D");
foreach($chosens as $chosen)
{
print "<select><option>Select</option>";
if(in_array($chosen,$options))
{
print "<option value='".$chosen."' selected='selected'>$chosen</option>";
}
/* $options = array_unique(array_merge($chosen,$options)); */
foreach($options as $option)
{
print "<option value='".$option."'>$option</option>";
}
print "</select>";
}
?>*
Actually, just a simple if condition is enough. Like this:
<?php
$options = array("A","B","C","D","E");
$chosens = array("A","D");
?>
<?php foreach($chosens as $chosen): ?>
<select name="">
<?php foreach($options as $option): ?>
<option value="<?php echo $option; ?>" <?php echo ($option == $chosen) ? 'selected' : ''; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select><br/>
<?php endforeach; ?>
I have this php code that input selected="" at a specific url. I need selected="" to in inserted into my html.
<?php
$uri = $_SERVER['REQUEST_URI'];
if ( strpos($uri,'retailers/amazon/?sort=2') !== false ) {
echo 'selected=""';
} else {
echo 'test';
}
?>
This is the html, I need to insert selected=""... Something like this
<option selected="" value="retailers/amazon/?sort=2">Newest to Oldest</option>
...
<select onchange="window.location=this.value;">
<option value="">Select</option>
<option selected="" value="retailers/amazon/?sort=2">Newest to Oldest</option>
<option value="retailers/amazon/?r_sortby=highest_rated&r_orderby=desc">Success Rate: High to Low</option>
<option value="retailers/amazon/?r_sortby=highest_rated&r_orderby=asc">Success Rate: Low to High</option>
<option value="retailers/amazon/?sort=0">Most Comments</option>
</select>
How about this...
<?php
$uri = $_SERVER['REQUEST_URI'];
$sort2 = strpos($uri, 'retailers/amazon/?sort=2');
?>
<option <?php echo ($sort2 === false? '' : 'selected=""'); ?> value="retailers/amazon/?sort=2">Newest to Oldest</option>
I assume you are trying to access the echo command outside the php tag and somewhere in html
it is not possible but there is an solution for this.
so you can try this option.
<select onchange="window.location=this.value;">
<option value="">Select</option>
<option selected="?php echo $uri?" </option>
</select>
its mean you are using the php code within the html area and you can access every variable of php by this.
I would like to select the option contained within a php variable on page load.
<?php
$pre = 78;
?>
<select id="form8" name="form8">
<option value="15">15</option>
<option value="25">25</option>
<option value="64">64</option>
<option value="78">78</option>
<option value="82">82</option>
</select>
<script>
$(document).ready(function(){
$("form8").val("<?php echo $pre; ?>");
}
</script>
Expected output should be,
<select id="form8" name="form8">
<option value="15">15</option>
<option value="25">25</option>
<option value="64">64</option>
<option value="78" selected="selected">78</option>
<option value="82">82</option>
</select>
http://jsfiddle.net/qQZVN/1/
Example
$('#form8').val("<?php echo $pre; ?>") ;
Why not just use HTML to set selected='selected'? Seems easier, and supports non-JS users.
$(document).ready(function(){
$("#form8").val(<?php echo $pre; ?>);
}
$('select#form8').val('<?php echo $pre; ?>');