Drop down does not show the option in correct format selected in codeigniter - codeigniter-3

<datalist id="stoplist">
Array
(
[0] => Array
(
[stops] => katraj dairy
)
[1] => Array
(
[stops] => bharati vidyapith
)
[2] => Array
(
[stops] => balaji nagar
)
[4] => Array
(
[stops] => k k market
)
)
</datalist>
This is my view
foreach ($data1 as $row) {
echo "<option value=".$row['stops'].">".$row['stops']."</option>";
}
When I select katraj dairy I get only katraj as a value.
I am getting correct value from database as I print above. What is the problem?

From your controller you need to pass data from your controller before you can access it from your view.
&this->load->view("viewname.php",$data)
Where $data is the array you get from your database
format for select option
<select>
<option value="">name</option>
</select>
Modified code:
echo "<select>";
foreach ($data1 as $row) {
echo "<option value=".$row['stops'].">".$row['stops']."</option>";
}
echo "</select>";

Related

How to get List for Contact Form 7

I have 2 contact forms created by Contact Form 7.
How to list all contact forms created through shortcode?
Please check images, tks.
Updated:
this is my code, this working!
$args = array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1);
$rs = array();
if( $data = get_posts($args)){
foreach($data as $key){
$rs[$key->ID] = $key->post_title;
}
}else{
$rs['0'] = esc_html__('No Contact Form found', 'text-domanin');
}
Below is a dropdown list:
<select name="field-name" id="field-id">
<option value="">--Select--</option><?php
$dbValue = get_option('field-name'); //example!
$posts = get_posts(array(
'post_type' => 'wpcf7_contact_form',
'numberposts' => -1
));
foreach ( $posts as $p ) {
echo '<option value="'.$p->ID.'"'.selected($p->ID,$dbValue,false).'>'.$p->post_title.' ('.$p->ID.')</option>';
} ?>
</select>

Retrieving values of Form::select

I have a select box with an array of data to populate it like this:
{!! Form::select('language',$languageArray,'null', ['id'=>'language','multiple',]) !!}
I am passing $languageArray with view , and is simply an array of values like ['A','B','C']...
Now while fetching the selected values i am getting numeric value of selected options. Is there a way to change the values from numeric indexes to Text written in option. i did it using an associative array as second argument like this:
['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..']
But it creates a long list and view seems to be overloaded with data is there a better way for achieving below output ???
<select name="language">
<option value="english" ">English</option>
<option value="french">French</option>
....
I suggest you to use Config values ,
Create a file like languages.php in config folder.
<?php
return ['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..'
'german' => Lang::get('languages.german')
];
View :
{!! Form::select('language',Config::get('languages'),'null', ['id'=>'language','multiple',]) !!}
As you can see, in this way you can use config values in another view too and support multi language(its important too.. look at 'german')
Last Option is creating your own Form macro for example,
Form::macro('slugiySelect', function($name , $list , $selected = null , $options = [])
{
$options = [];
foreach($list as $row)
$options[Str::slug($row)] = $row;
return Form::select($name , $options , $selected , $options);
});
In this macro your options array is slugify with Laravel string function and uses as a key , I kept value as yours. You can do it with your way.

$selected attribute not working in codeigniters form_dropdown()

