Im writing a foreach loop for my view using the alternative syntax, to get a piece of html like this
<h3>My Post</h3>
Ive used the line below, but it seems awfully clunky with all the concatenation.
<?php
foreach ($index_posts as $post):
?>
<h3><?= "" . $post['title'] . ""; ?></h3>
<?php
endforeach;
?>
Ive also tried writing it like this :
<?= "<a href='post.php/?id=$post['id']'>$post['title']</a>"; ?>
But it shows errors when written in sublime text 2 around the ['id'] and ['title'] any ideas why this is, as they are single quotes ?
Is there another way i could write this that is cleaner ?
You could just do:
<?php foreach ($index_posts as $post): ?>
<h3>
<a href="post.php?id=<?php echo $post['id']; ?>">
<?php echo $post['title']; ?>
</a>
</h3>
<?php endforeach; ?>
The reason Sublime is showing an error when you do this
<?= "<a href='post.php/?id=$post['id']'>$post['title']</a>"; ?>
is due to the fact that it's a syntax error as you can't have complex variables (like array values with strings for keys) interpolated into a string directly like this. You need to wrap them in {}
<?= "<a href='post.php/?id={$post['id']}'>{$post['title']}</a>"; ?>
Alternatively, you could use:
foreach ($posts as $post) {
printf('%s', $post['id'], $post['title']);
}
<?php foreach ($index_posts as $post): ?>
<h3>
<a href='post.php/?id=<?=$post['id']?>'>
<?=$post['title']?>
</a>
</h3>
<?php endforeach; ?>
Keeping it to HTML whenever you can; I much prefer to not 'echo' out HTML unless needed, although this question really quite about preference. Afterall, PHP is an templating language!
Gotta help a fellow sam...
<?php echo "<h3>{$post['title']}</h3>"; ?>
Related
I have a view that is outputting a list of nodes that contain a link, image and text.
I want to use the link field to wrap the output (rather than link to the node), but I can't figure out how to get the raw url/title etc to create create that link in the template.
The field is configured to output the url as plain text, but is wrapped in div/spans regardless of the style settings for the view field.
views-view-fields--my-view.php:
<a href="<?php echo $fields['field_link']->content ?>">
<?php foreach ($fields as $id => $field):
if ($id == 'field_link') continue;
?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php print $field->content; ?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
</a>
This produces:
<a href="<div class=" data-thmr="thmr_72"><div class="field-items"><div class="field-item even"><span data-thmr='thmr_24' class='devel-themer-wrapper'>/drupal/%237digital-buy</span></div></div></div>">
[...]
</a>
Which is obviously not what I need.
Found the solution. You need to rewrite the output. See below.
I'm new to CakePHP, I was wondering if there is a way to echo information from the database using a foreach loop but only have HTML links on images where the id is 1 & 7. What's the best way of achieving this?
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endforeach; ?>
<?php } ?>
This is how it looks in the veiw, my database in looks image data is stored like this:
suki-burberry-sq_500_500_90_s_c1.jpg
Would it be best to echo all the data individually without the foreach loop or could I write an if statement?
If you want only to get the 1 & 7 id you can do this, assuming that it is a predefined number. If it is dynamic or changeable you can do that in your controller.
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php if ($article['Post']['id']==1 || if ($article['Post']['id']==7) ): ?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php } ?>
Best way is to find from your Model only what you needed.
with the condition $conditions=array('Post.id'=>array(1,7));
I can't get this script to work for nothing. I tried many different ways to make this work but it's not working.
<?php
if (!empty ($image->alttext )) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
Any help would be appreciated!
It always shows true that there is alttext when there isn't sometimes.
Your question is vague but try the following
<?php
if (strlen(trim($image->alttext)) > 0) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
As said your string is probably not empty but with whitespaces.
use var_dump() or strlen() to find out. With trim you remove the whitespaces.
The value in $image->alttext is probably not blank, maybe there are white spaces in it.
You can trim it first before testing.
$imgText = trim($image->alttext);
if (!empty ($imgText)) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
Note: use var_dump($image->alttext) to see that it has the value you are expecting.
I should say first of all I'm not a PHP guy, so if anyone can help with this, I'll do my best to understand any suggestions.
I have the following code that accesses an API and outputs some of the data, via PHP wrapper:
$idMovie=11;
$pelinfo = $tmdb_V3->movieDetail($idMovie);
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
This works fine, it shows the data for one item. What I need to do though is show the data for many more items. So far I've just repeated the block and changed the $idMovie variable - but this is of course, is not the way to do it.
I think I need to do this:
Set up an array to hold each variable, so $idMovie[12,34,56,78]
Create a loop to go through each variable, and output the data using
my code block above.
If anyone can point me in the right right direction, that would be most helpful.
Thanks
Dave
There's one very useful construct in PHP - foreach:
<?php foreach($idMovies as $idMovie):
$pelinfo = $tmdb_V3->movieDetail($idMovie); ?>
<h1><?php echo $pelinfo['original_title']; ?></h1>
<h2><?php echo $pelinfo['release_date']; ?></h2>
<img src="<?php echo $pelinfo['poster_path']; ?>">
<p><?php echo $pelinfo['overview']; ?></p>
<?php endforeach; ?>
Here I've used so-called 'alternative syntax', useful when PHP snippets are included in HTML template.
Yet, there's more than one way to iterate through this array. For example:
<?php
$idMovies = array(11, 22, 33, 42);
$pelHTMLs = array_map(function($id) use ($tmdv_V3) {
$pelInfo = $tmdv_V3->movieDetail($id);
// perhaps you should check the result here, no?
return <<<HTML
<h1>$pelInfo[original_title]</h1>
<h2>$pelInfo[release_date]</h2>
<img src="$pelInfo[poster_path]" />
<p>$pelInfo[overview]</p>
HTML;
}, $idMovies);
echo implode("\n", $pelHTMLs);
?>
Here I used array_map function to create an array $pelHTMLs, each element of which is some HTML representation of a movie data, related to an id taken from $idMovies array. Then all these parts are just 'joined' into a single string with 'implode' function - and echoed out.
This form is quite often used in PHP 5.3+ environments (when you can supply an anonymous function into array_map and similar list comprehension functions). But it actually can be done in PHP 5.2 too - you just need to extract this part into a separate function (or class method), then give its name (or array with two params - class name and method name) as 'callback' argument.
$idMovie = array(12,34,56,78);
foreach($idMovie as $id){
$pelinfo = $tmdb_V3->movieDetail($id);
echo "<h1> $pelinfo[original_title] </h1>
<h2> $pelinfo[release_date] </h2>
<img src='$pelinfo[poster_path]'>
<p>$pelinfo[overview]</p>";
}
Try using
while($data = $pelinfo){<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>}
This should go through all of it.
From here you should be able to work out your girst question.
Its as simple as your pseudo-code. Here as an implementation:
<?php
$ids = array('12','34','56','78') //array of movie ids
$foreach($ids as $id) : //I'm a huge fan of foreach vs for
$pelinfo = $tmdb_V3->movieDetail($id);
?>
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
<?php endforeach; ?>
$idMovieArr=array(11,22,35,...);
foreach ($idMovieArr as $key) {
$idMovie=$idMovieArr[$key];
$pelinfo = $tmdb_V3->movieDetail($idMovie);
<h1><?php echo $pelinfo[original_title]; ?></h1>
<h2><?php echo $pelinfo[release_date]; ?></h2>
<img src="<?php echo $pelinfo[poster_path]; ?>">
<p><?php echo $pelinfo[overview]; ?></p>
<?php } ?>
something like this
I would like to surround this entire block of code in a try/catch since its causing an error when there is nothing in the grid array. Whats the best way to do this?
<?php foreach ($grid->result() as $idx => $row): ?> <?php if ($idx % 3 == 2): ?>
<div class="img_grid_3"><img src="/images/thumb/<?= $row->images_name; ?>" /></div>
<?php else: ?>
<div class="img_grid"><img src="/images/thumb/<?= $row->images_name; ?>" /></div>
<?php endif; ?>
<?php endforeach; ?>
Thanks
Maybe Im missing the point but why dont you test $grid before doing the foreach?
<?php if($grid): ?>
.... foreach ....
<?php endif; ?>
If I got you right, you're looking for the # error suppression operator. If you pass an empty array to foreach, you'll get a PHP warning, you can't catch this with a try/catch block.
<?php #foreach ($grid->result() as $idx => $row): ?>
<?php if ($idx % 3 == 2): ?>
<div class="img_grid_3"><img src="/images/thumb/<?= $row->images_name; ?>" /></div>
<?php else: ?>
<div class="img_grid"><img src="/images/thumb/<?= $row->images_name; ?>" /></div>
<?php endif; ?>
<?php endforeach; ?>
It's possible to use a try catch block with html contents too, but it will obviously only catch Exceptions.
Note that it's cleaner to check wheter the array is empty or not before you use it in a foreach block.
foreach will not produce an error if the array is empty.
So either $grid is not an object, or result() is returning a non-array like false or null. If the former, surround the foreach with if ($grid), of the latter, than use if (!empty($grid->result()))