I am a bit of a newby to mySQL. I have added a new column that stores image names for images stored in a file (eg.toweltest.jpg). I want to draw these to display images on a page. This is someone else's code I am adapting so its a bit patchy. Basically I want to draw the folder name and then add whatever image name is associated with that product. So far I have this:
<?php
$SNIPPET = true;
require './includes/php/header.php';
$product = Product::Get($_GET['product_id']);
$supplier = Supplier::Get($product->supplier_id);
$url = "/products/";
$image = 'toweltest.jpg';
?>
<h1><?php echo htmlspecialchars($product->code); ?><br><br><?php echo htmlspecialchars($product->name); ?></h1>
<h2><?php echo htmlspecialchars($product->description); ?></h2>
<img src='<?php echo $url.$image; ?>'>
This shows the image but obviously I want the image that is associated with each product. The part that says:
$image = 'toweltest.jpg';
needs to be dynamic but I don't know how to phrase it. Any help appreciated.
Thanks
Rich
Can't tell if we don't know the column name for the image, but it should be something like:
$image = $product->image;
This is actually a guess, since I can't see the rest of the code.
Related
I am trying to run a function which is using a featured image as my header image on my webpage I want to include the options to also allow my end user to select between using the featured image or to select a slider instead if they wish to use that page depending here is the code I have for the featured image development.
add_action('neve_before_primary', 'getPageFeaturedImage', 5);
function getPageFeaturedImage() {
// These two variables will only be used if set
$pageTitle = get_field('page-title');
$pageSecondTitle = get_field('page_second_title');
if (has_post_thumbnail($post -> ID) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ), 'single-post-thumbnail');
<div class="featured-image-container">
<img src="<?php echo $image[0]; ?>" class="featured-image">
</div>
</div>
This code when run is making the feadured image the main image on the page with a title container on top of it with some css which doesnt matter for this question below you will find the code I have for the slider.
function smartsliderheader() {
echo '<div class="smart-slider-header">';
$slider = get_field("smart_slider_header");
echo do_shortcode($slider);
}
This on its own does what I need it to do the featured image code works and so does the slider but getting them both to run toghether and have only one of them run if the other has no options used is where I could used some help.
Any help with this will be much apprechiated. I look forward to your questions if I have missed something out.
I would simply do this using an if statement checking if the $pageTitle and the $pageSecondTitle are empty
if(empty($pageTitle) && empty($pageSecondTitle)) {
// do stuff for when settings aren't filled in
}
else {
// do stuff for when settings are filled in.
}
I don't know if this answers your question. Let me know if this goes into the direction you want to go so I can build on it further.
I was actually able to solve this by myself by doing the following code we are using 2 variables one is for a featured image and the other is go a slider please look below for the code and an explanation.
if(!$checkFeaturedImage and !$slider) {
return null ;
}
This is checking to see if either of these variable's have a value assigned to them e.g. if a featured image is selected a value will be assigned to it and vice versa for a slider what this is doing if neither of these have a value it will stop the rest of the function running and save render time on the site.
if ($slider) {
echo '<div class="header-featured-image">' ;
echo do_shortcode($slider);
echo '</div>' ;
}
What this is doing is it is making a call to an advanced custom field set up to allow a slider to be selected and if a value is returned that value is then being directly echoed out onto the page.
else {
if (has_post_thumbnail($post -> ID) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post -> ID ), 'single-post-thumbnail'); }
?>
<div class="header-featured-image">
<div class="featured-image-container">
<img src="<?php echo $image[0]; ?>" class="featured-image">
</div>
</div>
}
<?php
}
What this is doing is this is displaying our featured image onto our page if a value is returned by our page editor.
The way this code has been set up is if a featured image and a slider have both been selected it will default back to a slider as that is the first call and due to the null statement at the beginning if nothing is selected nothing will run saving some rendering time.
I have an acf repeater containing images. I am only grabbing the first row in the repeater as I only want to display that image. But I also want to set its size to the default large image size that WordPress automatically creates I have tried a couple solutions but I think I'm getting the syntax wrong. Can you show me?
Here's my code:
<?php
$floorplans = get_field('floorplans');
$img = $floorplans[0]['floorplan'];
?>
<img src="<?php echo $img['url']; ?>" alt="">
Use below code to receive large image of acf field
$img=get_sub_field('floorplans');
<img src="<?php echo $img['sizes']['large']; ?>" alt="">
You can use the following code to fetch image with respective sizes via Advanced custom fields.
$floorplans = get_field('floorplans');
$thumbnail_img = $floorplans['sizes']['thumbnail'];
$medium_img = $floorplans['sizes']['medium'];
$large_img = $floorplans['sizes']['large'];
<img src="<?php echo $large_img; ?>" alt="">
Here's what I did to achieve this;
<?php
$floorplans = get_field('floorplans'); //gets the field
$first_row = $floorplans[0]; //first row of field
$img = $first_row['floorplan']; //gets the repeater field
$img_lrg = $img['sizes']['large']; //applies the size I want the image to be
?>
Image is successfully fetching from database but it not be shown by the PHP. In place of image it shows a iamge thumbnail.
If I use header('Content-type:image/jpg'); it will show the only thumbnail all page contents disappear.
include 'functions/connect.php';
$user = $_SESSION['email'];
$sql = "SELECT photo FROM user WHERE email='$user'";
$run_sql = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($run_sql);
$user_photo =$row['photo'];
echo"<p><img src='$user_photo'></p>";`
You can try something like this:
<img alt="<?php echo $user_photo; ?>" src="uploads/<?php echo $user_photo; ?>" /> // Upload is folder where image was uploaded
It depends on what the field "photo" contains. If the photo field contains the location of the picture on your system, then the way you have done it should work. However, if the field contains the actual photo in binary format (having field data type as 'blob') then you'll need to do the following:
file_put_contents("image.jpg", $user_photo);
echo "<p><img src='image.jpg' ></p>";
I'm using the lightbox plugin: Fancybox
I wanted to apply lightbox effect on image gallery php file. The image is stored in longblob format instead of folder. Following is the code I did to read image.
<?php
$img_slct=mysql_query("SELECT * FROM news_gallery WHERE news_id='$page[news_id]' ORDER BY position ASC");
while($img=mysql_fetch_array($img_slct))
{ ?>
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
<?php } ?>
load_image.php
$id=$_GET['id'];
$is_file=false;
if(!empty($id))
{
$file=mysql_query("SELECT id FROM news_files WHERE id='$id'");
if(mysql_num_rows($file)>0)
{
$is_file=true;
$file=mysql_fetch_array($file);
}
}
if($is_file)
display_file_news($id);
Everything is displayed correctly but that is an issue when using lightbox to display the image retrieved from longblob database.
<a rel="example_group" href="load_image.php?id=<?php echo $img['file_id']; ?>">
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
</a>
I have no idea how to read the longblob database using lightbox. So I simply put like this --> href="load_image.php?id=<?php echo $img['file_id']; ?>"
I know that is wrong, but can you help me? Please...Let me know if you need more info, because I can't insert all my code at here. Thank you.
Following are my database tables.
news_files table
news_gallery table
I have a slideshow set up with Magic fields like the code below, but
now I need each image to have a seperate link. How can I set this up?
I just can't think how I can add this to the code below, I appreciate
any help anyone can offer me.
<div id="slider">
<?php
$images = getFieldOrder('slideshow_slide');
if(is_array($images)){
foreach($images as $image){
echo get_image('slideshow_slide',1,$image);
}
}
?>
</div>
Hooray MagicFields! <3
There are two ways to get an image in MagicFields.
Method 1 will return a full image tag:
echo get_image('slideshow_slide');
Method 2 just returns the url of the image:
echo get_image('slideshow_slide',1,1,0);
In order to generate a link to your full-size image, you'll need to construct an anchor tag using the second method. Maybe something like this:
$image_path = get_image('slideshow_slide',1,1,0);
echo 'Insert link text or thumbnail here';
You might need to modify the above to work with your foreach loop, but that's the basic idea.
Update:
Here's what you need to do. Create another duplicateable text field, called image_url. This field will hold the link for your image. Each image will need a corresponding url. This loop should do what you want:
if(is_array($images)){
foreach($images as $image){
$image_url = get('image_url',1,$image);
echo "<a href='" . $image_url ."'>" . get_image('slideshow_slide',1,$image) . "</a>";
}
}