I have an editform with a dropdown for choosing a category.
I also made a function for getting the category from the database.
I set an array $opties for the dropdown to select form and a 'selected' parameter called $selectie this one contains the category for that specific company in the database.
It looks like this:
<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_dropdown('categorieen', $opties, $selectie); ?></td>
</tr>
Question
Why is my 'selected' part not working?
When I do a print_r($selectie) I get:
Array
(
[12] => Vlaggen
)
which is my category in the database.
When I do a print_r($opties) I get:
Array
(
[11] => Webdesign
[12] => Vlaggen
[13] => Auto-s
[14] => Electronica
[15] => Boeken
[16] => Antiek-en-Kunst
[17] => Auto-Onderdelen
[18] => Computers-Hardware
[19] => Computers-Software
)
I don't know what the problem is.
Some help would be appreciated.
Controller:
function updatebedrijven()
{
$dbres = $this->db->get('categorieen');
$ddmenu = array();
foreach ($dbres->result_array() as $tablerow) {
$ddmenu[$tablerow['idcategorieen']] = $tablerow['Categorie'];
}
$data['opties'] = $ddmenu;
$id = $this->uri->segment(3);
$id2 = $this->uri->segment(3);
$data['selected'] = $this->members_model->getselection($id2);
$data['info'] = $this->members_model->getbedrijf($id);
$data['id'] = $id;
$this->load->view('members/header');
$this->load->view('members/editform', $data);
$this->load->view('members/footer');
}
Model:
//This one is for the $opties
function getbedrijf($id)
{
$this->db->where('idbedrijven', $id);
$query = $this->db->get('bedrijven');
if ($query->num_rows() == 1) {
$row = $query->row_array(0);
return $row;
}
}
//This one is for the $selectie
function getselection($id2)
{
$this->db->from('bedrijfcategorieen');
$this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen');
$this->db->where('bedrijfcategorieen.idbedrijven', $id2);
$query = $this->db->get();
return $query->result();
}
form_dropdown uses the arrays key => value as its 'value' and 'option',
to select a data from your dropdown you will need the key of your selected data for example you choose
[12] => Vlaggen to select it you need its key which is 12, and not an array like
Array
(
[12] => Vlaggen
)
<?
//just for testing
echo form_dropdown('categorieen', $opties, key($selectie));
?>

zend-form select optgroup, how to specify id

