cakephp get all dropdown options from DB - php

I have a drop-down and drop-down options in separate tables. That's good but now I want to be able to retrieve all the options with the drop-down code. On this page I have all of the drop-downs and items. Let's say the drop-down code is dropdown1.
I'd like to be able to do
$this->Form->input('dropdownitem_id', array('options' => $dropdown['dropdown1']));
Is there a way to do this without a helper?

In the controller,
$dropdownitems = $this->OtherModel->find('list');
$this->set(compact('dropdownitems'));
In your view
$this->Form->input('dropdownitem_id');
The options for select will be populated automatically.
But I don't understand, what you meant by helper ?

1) why do you want to do it "without a helper"?
2) Yes, use normal PHP stuff - ie foreach() loop that echos HTML content to the page
Just look at what content the helper generates, and use PHP to mimic it.
<select name="whatever">
<?php
foreach($items as $item) {
echo '<option value=" . $item['id'] . '">' . $item['name'] . '</option>';
}
?>
</select>
(something like that - I wrote that quickly off the top of my head, but you should get the idea)

You should use containable behavior.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
Then you would query the top level element.
After you have done so.
You must run a foreach loop still as Dave said and format the option.
Let me know if you need help with the containable, they are a life saver and your friend !

I created a custom helper to get the exact behavior I wanted.

Related

Wordpress categories list with jquery load()

A "select" element is created by a wordpress plugin (PHP) and I want to add elements in it with .load() function of jQuery.
jQuery:
$("#ugc-input-post_category").load("../wp-content/themes/anim-theme/post-category.php");
post-category.php:
<?php
echo '<h1>TEST</h1>';
$categories = get_categories();
foreach ( $categories as $category ) :
echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';
endforeach;
?>
The echo function that returns "TEST" works and the "h1" element appears in the "select" element, but not the different categories that should be generated by the loop. However, when I add the PHP code in the PHP code of a page, it works and the category list is generated. So I think it's an issue with the load() function.
Thanks a lot !
When the code is included to the normal page, WordPress use data derived from the page that was loaded through WordPress Main Loop, so they can know which data to take, based on the page you are displaying.
But when you took load from the separate file, they didn't have data prepared for them, so it's unable to display the data correctly.
So the next question is you want to use Ajax method to load the page or not?
If not, use PHP 'include' instead so it can generate date since server's load.
If using Ajax, write an Ajax function that return 'json' and use jQuery to display them.
Good luck

Populating a textarea with database records

I populated a textarea with database records using:
<textarea name="textarea" cols="200" rows="20"> <?php
echo "Player Id\t";
while($row = mysql_fetch_array($resourcebuilt)) {
echo stripslashes($row['playerid']);
....;
....;
} ?>
But this isn't exactly 100% what I need. I need to display records in what I believe is a textarea maybe not. But the records need to be clickable so I have functionality to those records (such as edit, delete, or even add a new record to database). Something like what admin panel contains.
I search SO and the web for something similar but with no luck. So does anyone know if this is possible with <textarea> </textarea> or do I need to using something like JavaScript or something related for the interactive functions? If possible provide examples. Thanks you.
I think what you are trying to do is populate a multiline select:
http://www.w3schools.com/tags/att_select_multiple.asp (sorry for w3 schools).
Your code for this would look more like:
<select multiple id="player-id-select">
<?php
while($row = mysql_fetch_array($resourcebuilt)) {
echo '<option>'.stripslashes($row['playerid']).</option>;
....;
....;
}
?>
</select>
Any other interaction (like clicking or whatnot) is done client side via javascript/jQuery:
http://jquery.com/

Passing variable from first page to third page PHP

