I have a basic table in a wordpress site that looks like this: https://jsfiddle.net/4fcpdgs8/
Right now the table is hard coded with values found in the html but ideally I would like it to be dynamic based on the values in the MYSQL database. I have seen some solutions that involve php such as:
global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "your_table_name";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<ul>
foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->column_name;?></li>
<li><?php echo $retrieved_data->another_column_name;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
<?php
}
?>
</ul>
<?php
But one wrinkle is that one column is a url for an html image tag so doing the above just displays the url.
So, I would rather do something that would enable me to just reference the sql results like:
<img src=[mysql_image_column_row_one]>
<img src=[mysql_image_column_row_two]>
Rather than just displaying the sql results in a table. Sidenote: I know very little php and it is quite mysterious to me!
you can use this for images
<?php echo '<img name="myimage" src=' . $retrieved_data->column_name
. ' width="60" height="60" alt="imagedesciription" />'?>;
You must adept the image size or even resize the image.
You can use img tag with an image path inside li tag in foreach loop.
<li><img src='path_to_image_folder/<?php echo $retrieved_data->image_name?>'></li>
Related
I made the ACF plugin group with files to download. In group I have fields "File 1", "File 2"...etc.
I would like to display all attached files to page. It is possible to display all fields belong to group? I try with basic code, but in this case I have only 1 file.
How can I add iteration to this or display all fields?
<?php
$file = get_field('attachment_1');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<ul>
<li><a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<ul>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
As all your fields are set up individually, it isn't just a matter of looping through an array of all your fields of the same type (i.e. just your file fields).
There are a few ways that might work for you:
Option 1.
If all the field names for your files follow the same naming pattern and are named sequentially, you could loop using the name.
Example, assuming your fields are named attachment_1 up to attachment_5:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
// display file details as appropriate
}
}
Option 2.
If the file field names do not follow the same pattern, you could loop through an array of the field names.
Example:
$statement = get_field('name_of_your_statement_field');
//do whatever you need to with $statement
// Create an array with the field names of all your files
// N.B. This also lets you specify the order to process them
$file_fieldnames = array('file_1', 'file2', 'another_file');
foreach ($file_fieldnames as $fieldname) {
$file = get_field($fieldname);
if( $file ){
// display file details as appropriate
}
}
Option 3. If you want to loop through ALL fields on the post/page, you can save the fields into an array.
This might seem like the most generic approach at first, but it is complicated by the fact that you don't know what type each field is in order to know how to process and display them... you first have to work out what field type it is. You could do this by name (similar to above) or you could try to identify what each field by checking the field content.
Note, checking the field content is very risky, as there are other field types that can have similar featured (e.g. a file is not the only type that can have a url) so I wouldn't advise that strategy unless you are 100% sure you'll never change the field group or add another field group to the post/page.
Example:
$fields = get_fields();
foreach ($fields as $fieldname => $content) {
if (is_string ($content)){
// display the string
}
else if (is_array($content) && $content['url']) {
// then you could assume its a file and display as appropriate
}
}
Note that none of this code is tested. However it should give you an idea of the logic behind each option so you can decide what works for you.
UPDATE based on new code provided:
See below based on the code in your JSFiddle. I've ignored the caption outside the file list because it makes no sense - every file can have its own caption.
<?php
for ($i=1; $i<=5; $i++){
//use the number from the loop to find the file by name
$file = get_field('attachment_'.$i);
if( $file ){
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// now display this file INSIDE the loop so that each one gets displayed:
?>
<li>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
<?php endif; ?>
</li>
<?php
} // end if
} // end for loop
?>
<ul>
If you understand arrays, I'd suggest you add the file details into an array and then do a second loop to display the files... however I'm guessing you're not that proficient with basic coding constructs as you don't understand loops, so I've tried to keep it simple. I strongly recommend that you do some tutorials on programming basics if you are attempting to write code.
How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)
I have a table "videos" with id_video, client, code. And i found this:
http://demo.juanfra.me/multiple-videos-flexslider-v2/
First i tried put the -li- into a while and show all results with mysqli_fetch_array.
But i cant show anything. I think the id of each iframe "player_number" must be unique, so do i have to use a for()?
<?php while($rowmulti = mysqli_fetch_array($resultmultimedia)){
echo'
<li>
<iframe id="player_1" src="http://player.vimeo.com/video/'.$rowmulti["code"].'?api=1&player_id=player_1"></iframe>
</li>
';}?>
Maybe the error is in the jquery code but i dont good js programmer. thank you!
You don't necessarily need a for loop. You can just keep track of an $id variable within the loop that increments each iteration.
Try this:
<?php
$id = 1;
while ($rowmulti = mysqli_fetch_array($resultmultimedia)) {
echo'
<li>
<iframe id="player_' . $id . '" src="http://player.vimeo.com/video/' . $rowmulti["code"] . '?api=1&player_id=player_' . $id . '"></iframe>
</li>';
$id++;
}
?>
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.
?>
I'm using this piece of code I found (http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage) to display the first image uploaded to a post on the homepage next to the excerpts of posts. I'm doing this outside the main loop on the homepage, and have been having problems. When I do rewind_posts() to get the same loop results, it works fine, but when I try to create a different loop, this code snippet breaks down:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images)
{
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";
}
I have tried the methods in The_Loop#Multiple_Loops_in_Action in the docs, and they work, meaning I can get normal output after the loop, but my snippet above doesn't work. Any idea if there is a conflicting method call or something that's going on that's stopping it from working? Would appreciate some help, thanks!
To be more specific:
<?php $my_query = new WP_Query('category_name=Daily Photo&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<p>a</p>
<?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images) {
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";} ?>
<?php endwhile; ?>
will output <p>a</p>, but not the <li><img /></li> code I need in the snippet. Whereas if I use rewind_posts();, everything works, and I get the <li><img /></li> code, but I don't want to use the same loop that I had been using previously. I'm using this to display a daily photo in the sidebar, that pulls from the "Daily Photo" category. I will exclude Daily Photos from the main loop, and only want to use them to draw images from in that snippet.
Try removing the if statement for starters. Remove any references to images and see if it outputs your html without the image source. If that's the case then images isn't getting properly assigned and it's never executing the code.
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
This code checks to see if there is an
image uploaded to the gallery.
Now you need to troubleshoot the above statement and see why you're not getting any images.
Also, it looks like there is a plugin to do just this if you don't want to continue messing with the code.
Another thing to keep in mind is that this code is pretty old and may not be compatible with your version of wordpress.