Hello i am using Zend Framework Form and have tried to get this example to work http://framework.zend.com/issues/browse/ZF-8252, but it fails xD
this is my code
$options = Array
(
[] => Qualsiasi Agente
[agenti_attivi] => Array
(
[4] => Giovanni Abc
[10] => Luigi Abc
[13] => Michela Abc
)
);
$agenti->addMultiOptions($options);
and the generated code is :
<select name="agente_id" id="agente_id" tabindex="6">
<option value="" label="Qualsiasi Agente" selected="selected">Qualsiasi Agente</option>
<optgroup id="agente_id-optgroup-Agenti attivi: " label="Agenti attivi: ">
<option value="4" label="Giovanni Abc">Giovanni Abc</option>
<option value="10" label="Luigi Capoarea">Luigi Abc</option>
<option value="13" label="Michela Abc">Michela Abc</option>
</optgroup>
</select>
where id="agente_id-optgroup-Agenti attivi: " is not xhtml valid Line 724, Column 44: value of attribute "id" must be a single token
i am using zend 1.11.10
thanks
Create a custom view helper FormSelect that extends the core FormSelect and then modify the code.
Include the path to your view helpers in the bootstrap file
protected function _initHelpers()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
The custom view helper. It's a copy of Zend_View_Helper_FormSelect but with small modification.
class My_View_Helper_FormSelect extends Zend_View_Helper_FormSelect
{
public function formSelect($name, $value = null, $attribs = null,
$options = null, $listsep = "<br />\n")
{
$info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
extract($info); // name, id, value, attribs, options, listsep, disable
// force $value to array so we can compare multiple values to multiple
// options; also ensure it's a string for comparison purposes.
$value = array_map('strval', (array) $value);
// check if element may have multiple values
$multiple = '';
if (substr($name, -2) == '[]') {
// multiple implied by the name
$multiple = ' multiple="multiple"';
}
if (isset($attribs['multiple'])) {
// Attribute set
if ($attribs['multiple']) {
// True attribute; set multiple attribute
$multiple = ' multiple="multiple"';
// Make sure name indicates multiple values are allowed
if (!empty($multiple) && (substr($name, -2) != '[]')) {
$name .= '[]';
}
} else {
// False attribute; ensure attribute not set
$multiple = '';
}
unset($attribs['multiple']);
}
// now start building the XHTML.
$disabled = '';
if (true === $disable) {
$disabled = ' disabled="disabled"';
}
// Build the surrounding select element first.
$xhtml = '<select'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. $multiple
. $disabled
. $this->_htmlAttribs($attribs)
. ">\n ";
// build the list of options
$list = array();
$translator = $this->getTranslator();
foreach ((array) $options as $opt_value => $opt_label) {
if (is_array($opt_label)) {
$opt_disable = '';
if (is_array($disable) && in_array($opt_value, $disable)) {
$opt_disable = ' disabled="disabled"';
}
if (null !== $translator) {
$opt_value = $translator->translate($opt_value);
}
$opt_id = ' id="' . $this->formatElementId($id . '-optgroup-' . $opt_value) . '"';
$list[] = '<optgroup'
. $opt_disable
. $opt_id
. ' label="' . $this->view->escape($opt_value) .'">';
foreach ($opt_label as $val => $lab) {
$list[] = $this->_build($val, $lab, $value, $disable);
}
$list[] = '</optgroup>';
} else {
$list[] = $this->_build($opt_value, $opt_label, $value, $disable);
}
}
// add the options to the xhtml and close the select
$xhtml .= implode("\n ", $list) . "\n</select>";
return $xhtml;
}
private function formatElementId($id)
{
// in here put whatever filter you want for the id value
$id = trim(strtr($id, array('[' => '-', ']' => '', ' ' => '', ':' => '')), '-');
$id = strtolower($id);
return $id;
}
}
Done. Create multi select element with a valid id.
<?php
$this->addElement('multiSelect', 'agente_id', array(
'label' => 'Label Name:',
'multiOptions' => array(
'' => 'Qualsiasi Agente',
'Agenti attivi: ' => array(
4 => 'Giovanni Verdi',
10 => 'Luigi Capoarea',
13 => 'Michela Passarin',
)
)
));
try this, it's works for me:
$select = new Zend_Form_Element_Select('select');
$options = Array(
'' => 'Qualsiasi Agente',
'agenti_attivi' => Array(
4 => 'Giovanni Verdi',
10 => 'Luigi Capoarea',
13 => 'Michela Passarin'
)
);
$this->addElements(array($xxxx,$select,$yyyy)); // $this : the form instance
and the result is:
<select id="select" name="select">
<option label="Qualsiasi Agente" value="">Qualsiasi Agente</option>
<optgroup label="agenti_attivi">
<option label="Giovanni Verdi" value="4">Giovanni Verdi</option>
<option label="Luigi Capoarea" value="10">Luigi Capoarea</option>
<option label="Michela Passarin" value="13">Michela Passarin</option>
</optgroup>
</select>
the problem is that the id attribute does not accept spaces and special special characters:
id="agente_id-optgroup-Agenti attivi: "
Zend is usually pretty good about rendering the proper html, given a doctype.
Try setting your doctype like this if you aren't already.
<?php
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
$viewRenderer->view->doctype('XHTML1_STRICT');
AND
<?php echo $this->doctype(); ?>
at the top of your layout
I don't have a install of ZF i can mess with easy, if this doesn't work ill setup a test environment.

Parse Facebook API data using loop for getting fan page ID#s?

I've been learning how to parse json data returned from the facebook->api. I've figured out how to fetch fan pages from a specific profile id and want to parse them using a loop!
Heres the code and example I have below:
This is the data I get back from the facebook->api
Array ( [0] => Array (
[page_id] => XXXXXX60828 ) [1] => Array (
[page_id] => XXXXXX0750 ) [2] => Array (
[page_id] => XXXXXX91225 ) [3] => Array (
[page_id] => XXXXXX1960343 ) [4] => Array (
[page_id] => XXXXXX60863 ) [5] => Array (
[page_id] => XXXXXX8582 ) )
I need to be able to put this data in a loop and extract the page_id#s out... still getting familiar with json and am having issues figuring this out?
How can I get this in a loop using for each and strip out the page id#s?
Why not just use the array you've got:
<ul>
<?php
foreach($pages as $k=>$v) {
echo "<li>page id#: $v['page_id'] </li>";
}
?>
</ul>