CodeIgniter - multiple results with foreach? - php

I was trying to do simple news system with CodeIgniter, and I've done that. But I've problem with displaying the results. Actually, they're displaying right as I wanted to, but I can not display more than one of them.
I'm using 'foreach' for that, and the thing is, that, when I'm putting my style inside foreach, and displaying it in it, then it works well. But when I'm trying to define these datas first, and then display them (outside the foreach), then only one record is showing up.
What I wanted to do, and doesn't work:
<?php foreach ($posts as $row) {
$title = $row['title'];
$text = $row['text'];
$date = $row['date'];
$autor_id = $row['user_id'];
}
echo $title; ?>
What works, but isn't comfortable to use for me:
<?php foreach ($posts as $p): ?>
<h2><?php echo htmlspecialchars($p['title']); ?></h2>
<p><?php echo nl2br(htmlspecialchars($p['text'])); ?></p>
<?php endforeach; ?>
Is it even possible to do this that way that I'm trying to do?

Just combine the two:
<?php foreach ($posts as $row) {
$title = $row['title'];
$text = $row['text'];
$date = $row['date'];
$autor_id = $row['user_id'];?>
<h2><?php echo htmlspecialchars($title); ?></h2>
<p><?php echo nl2br(htmlspecialchars($text)); ?></p>
<?php } ?>
Or use echo inside the loop and you don't need to break the PHP tags.

Related

Duplicate field within duplicate group magic fields

i have a write panel in magic fields with a group that can be duplicated, within this is an image field that can be duplicated.
So far i have the code for the duplicate group but i'm a little stuck on how to display the duplicate field within this.
I'm using Magic Fields 1.6.2.1
My code for the duplicate group is
<?php
$gallerys = getGroupOrder('gallery_section_title');
foreach($gallerys as $gallery) {
?>
<?php echo get('gallery_section_title',$gallery);?>
<?php echo get('gallery_section_text',$gallery);?>
<?php echo get('gallery_section_image',$gallery);?>
<?php } ?>
This part is the part that needs to be duplicated with in the duplicate group
<?php echo get('gallery_section_image',$gallery);?>
I did a few searches but can't find anything that works.
Does anyone have any ideas how to implement this, i'm stumped.
Thank you
I've worked this out and the code below works for me, there a probably a better more efficient way of doing it, but this works.
<?php $groups = getGroupOrder('gallery_section_title');
foreach($groups as $group) { ?>
<?php echo get('gallery_section_title', $group); ?>
<?php echo get('gallery_section_text', $group); ?>
<?php $fields = get_field_duplicate('gallery_section_image',$group);
foreach($fields as $field)
{ ?>
<?php echo $field[o]; ?>
<?php } ?>
<?php } ?>

How do you stop echo of multiple values with 'foreach' in PHP?

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; ?>

PHP Symfony Convert Propel Choice to String

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!

Print fields in drupal

I'm trying to make Drupal print a list with two different fields in the same array. So first comes field A, then field B and it prints in this way until the whole array is printed.
The result I'm trying to get is something like
<tr>
<td>Field_1[value1]</td>
<td>Field_2[value1]</td>
</tr><tr>
<td>Field_1[**value'n'**]</td>
<td>Field_2[**value'n'**]</td>
</tr>
Until all values are printed.
EDIT.
Figured out one way of achieving this directly in node--testnode.tpl.php.
<table>
<?php if ($content['field_test'][1]): ?>
<tr><td><?php print render($content['field_test'][0])?></td><td><?php print render($content['field_test'][1])?></td></tr>
<?php endif; ?>
<?php if ($content['field_test'][3]): ?>
<tr><td><?php print render($content['field_test'][2])?></td><td><?php print render($content['field_test'][3])?></td></tr>
<?php endif; ?>
<?php if ($content['field_test'][5]): ?>
<tr><td><?php print render($content['field_test'][4])?></td><td><?php print render($content['field_test'][5])?></td></tr>
<?php endif; ?>
</table>
Second fix, only manual work is to say how many repetitions you want.
<dl class="My Class">
<?php
$i = 0;
$counter = 2 * render ($content['field_counter_slides'][0]) -1;
while ($i <= $counter):
if ($content['field_test'][1]){
echo "<dt>";
print render ($content['field_test'][$i]);
$i++;
echo "</dt><dd>";
print render ($content['field_test'][$i]);
echo "</dd>";
$i++;
}
endwhile;
?>
</dl>
It's quite hard to answer without knowing more but something like this would probably do it:
$header = array();
$rows = array();
foreach ($node->field_my_field[$langcode] as $key => $field_entry) {
$rows[] = array(
$field_entry['value'],
$node->field_my_second_field[$langcode][$key]['value']
);
}
$output = theme('table', array('header' => $header, 'rows' => $rows));
You'd have to make sure yourself that there are equal numbers of entries for each field so that there will always be a field_my_second_field entry for each run through the foreach loop.

How do I print a certain wordpress category anywhere I want on any page?

Can someone tell me what the PHP would look like in order to get a category from Wordpress and then print it where ever I want?
I'm guessing I have to build a PHP function. Something like:
function get_a_category() {
$category = get_the_category(); <-----( not sure how to ge ta specific category )
echo $category;
}
I have no idea I know nothing about PHP
can someone help me please ?
You don't have to write your own function; you do have to work with the The Loop (WordPress Codex) and a new query (Function Reference/query posts « WordPress Codex) to keep the original WP loop in place. Run this new query in a page template to get the first post from "mycategory". Change showposts to the number of posts you want.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
Try this
function get_a_category() {
$category = get_the_category();
foreach ($category AS $key => $value) {
$category_name[] = $value->cat_name;
}
$categories = implode(', ',$category_name);
echo $categories;
}

Categories