I have a portal build in PHP + mysql. There's a specific field with the country code value ie "351" in number format.
Can I use the var countrycode.jpg to open the respective file?
something like
$var = "351"
<img src=$var.jpg>
ps: i have a folder with all country codes in (number).jpg format
sorry my dumb question. Thanks in advance for any help.
Cheers
To use php inside html element use echo, something like
<img src="<?php echo $var ?>.jpg">
The question is not very clear, but this is what I assume you want to achieve.
you want to show the image 351.jpg and that actually exists on your server then you can show it like this.
$var = 351;
<img src="<?php echo $var; ?>.jpg ">
Related
I am doing some sort of online storefront and each item has a corresponding image in the database. i need to echo the image of that specific item, how do i do it?
This is what i've done, But it doesn't seem to work:
<?php
$prebuy = "SELECT lot_image FROM lots WHERE lot_id= '$lot_id'";
$prebuyres = mysqli_query($mysqli, $prebuy) or die(mysqli_error($mysqli));
$lot_name = mysqli_fetch_assoc($prebuyres);
?>
<img src="C:\\xampp\htdocs\storefront\img\<?php echo ucwords($lot_name['lot_image']); ?>"
The image does not appear but there is no error either. What am i doing wrong? Please, help if you can.
Thanks in advance!
You missed enclosing img tag. try this way...and what about ucwords, to be sure first check result before using it.
echo ucwords($lot_name['lot_image']);
<img src="C:\\xampp\htdocs\storefront\img\<?=ucwords($lot_name['lot_image'])?>"/>
Edit:
set you image source relative to your php script in server like
<img src="img/<?=ucwords($lot_name['lot_image']);?>"/>
As I am retrieving a column of content in html form, from database as this column contain text and image which is retrieved through base url();
see example:
<p>Although the complexities, individualities and uniqueness of the human mind is beyond
imagination, let alone comprehension.</p>
<img src="<?php echo base_url(); ?>assets/uploads/guide-banner.jpg" alt="guide" />
data mentioned above is stored in database field. I can retrieve html fine but I can't access base_url() from database field, help me what i'm doing wrong calling php while we are already in php ??
You cannot execute PHP code inside PHP code !. I think we can do some trick to make PHP chunks executable.
At this point we need JavaScript capabilities , you have to put that column content into JavaScript variable , then output that variable to your page while loading. You can notice at that moment your PHP code converted to real output !
I hope this helping you !
you can use like this syntax.
<img src="' . base_url() . 'assets/uploads/guide-banner.jpg " alt="guide" />'
you can use like this syntax to retrieve image from database in codeignitor
<?php foreach($results_partners as $part) { ?>
<a href="main/partner" class="roller-item fancybox fancybox.ajax">
<img src="<?php echo base_url() .'cms/uploads/partners/thumb_290/'.$part->partner_image; ?>" style="width:264px; height:263px;">
</a>
<?php } ?>
assign your base_url() to a variable like
$base_url = base_url();
modify your code as shown
<p>Although the complexities, individualities and uniqueness of the human mind is beyond
imagination, let alone comprehension.</p>
<img src="{$base_url}assets/uploads/guide-banner.jpg" alt="guide" />
---^
it will solve this: change your base_url(); to the content of base_url : exp:http://localhost/blablabla;
through var_dump(base_url());
SHORT VERSION OF MY QUESTION: Is there a way using PHP to echo the URL of the directory containing an image?
LONG VERSION: Hello all, I am working on a simple CMS (using Kirby in case it's relevant) and I've got almost everything working except for one thing. Kirby is a file-based CMS and each folder is its own page. I have it set so that all the images in a folder are automatically placed on the page. This is all working fine, but the thing is, I want to have a subfolder inside each project folder containing larger versions of the images. I don't want to use an automatic thumbnail plugin, or lightbox or anything like that. I'd like to manually save both versions of the image. I envisioned something like this: by clicking on the "thumbnail" image
website.com/projects/01-test/cat.jpg
could open a larger image
website.com/projects/01-test/large/cat-large.jpg)
I was hoping that I could somehow use PHP to display the full URL up to the directory of the image, but not the name of the image itself. The closest I have come is using:
<?php echo $image->url(); ?>
but that gives http://website.com/projects/01-test/cat.jpg while I just need http://website.com/projects/01-test.
If I could somehow automate this, then I figured I could do something like:
<a href="<?php echo $image->MYSTERY(); ?>/large/<?php echo $image->name(); ?>-large.jpg">
Okay, I hope this makes sense. I am learning PHP as I go along so I apologize if this has been covered beforeāI have tried searching everywhere but it might just be that I don't quite know what to search for. Any help you can offer would be greatly appreciated!
You're probably looking at dirname():
<?php echo dirname($image->url()); ?>
For example:
echo dirname('http://example.org/path/to/picture.jpg');
// http://example.org/path/to
Explode your URL to be an array and remove last element. using explode() - array_pop() - implode() like this:
$arrayofurl = explode("/", $url);
array_pop($arrayofurl);
$final = implode("/",$arrayofurl);
From your question, I assum that $image->url(); returns a full image URL and $image->name(); returns the name (without ext) of the image, right?
So I think this should do the trick, shouldn't it?
$urlPath = str_replace($image->name() . '.jpg', '', $image->url());
printf('<img src="%s"/>', $urlPath, $image->name(), $image->url());
Update: Forget about this answer, just use dirname().
Just for the fun of it, below a JS alternative that will iterate over all image links and set the XL image path:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<img src="test.jpg"/><hr>
<img src="test2.jpg"/><hr>
<img src="test3.jpg"/>
<script>
for(var a = document.getElementsByTagName("a"), b = 0;b < a.length;b++) {
var c = a[b], d = new String(c.children[0].src), e = d.replace(/\\/g, "/").replace(/\/[^\/]*\/?$/, ""), f = d.replace(e, "");
c.href = e + "/large" + f.replace(".jpg", "-large.jpg")
}
;
</script>
</body>
</html>
I'm trying to make a vote thing for my Minecraft server. I want it to show
on there as in the site. The database is MySQL.
Here is my code. It's just a test; I'm just trying to turn a link into a variable.
<?php
$imageNumber = https://minotar.net/helm/GRANTSWIM4/100.png;
?>
<img src="<?php echo $imageNumber ?>.jpg">
The code you provided will work fine, assuming you fix the syntax issues and remove the double extension (you are appending .jpg after the .png).
<?php $imageNumber = 'https://minotar.net/helm/GRANTSWIM4/100.png'; ?>
<img src="<?php echo $imageNumber ?>">
If you are asking how to get a MySQL table name and reference the value in a variable, then yes. You can retrieve the table name from the result with mysql_tablename or get the full list of tables with mysql_list_tables methods.
But I wouldn't suggest you to include your table names on the client side.
I use code:
<img src="images/english.png"/>
My site use 2 language English and Vietnamese, I want when change language to Vietnamese, the image filename will be change to vietnamese.png example:
<img src="images/vietnamese.png"/>
have already 2 image file into images folder, i need PHP script for change it when change language
You may need to provide some more information than that- for example, how does your site determine which language to use?
Assuming that you can create a boolean value in PHP (let's call it langV) that is true if the language is Vietnamese, and false if it's English, then here's all you have to do:
Replace your img tag:
<img src="e_image" alt=""/>
With something like this:
<img src="<?php echo langV?:'v_image''e_image' ?>" alt=""/>
Replacing v_image and e_image with the paths to your separate image files, of course.
create an if else statement. When you select for example English, show image A, else show image B. What happens is when you select a language, you most likely change a value. So use that value to create an if else statement, that has to correspondent with you image.
Its really easy if you ask me. I put an example here below
if($LanguageVar == 1)
{
Echo "<img src='blablaba1.png'></img>";
}
else
{
Echo "<img src='blablaba2.png'></img>";
}
or something in that general direction
How does your website know which language is being used?
You can use parts of the URL. For example, if you have
http://www.mysite.com/viet/
http://www.mysite.com/en/
name your images "viet.png" and "en.png"
$lang = end(explode('/',$_SERVER['REQUEST_URI'))
<img src="$lang.png" />
It really depends on how the site determines which language to use
Set Cookie value For language
When User Select Language
<?php
$value = 'en';
setcookie("lan", $value);
setcookie("lan", $value, time()+3600);
?>
For Generating Image
<?php
if($_COOKIE['lan']=='en'){
echo "<img src='blablaba1.png'></img>";
}
else{
echo "<img src='blablaba2.png'></img>";
}