I am using a propel form to allow a user to create an account, the form has two parts; the entering and then the preview.
On my preview page I declare the values of the form as usual and these are provided by the previous form
public function configure() {
//Preview page no fields are displayed anyway xD
$this->useFields(array('email', 'user_gender_id', 'search_gender_id', 'content', 'age', 'location'));
But instead of outputting the fields I am trying to render the values on the page:
<?php echo $form->renderHiddenFields(); ?>
<?php foreach($form as $field): ?>
<?php if(!$field->isHidden()): ?>
<tr>
<th><?php echo $field->renderLabel() ?></th>
<td><?php echo $field->getValue(); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo $form; ?>
<?php endif; ?>
Unfortunately for my sfWidgetFormPropelChoice / sfWidgetFormChoice fields it just outputs the chosen ID rather than a string representation of it.
Is there a proper way in Symfony to output the text representation of a widget's value? Or do I have to hack something together? (any ideas?)
Many thanks,
Pez,
Try:
$obj = $form->getObject();
to have the values of your object. Then:
echo $obj[$field->getName()];
this can work in most cases. But, if it won't, you'll have to write all the preview fields by hand and display them using the $obj above.
I got around this problem by doing the following:
<td><?php if(method_exists($field->getWidget(), "getChoices")) {
$choices = $field->getWidget()->getChoices();
echo $choices[$field->getValue()];
} else
echo $field->getValue();
?></td>
I hope this is of use to someone as it has been driving me crazy!
Related
Im using a custom MVC in PHP
I have two tables.
Controles and professors
professors table contains an id of course --> id_professor
controles table contains a field called --> id_professors in which I store a list of profs ids this way ==> 1;2;5;9
In my datatable Im retrieving a list of controles from controles table
for the Model, Im using this class
public function fetchControlesListe(){
stmt = $this->db->prepare("SELECT * FROM controles ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
Controller
liste_controles.php
class Liste_Controles extends Controller {
function __construct(){
parent::__construct();
}
$this->view->fetchCtrlListe= $this->model->fetchControlesListe();
$this->view->render('liste_controles/index');
}
VIEW
<table>
<thead>
<tr>
<th>N°</th>
<th>Module</th>
<th>Salle</th>
<th>Surveillants</th>
</tr>
</thead>
<tbody>
<?php foreach($this->fetchCtrlListe AS $key=>$value): ?>
<tr>
<td><?php echo $controle['id_controle']; ?></td>
<td><?php echo $controle['intitule_module']; ?></td> <
<td><?php echo $controle['intitule_salle'];?></td>
<td>
<?php foreach($this->prof as $profs): ?>
HERE, I would like to retrieve the list of profs from professors
table and the field id_profs in professors table stores an
array, as i said before, containing a list of ids (Ex: 1;2;3;4;5)
I would like to loop through that array and retrieve the name of
professor from professors table if the id_professor Exists in that array
. I know I have to create another METHOD but The PROBLEM is i don't know how to pass parameters in the method and call it from the controller to pass it to the view . and the param would be $controle['id_professor']
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
The result would be like this:
EDIT:
I know that is not a good idea to store lots of ids as an array, but
my question I just want to know how to do a foreach loop inside
another foreach loop using another method with a paramater that will
be passed from the first loop
There isn't one solution but what you can do is, because you know that id_professors is a string composed of ids separated by commas, you can retrieve an array of identifiers this way :
$ids = explode(",", $profs[id_professors']);
and then
<?php foreach($this->prof as $profs): ?>
<?php $ids = explode(",", $profs['id_professors']); ?>
<?php foreach($ids as $id): ?>
//DO STUFF with $id
<?php endforeach; ?>
<?php endforeach; ?>
Notice that the entire thing is not very beautiful because of opening/closing php tags constantly but I hope you'll make a wonderful templates system for your MVC framework :)
From there, it would not be that nice to access the database directly from the view so a better (not perfect) solution would be to provide the view an array made of professors like this one :
$this->view->profs_list = array(
1 => "Prof 1",
2 => "Prof 2",
//etc.
);
with the key of the array being the profs' identifier.
Then you can retrieve their name through the loop :
<?php foreach($this->prof as $profs): ?>
<?php $ids = explode(",", $profs['id_professors']); ?>
<?php foreach($ids as $id): ?>
<span><?php echo $this->profs_list[$id]; ?></span><br />
<?php endforeach; ?>
<?php endforeach; ?>
this is the piece of code I'm not sure how to deal with:
<?php echo get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="InsertHere"]'); ?>
I simply want to insert the first echo instead of InsertHere. The first echo should output the content of a custom field. The second should recall the slider with the specific number inserted in the custom field. When trying different possibilities I only get errors.
Can anybody help?
Thank you :)
Don't echo the first value, use it directly inside your second echo.
<?php
echo do_shortcode('[smartslider2 slider="'.get_post_meta($post->ID, 'smartslider2', true).'"]');
?>
Or you can easily put that first value in a variable and use that variable in the second line
<?php $slider = get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="'.$slider.'"]'); ?>
I have obtained an array output from a curl command. Now, I need to place that output in a table. I have written the entire code in php.
The .phtml part is:
echo "<table border='1'>
<tr><td>NAME</td></tr>
<tr><td>COUNTRY</td></tr>
</table>";
The loop part is:
foreach($arr['fruits'] as $key=>$fruit) {
?>
<tr><td><?php echo $fruit->NAME["Name"];}?></td></tr>
<tr><td><?php echo $fruit->COUNTRY["Country"];}?></td></tr>
I'm very very new to all this. So, I don't really know what I'm exactly doing. Thanks in advance...
Use it like this :
Considering your array structure is like this :
$your_array = array(array("name"=>"prasanth","country"=>"India"),
array("name"=>"bendra","country"=>"India"),
array("name"=>"User","country"=>"US")
);
<?php foreach($your_array as $key=>$fruit) { ?>
<tr><td><?php echo $fruit['name'];?></td></tr>
<tr><td><?php echo $fruit['country'];?></td></tr>
<?php } ?>
Here's all code.
<?php
echo "<table border='1'>
<tr><td>NAME</td></tr>
<tr><td>COUNTRY</td></tr>";
foreach($arr['fruits'] as $key=>$fruit) {
?>
<tr><td><?php echo $fruit->NAME["Name"];?></td></tr>
<tr><td><?php echo $fruit->COUNTRY["Country"];?></td></tr>
<?php ;}
echo "</table>";
?>
Your problem is with this sort of thing:
$fruit->NAME["Name"]
This means in the $fruit object look for a property called NAME that is an array containing the key "Name".
Also, your table isn't very tabular with only one cell per row. I would try something like this (if $fruit is really an array not an object):
<table border="1">
<tr><th>Name</th><th>Country</th></tr>
<?php
foreach( $arr['fruits'] as $fruit ) {
echo "<tr><td>$fruit['NAME']</td><td>$fruit['COUNTRY']</td></tr>";
}
?>
</table>
We can't help you without knowing the actual structure of $arr['fruits']
How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>
I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default.
As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration.
Can anyone help please?
easiest way I can think of off the top of my head...
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<ul id="<?php echo uniqid(); ?>">
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</ul>
<?php print $wrapper_suffix; ?>
that would go in your views-view-list.tpl.php file.
For future reference:
Put a div around everyting in view-views-list.tpl.php. You can (ab-)use the $title to generate unique (but consistent) id's.
Do it like this:
<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>
<div id="<?php print strtolower($id); ?>">
You can use the $view->dom_id variable. It is a unique id for that views instance.
In your .tpl.php file:
<?php print $view->dom_id; ?>
From comments in modules\views\theme\theme.inc:
<?php
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
// each view. This identifier is written to both Drupal.settings and the DIV
// wrapper.
?>