I have a drop down list on page one with select code:
print "Select week for season 1: <select name='Week_select'> <br>";
On page 2 I have
$varWeek=$_POST['Week_select'];
Then another drop down list:
print "Select a team that played season 1and week $varWeek: <select name='Team_select'><br>";
So far so good and many thanks to all who have gotten me this far.
Now when I go to page 3, I lose $varWeek
I see that I should either use a $_GET or pass it as hidden.
I tried $varWeek=$_GET['Week_select'];
but that didn't work.
I am unsure how to pass it hidden. Please help me understand a little more.
Many thanks in advance
A better approach would be to register those variables as session variables. This way they won't show up in the URL and you will be able to access them across several pages. Have a read here:
http://www.php.net/manual/en/intro.session.php
You can access/store a session variable like this:
$_SESSION['varname'] = 'value';
and on another page
var_dump($_SESSION['varname']);
Add the variable into the form, like you said, as a hidden field, like so:
print '<input type="hidden" name="Week_select" value="'. $_GET['Week_select'] .'" />';
Then on the page which handles the form, the variable will be available in $_POST['Week_select']

Automatically select option from select menu with link

I am trying to use php (and if not php, javascript) to link to a page and include a "?type=foo" or "#foo" at the end of the link so that when the linked page loads it will automatically select a specific option, depending on the end of the link, from a dropdown menu in a form. I have tried to search for this but do not know what this action is actually called, but I've seen it done before. Does anyone know how to achieve this? Thanks very much.
When I have created these in the past, for ease of use, I have simply inserted the selected value in the top of the select object, rather than roll through the whole list and mark the selected one as such when it is encountered.
Assuming the page is called like page.php?theSelectedList=Cougar:
<select name="theSelectList">
<?php
if( isset( $_GET['theSelectList'] ) )
echo '<option selected="selected">'.$_GET['theSelectList'].'</option>';
?>
<option>Aardvark</option>
<option>Baboon</option>
<option>Cougar</option>
<option>Dog</option>
<option>Elephant</option>
</select>
In that case, the option for Cougar would be displayed twice - once at the top of the list, and selected, and again further down the list in the normal location. This may sound confusing, but, in the instances I have used it, it has been quite intuitive.
It's nice and simple, but, I will mention the caveat, that the above would need a bit of re-jigging if the Option Labels and the Option Values are different (like it each option has a numeric value, but a text label.
If the option is in a <select> then the name you are looking for is the selected attribute. It can be applied to each <option> tag, like so: W3Schools.
Using that you can simply use a PHP if-statement, eg:
<?php
$options = array('Norway', 'United States', 'Springfield');
echo '<select>';
foreach($options as $country) {
if(array_key_exists('selected', $_GET) && $_GET['selected'] === $country) {
echo '<option selected="selected">'.$country.'</option>';
}
else {
echo '<option>'.$country.'</option>';
}
}
echo '</select>';
If the query is ?country=Norway then Norway would be selected when the page loads.
It can of course be solved using javascript as well.

Zend Form Radio Elements, using images instead of default radio elements

Ultimately here is my goal. Using Zend_Form I want to turn this idea http://www.sohtanaka.com/web-design/fancy-thumbnail-hover-effect-w-jquery/ into a list of radio buttons.
Kind of using this concept.
http://theodin.co.uk/tools/tutorials/jqueryTutorial/fancyRadio/
I know there has to be a way to do this but I can't seem to figure anything out! Any ideas?
Thanks!
-d
Why do you think there has to be a way?
Did you have a look at the source code? The second one might be done using custom decorators. Have a read about that:
http://framework.zend.com/manual/1.10/en/learning.form.decorators.html
More added after clarifying question:
I had a similar problem where I needed to add a preview button to each radio button. What I did was insert the complete html into the array passed to setMultiOptions. The list is dynamic. Each item on the list is defined elsewhere in the app and stored in the database. When defining these options, the user can specify to up load a file. This file forms part of the url in the a tag. Here is a bit of SQL to help explain what I mean:
function readAllObjlistitemPairpreview($id)
{
// get all objlistitem rows that belong to a certain list
$id = (int)$id;
$select = $this->getAdapter()->select()->from(array('li' => 'objlistitem'), array())->join(array('i' => 'objitem'), 'li.objitem = i.id', array('id' => 'li.objitem', 'name' => "CONCAT(i.name, ' url')"))->where('li.objlist = ?', $id);
//return $this->getAdapter()->fetchAll($select);
return $this->getAdapter()->fetchPairs($select);
}
You could do something similar with an img tag. The array that this function returns can be passed to setMultiOptions: $radio->setMultiOptions($array);
HTH

Categories