Assign image to PHP variable - php

I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}

Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred

$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}

$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}

Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.

<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>

please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>

Related

php glob() retrieve all files except one

I have a folder named 'Folder'. There are several photos inside it.
One of them is "1.jpg"..
I need to retrieve all the photos from this folder, except "1.jpg" ($first)..
As I understand, I need something like if ($image=$first) { . . . } inside of foreach.
$first="1.jpg";
$dirname="folder";
$images = glob($dirname.'*');
foreach($images as $image) {
$html="<img src='".$image."'><br />";
echo $html;
}
Thanks for attention
You can skip the echo when $image is not equal (!=) to $first:
foreach($images as $image) {
if ($image != $first) {
$html="<img src='".$image."'><br />";
echo $html;
}
}
Or you can use continue to skip to the next image, when $image is equal to $first, if you have more complex code in the foreach:
foreach($images as $image) {
if ($image == $first) {
continue;
}
$html="<img src='".$image."'><br />";
echo $html;
}
$first="1.jpg";
$dirname="folder";
$images = glob($dirname.'*');
unset($images[$first]);
foreach($images as $image) {
echo "<img src='".$image."'><br />";
}

Generate link with array

I want generate a link using two array: the first one contains addresses; the second one contains text.
I want have:
- text3
- text3
- text3
to do so I tried like this but I can't generate texts.
<ul>
<?php
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
$nome=array_values($nomimenu);
foreach($vocimenu as $voce) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$voce.'">';
echo $nome;
if($page!=$voce) echo "</a>";
echo "</li>";
}
?>
</ul>
You can use one array
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$links=array("address1"=>"text1","address2"=>"text2","address3"=>"text3");
foreach($links as $href=>$text){
if($page!=$voce){
echo ''.$text.'';
}else{
echo $text;
}
}
This should work:
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
//since you're using two arrays, foreach is not the way to go
//you need a counter so you can get elements from each array
for ($i=0;$i<count($vocimenu);$i++) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$vocimenu[$i].'">';
echo $nomimenu[$i];
if($page!=$voce) echo "</a>";
echo "</li>";
}
An alternate option is to do it like this, but that could make some of your other code less flexible:
$array = array("address1"=>"value1","address2"=>"value2",...);
foreach($array as $address=>$value){
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$address.'">';
echo $value;
if($page!=$voce) echo "</a>";
echo "</li>";
}
It would be much easier if you create an associative array:
$menu = array(
"fmp_trama" => "Full Metal Panic!",
"fumoffu_trama" => "Full Metal Panic? Fumoffu",
"fmp_tsr" => "Full Metal Panic! TSR"
);
echo '<ul>';
foreach ($menu as $key => $value) {
echo "<li>";
if($page != $key) {
echo sprintf('%s', $key, $value);
}
else {
echo sprintf('<span>%s</span>', $value);
}
echo "</li>";
}
echo '</ul>';
You can build the array like this: (if you are bound to the 2 array structure)
$menu = array_combine($vocimenu, $nomimenu);

How to use nested foreach

I've successfully retrieved car make and car name accordingly but I've no idea on how to retrieve image for that particular car.I tried using nested foreach but didn't work as I expected.Instead of displaying particular image for a car id ,it shows the same image from last folder for all ids.
here's my code :
while($row = mysql_fetch_array($result_name,MYSQL_ASSOC)) {
$car[$row['carMake_id']][] = $row['carName'];
//$car_name_id=$row['carName_id'];
$gallery=$row['gallery'];
//$car_make_id=$row['carMake_id'];
//$car_gallery[$row['carName_id']][]=$row['gallery'];
}
foreach ($car as $carmake => $carname) {
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
?>
//this part displays image.. I want it to display according to the car name ($title)
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $gallery;?>" width="100" height="100"></td>
<?php
}
echo'</tr>';
}
Also how do I retrieve all other information pertaining to a particular car inside the foreach?
Thanks.
EDITED PART:
$gallery[]=$row['gallery'];//inside while loop
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
foreach($gallery as $g)//new foreach to retreive images
{
echo $g;
}
?>
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $g;?>" width="100" height="100"></td>
You need to put gallery into the array.
<?php
while($row=mysql_fetch_array($result_name,MYSQL_ASSOC))
{
$car[$row['carMake_id']][] = $row;
}
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title)
{
echo "<td>{$title['carName']}<br/> ";
//this part displays image.. I want it to display according to the car name ($title)
echo "<img src='management/uploads/$carmake/{$title['gallery']}' width='100' height='100'></td>";
}
echo'</tr>';
}
?>

php foreach how to filter image names to display certain images you want

Files are
AAA_1.jpg AAA_2.jpg AAA_3.jpg BBB_1.jpg BBB_2.jpg CCC_1.jpg
foreach ($carousel as $image) {
echo "<img src='images/$image'>";
}
How to filter image names, just to display AAA_ images.
result to see
<img src='images/AAA_1.jpg'>
<img src='images/AAA_2.jpg'>
<img src='images/AAA_3.jpg'>
There are a probalby a million ways to do this, but I would just check the name of the image to see it begins with 'AAA_'
foreach ($carousel as $image) {
if(strpos($image,'AAA_') === 0){
echo "<img src='images/carousel/$image'>";
}
}
Or more complex, but still fun, entertainingly worth the extra performance hit of explode:
foreach ($carousel as $image) {
$image_parts = explode('_', $image);
if($image_parts[0] == 'AAA'){
echo "<img src='images/carousel/$image'>";
}
}
foreach ($carousel as $image) {
if(substr($image, 0, 3) == 'AAA'( {
echo "<img src='images/carousel/$image'>";
}
}
If you know all your file, you can use that code, also use in_array ;-)
$carousel = array('AAA_1.jpg', 'AAA_2.jpg', 'AAA_3.jpg','BBB_1.jpg','BBB_2.jpg','CCC_1.jpg');
$entry = array('AAA_1.jpg', 'AAA_2.jpg', 'AAA_3.jpg');
foreach ($carousel as $image) {
if(in_array($image, $entry)) {
echo "<img src='images/carousel/$image'>";
}
}
By using substr and strlen you will be able to get the images you want starting with the prefix variables value.
$prefix = 'AAA_';
$carousel = array(
"AAA_1.jpg",
"AAA_2.jpg",
"AAA_3.jpg",
"BBB_1.jpg",
"BBB_2.jpg",
"CCC_1.jpg"
);
foreach ($carousel as $k => $v) {
if (substr($v, 0, strlen($prefix)) == $prefix) {
echo "<img src=\"images/{$v}\" alt=\"image {$k}\" />";
}
}

Getting XML element based on attribute value

Hey.
I have an XML file and would like to use PHP to display the medium size only. The part of the XML looks like this:
<image size="small">/small23.png</image>
<image size="medium">/medium23.png</image>
<image size="large">/large23.png</image>
I'd like to display the image on the page using <img src="" />, but I'm not sure how to put only the medium image. Within a forloop going through all the other elements, I tried this:
if($file->image->attributes()->size == "medium")
echo "<img src=$file->image />";
but nothing is drawn.
thanks
Based on your example I assume you're using SimpleXML, so try this:
$mediums = $file->xpath("image[#size='medium']");
if (count($mediums)) {
echo '<img src="' . (string) $mediums[0] . '" />';
}
$done = false;
foreach($file->image as $img) {
foreach($img->attributes() as $key => $value) {
if($key == "size" && $value == "medium") {
echo "<img src = {$file->image} />";
$done = true;
break;
}
}
if($done) { break; }
}
This might work, try it out and let me know.

Categories