Drop Down list in php (help) - php

Well, this is my first question in stackoverflow. I hope that you can help me!
I would like to make a select box or a drop down list, if you prefer
So, i have this php code:
<?php $i = 0; ?>
<?php $orders = osc_list_orders();
foreach($orders as $label => $params) {
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1'; ?>
<?php if(osc_search_order() == $params['sOrder'] && osc_search_order_type() == $orderType) { ?>
<a class="current" href="<?php echo osc_update_search_url($params); ?>"><?php echo $label; ?></a>
<?php } else { ?>
<?php echo $label; ?>
<?php } ?>
<?php if ($i != count($orders)-1) { ?>
<span>|</span>
<?php } ?>
<?php $i++; ?>
<?php } ?>
I have tried to do something like this:
function osc_list_orders_drop() {
$orders = osc_list_orders();
echo '<select name="order" id="order" ONCHANGE="location = this.options[this.selectedIndex].value;">';
foreach($orders as $label => $params){
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1';
echo '<option value="'.osc_update_search_url($params).'">';
echo $label;
echo'</option>';
}
echo'</select>';
}
But the first option is not stored, so when i select one option and then try to select the first one, the first one never works.
Can you help me?
Thx

Try adding a blank option tag in the select, it will allow the change to trigger.
function osc_list_orders_drop() {
$orders = osc_list_orders();
echo '<select name="order" id="order" ONCHANGE="location = this.options[this.selectedIndex].value;">';
echo '<option></option>';
foreach($orders as $label => $params){
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1';
echo '<option value="'.osc_update_search_url($params).'">';
echo $label;
echo'</option>';
}
echo'</select>';
}
A better option would be to add a selected="selected" to the current filter's option tag.

Related

WordPress: How to order child pages in a select menu?

I have a select box, which allows you to navigate between parent pages and their children, they are given a specific order, in order to display them in a certain way on the website.
I believe that the line of code that interest you is perhaps this one
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
The full code is below. You can see that the order of the pages on the site is different than the order of the pages in the select menu, that is because wordpress sorts per order, my pages are sorted alphabetically. How to sort them by order, so that what you see on the website matches the select box?
<?php
// determine parent of current page
if ($post->post_parent)
{
$ancestors = get_post_ancestors($post->ID);
$parent = $ancestors[count($ancestors) - 1];
}
else
{
$parent = $post->ID;
}
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0");
?>
<div id="info-select-wrap" class="single-france-select">
<select id="info-select"class="fr-select" >
<?php
if (get_the_ID() == $parent->ID): ?>
<option value="<?php
echo get_the_permalink($parent->ID); ?>" selected>  <?php
echo get_the_title($parent->ID); ?></option>
<?php
else: ?>
<option value="<?php
echo get_the_permalink($parent); ?>">  <?php
echo get_the_title($parent); ?></option>
<?php
endif;
foreach($children as $child):
if (has_children($child))
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected>  <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
else
{
if (get_the_ID() == $child->ID)
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>" selected>  <?php
echo get_the_title($child->ID); ?></option>
<?php
}
else
{ ?>
<option value="<?php
echo get_the_permalink($child->ID); ?>"> <?php
echo get_the_title($child->ID); ?></option>
<?php
}
}
endforeach; ?>
</select>
</div>
I tried
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&orderby='menu_order'&order='ASC'");
Didn't solve it.
Issue solved by adding sort_column=menu_order
$children = get_pages("title_li=&child_of=" . $parent . "&echo=0&sort_column=menu_order");
Just a quick note, &echo=0 is useless, you can remove it.

Preselect option dynamically dropdown PHP

I have an array $faecher to create an SELECT Field with options. My problem is that I want to pre-select an option which is saved in variable f_name1.
<select>
<?php
for ($i = 0; $i <= count($faecher); $i++)
echo "<option" if ($faecher[$i] == f_name1) echo 'selected="selected"'; ">".$faecher[$i]."</option>";
?>
</select>
When you'll learn to indent you code, it will be easier:
echo "<option";
if ($faecher[$i] == f_name1) {
echo ' selected="selected"';
}
echo ">".$faecher[$i]."</option>";
Try:
echo "<option".($faecher[$i] == $f_name1 ? ' selected="selected"' : null).">".$faecher[$i]."</option>";
or
echo "<option".($faecher[$i] == $f_name1 ? ' selected="selected"' : '').">".$faecher[$i]."</option>";
What is f_name1 here? You should use direct string here. Like below
<select>
<?php for ($i = 0; $i <= count($faecher); $i++) { ?>
<option <?php echo $faecher[$i] == "Matching String" ? "selected" : ""; ?>><?php echo $faecher[$i]; ?></option>
<?php } ?>
</select>

