Wordpress Advanced Custom Fields repeater display title outsite of loop - php

So I have a mediaelement.js audio player and I'm using the advanced custom fields plugin and I want to be able to display the title of the current track on the player, here's my code
<audio class="mejs-new" style="width: 100%; height: auto;" controls width="100%" height="auto">
<?php if(get_field('audio_repeater')): ?>
<?php while(has_sub_field('audio_repeater')): ?>
<?php $title = get_sub_field('music_title'); ?>
<source src="<?php the_sub_field('music_url'); ?>" title="<?php echo $title; ?>" type="audio/mp3">
<p><?php echo $title; ?></p>
<?php endwhile; ?>
<?php endif; ?>
</audio>
Edit..
I have tried echoing the title outside the audio tag but it only echoes the 2nd track title and won't change when changing tracks.

If I understand correctly, you're wanting to move the title outside the <audio /> tag, but you can only get the title from within the loop.
You're almost there actually - the easiest way to do this is to set the title to a variable while you're in the loop, which you're already doing ($title = get_sub_field('music_title');). So now, all you need to do is echo the variable when you need it.
Because you've already set the variable while you had access to the functions in the loop, you can now echo that variable afterwards without needing the loop.
For example:
<?php endif; ?>
</audio>
<p><?php echo $title; ?></p>
EDIT:
I assumed you only had one title and I didn't question why you were using a repeater with one title :) To print out a list of the titles afterwards, you can use a similar approach, except this time you need to save the results of the loop and then loop through it again.
The easiest way to do this is to repeat the entire loop code but only print out the title, but that's a little inefficient. Ideally, we should only be getting the field once. Here's another way to do the whole thing:
<?php $audio_repeater = get_field("audio_repeater"); ?>
<audio class="mejs-new" style="width: 100%; height: auto;" controls width="100%" height="auto">
<?php if((array) count($audio_repeater)): ?>
<?php foreach($audio_repeater as $audio_source): ?>
<source src="<?php echo $audio_source["music_url"]; ?>" title="<?php echo $audio_source["music_title"]; ?>" type="audio/mp3">
<?php endforeach; ?>
<?php endif; ?>
</audio>
<?php foreach($audio_repeater as $audio_source): ?>
<p><?php echo $audio_source["music_title"]; ?></p>
<?php endforeach; ?>
In this example, we're grabbing the entire ACF repeater once at the top, and assigning it to a variable. We're then checking it has items in it, and looping through those items.
This is an alternative way to use ACF repeaters, which might not quite be as easy as using the loop functions, but it is more extensible when you're moving things around a bit.

Related

display images with online source in foreach loop in php, repeating loop issue

I have A problem While displaying Images using foreach loop in php.
I am Passing Online http:// Source To The Image Tag
But My loop execute Only For One Time.
here is my code:
$popularmovies contain multiple values But loop execut only once.
<?php foreach ($popularmovies as $popmovies); ?>
<img class="img-responsive" src="<?php echo 'http://'.$popmovies-
>Movie_cover; ?>" alt="" width="210" height="315" >
<? endforeach; ?>
this loop show only one image. but does not repeates.
help me please
Try this:
<?php foreach ($popularmovies as $popmovies): ?>
<img class="img-responsive" src="<?php echo 'http://'.$popmovies-
>Movie_cover; ?>" alt="" width="210" height="315" >
<?php endforeach; ?>
...also check your $popularmovies array for length.

FuelPHP- Using Asset::img() with images fetched from database by for loop

<?php foreach($works as $work) : ?>
<?php echo Asset::img('project-icons/icon/$work->cover_img', array('class'=>'img-responsive', 'alt'=>'...')); ?>
<?php endforeach; ?>
I am using FuelPHP to build a portfolio website. PHP v5.6
This is the "works" section where images and details of a "work" are fetched from database. Using foreach loop. I want to get the "cover_img" (which is the image name), inside the Asset::img(..)
How to do this? what to put in place of $work->cover_img?
Edit: I was able to get the result by using this:
<img src="<?php echo Uri::base(false); ?>/assets/img/project-icons/icon/<?php echo $work->cover_url; ?>" class="img-responsive" alt="..." />
Can this be achieved by using Asset::img() instead of Uri::base()?
Can't you just do:
<?php
echo Asset::img('project-icons/icon/'.$work->cover_img, array('class'=>'img-responsive', 'alt'=>'...'));
?>
...?

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>

Loading picture from database to jquery slider using php

I am trying to load pictures from database to jquery slider
infact what is stored at database is the name of the picture
i implemented this code, it's working without errors but it showing me nothing at slider all is empty
<div id="slider">
<?php
while ($result = mysqli_fetch_object($banner)):
?>
<img src="images/banner/<? $banner['picture'];?>/" width="950" height="400"alt="" title="<strong><? echo $banner['title'];?></strong><span><? echo $banner['description'];?> </span>" />
<?php
endwhile
?>
</div>
name of the table at database is banner in which i have id(INT), picture(varchar 50) title(varchar(100), description(longblob)
query is working and returning number of selected rows
but nothing is shown
You need to echo the result rather than just use the variable...
<div id="slider">
<?php
while ($result = mysqli_fetch_object($banner)):
?>
<img src="images/banner/<?php echo $banner['picture'];?>" width="950" height="400" alt="" title="<strong><? echo $banner['title'];?></strong><span><?php echo $banner['description'];?></span>" />
<?php
endwhile;
?>
</div>

Multiple JQuery hovercards on one page

I am using the jQuery Hovercard plugin (here) and trying to impliment it with user images pulled from Facebook with their php sdk, here is the code I am using to display the Hovercard:
<?php
$friends = $this->facebook->api('/me/friends');
foreach ($friends["data"] as $value) { ?>
<label id="demo-facebook" data-hovercard="<?php echo $value['id'] ?>">
<img src="https://graph.facebook.com/<?php echo $value["id"] ?>/picture" alt="<?php echo $value["name"] ?>" />
</label>
<?php }
?>
The problem I am having is that the Hovercard will only show for the first users image displayed, after that no Hovercard will show when the image is moused over ?
You should change the CSS selector "demo-facebook" from an id to a class. I haven't used HoverCard before, but that seems like it would be the problem.
An ID specifies one specific element of a page, whereas a class can be re-used on multiple elements.
Also, I think it would be easier to just echo the HTML instead of having a <?php echo $value; ?> everytime you have to insert a PHP value. You could just do echo "<img src='$value'>"; instead of <img src="<?php echo $value; ?>" />. Just seems like it would be easier that way.

Categories