Replace &ct_= with %2c or comma

I've been wrestling with this function for far to long and I wish I had enough respect to offer a bounty. Help would be greatly appreciated.
// Advanced Search Check
function ct_search_form_check($name, $taxonomy_name = null) {
global $search_values;
if (!$taxonomy_name) {
$taxonomy_name = $name;
} ?>
<?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t) : ?>
<?php if ($search_values[$name] == $t->slug) { $selected = 'checked="checked"'; } else { $selected = ''; } ?>
<div><input id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>" type="checkbox" style="margin-right:5px; margin-left:5px" <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?><span style="margin-left:10px"></span></input></div>
//recently added this part to replace duplicate taxonomy_name in url
<?php $data = array();
while (list($name, $t->slug) = each($arr)) {
$data[] = "$name";
}
echo implode($data); ?>
<?php endforeach; ?>
<?php
}
How do I change my output from
?ct_zipcode=&ct_event_type=alumni&ct_=anneverisy&ct_setting=ballroom&ct_=bar&Venue-search=true
to
?ct_zipcode=&ct_event_type=alumni%2canneverisy&ct_setting=ballroom%2cbar&Venue-search=true
This worked. hopefully it will help someone else
function ct_search_form_check($name, $taxonomy_name = null) {
global $search_values;
if (!$taxonomy_name) {
$taxonomy_name = $name;
} ?>
<input type="hidden" value="" name="ct_<?php echo $name; ?>" />
<?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t) : ?>
<?php if ($search_values[$name] == $t->slug) { $selected = 'checked="checked"'; } else { $selected = ''; } ?>
<div><input id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>" type="checkbox" style="margin-right:5px; margin-left:5px" <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?><span style="margin-left:10px"></span></input></div>
<?php
$data = array();
while (list($name, $t->slug) = each($arr)) {
$data[] = "$name";
}
echo implode($data);
if (!empty($_GET['ct_'])) {
$url = str_replace('&ct_=', '%2c', $_SERVER['QUERY_STRING']);
header("Location: ?$url");
} ?>
<?php endforeach; ?>
<?php
}

Codeigniter issue with loop and Condition

I am loading a view like this
if($type == 'view'){
// do something
}else if($type == 'insert'){
// Here i am making a form
?>
<select>
<?php
foreach($applications as $row)
{
?>
<option value = "<?php echo $row->id;?>">
<?php echo $row->name;?>
</option>
<?php } ?>
</select>
<?
}else{
//do some thing else
}
Now this is the error i am encountering:
Parse error: syntax error, unexpected T_ELSE in url on line xx
The weird thing is that if i comment out the loop if works fine. What is the problem and how can it be resolved?
you need to use codeigniter loops in view way like this:
<?php foreach ($applications as $row):?>
<option value = "<?php echo $row->id; ?>">
<?php echo $row->name; ?>
</option>
<?php endforeach;?>
It works fine for me... try again
<?php
if($type == 'view'){
// do something
}else if($type == 'insert'){
// Here i am making a form
?>
<select>
<?
foreach($applications as $row){
?><option value = "<?php echo $row->id;?>"><?php echo $row->name;?></option>
<?php
}
?>
</select>
<?
}else{
//do some thing else
}
Using switch, you have a more elegant way of code writing and of course faster, try:
switch($type){
case 'view':
//do something
break;
case 'insert':
echo '<select>';
foreach($applications as $row){
echo '<option value="'.$row->id.'">'.$row->name.'</option>';
}
echo '</select>';
break;
default:
//do something else
break;
}
Maybe your want like this code:
<?php
if ($type == 'view')
{
// do something
}
elseif ($type == 'insert')
{
// Here i am making a form
?>
<select>
<?php
foreach($applications as $row)
{
?>
<option value = "<?php echo $row->id;?>"><?php echo $row->name;?></option>
<?php
}
?>
</select>
<?php
}
else
{
//do some thing else
}
?>

PHP Select Box...after selection help needed (yii)

I am using a script that runs on the YII framework...
I have this select box that lets a user view content based on their country. But right now, when a user selects their country, after the selection, it just shows the country name...
And what I want is, after a user selected their country... it must still show the select box, but have the selected country at the top of the list.
I have tried so many things but I'm still new to this this.
<?php if ( $_country == '' ) { ?>
<select onchange="changeCountry(this)" name="country" id="country">
<?php if ( count ($c) != 0 ) { ?>
<?php if ( $_country == NULL ) ?>
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
<?php } ?>
</select>
<?php } else { ?>
<span class="countryName"><?php echo $countryName; ?></span>
<?php } ?>
I didn't try but following should work
<select onchange="changeCountry(this)" name="country" id="country">
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
</select>

Categories