PHP code:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
foreach($images as $curimg) {
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
if($counter==1){$ImageView_1 = $AutomaticallyOpenShow.$curimg; }
elseif($counter==2){$ImageView_2 = $AutomaticallyOpenShow.$curimg; }
elseif($counter==3){$ImageView_3 = $AutomaticallyOpenShow.$curimg; }
elseif($counter==4){$ImageView_4 = $AutomaticallyOpenShow.$curimg; }
$counter++;
}
}
HTML code:
<img src="<?php echo ImageView_1 ; ?>" width="500" height="500" />
Thanks for Kurro1 and RaggaMuffin-420 answer.
I finally made the integration
PHP code:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 1;
foreach($images as $curimg) {
if (preg_match('/^.*\.[jpeg]{3,4}$/i', $curimg)) {
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
$counter++;
}
}
HTML code:
<img src="<?php echo $ImageView[????] ; ?>" width="500" height="500" />
How about using an array for storage instead of 4 variables?:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
$ImageView = array();
foreach($images as $curimg) {
// the condition could also be optimized
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
// write result in array at appropriate position
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
}
}
The html code would be like the following:
<img src="<?php echo $ImageView[0]; ?>" width="500" height="500" />
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 1;
foreach($images as $curimg) {
if (preg_match('/^.*\.[jpeg]{3,4}$/i', $curimg)) {
if($counter >= 1 && $counter <= 4){
$varName = 'ImageView_'.$counter;
$$varName = $AutomaticallyOpenShow.$curimg;
}
$counter++;
}
}
Based on the first code you posted.
PHP code:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$ImageView = Array();
$counter = 0;
foreach($images as $curimg) {
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
}
}
HTML code:
<img src="<?php echo ImageView[0] ; ?>" width="500" height="500" />
Related
I have been trying to insert multiple images in mysql database. After using below code, it was inserted in the database along with other fields as array. But now I am unable to fetch array to display images.
if(isset($_POST['submit'])){
$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$days = $_POST['days'];
$nights = $_POST['nights'];
$people = $_POST['people'];
$hotel_rating = $_POST['hotel_rating'];
$images_name = $_FILES['images']['name'];
$images_type = $_FILES['images']['type'];
$images_size = $_FILES['images']['size'];
$images_tmp = $_FILES['images']['tmp_name'];
$inclusion = $_POST['inclusion'];
$exclusion = $_POST['exclusion'];
$date = date('d/m/y');
$category = $_POST['category'];
if($title == '' or $category == '' or $description == '' or $price == '' or $days == '' or $nights =='' or $people =='' or $hotel_rating==''
or $images_name == '' or $inclusion =='' or $exclusion== ''){
echo "<script>alert('Please fill all fields!')</script>";
exit();
}
for($i = 0; $i< count($images_tmp)-1; $i++){
if($images_type[$i] == "image/jpeg" or $images_type[$i] == "image/png" or $images_type[$i] == "image/gif"){
if($images_size[$i] <= 150000 && $images_size[$i] >= 100000){
move_uploaded_file($images_tmp[$i], "images1/".$images_name[$i]);
}
else {
echo "<script>alert('Please upload image between 100kb and 150kb only.')</script>";
exit();
}
}
else{
echo "<script>alert('Image type is invalid!')</script>";
exit();
}`
I am using the following code to fetch images.
<img src = "<?php echo $images; ?>>
Can anyone help me that what am I doing wrong? Thanks in advance. Following is the code missing for variable initialization:
if(!isset($_GET['cat'])){
$query = "select * from post order by 1 DESC LIMIT 0,4";
$run = mysqli_query($db, $query);
while($row = mysqli_fetch_array($run)){
$post_id = $row['post_id'];
$title = $row['post_title'];
$description = substr($row['post_desc'], 0, 300);
$price = $row['post_price'];
$days = $row['post_days'];
$nights = $row['post_nights'];
$people = $row['post_people'];
$hotel_rating = $row['post_hotel'];
$images = array('images1/'.$row['post_images']);
$inclusion = $row['post_include'];
$exclusion = $row['post_ninclude'];
$date = $row['post_date']; ?>
<!-- Item -->
<div class="item clearfix rating_5">
<div class="item_image"><img src = "<?php echo $images; ?>" width="500" height="400" style="width:100%; height:400px; object-fit: cover;" alt="Image cannot be displayed"></div>
If the $images variable is array of images, you have to do at least
<?php foreach ($images as $image): ?>
<img src="<?= $image ?>">
<?php endforeach ?>
But in your code is no communication with database, no $images variable initialization, nothing. So only we can do is guess.
regardless for all of the mentioned code but in your img tag you have to close the src attribute.
I created an application to get all the photos which are related to a specific tag. But it limit to 15 photos. How can I get the all the photos with username.
Here is the code...
<?php
function scrape_insta_hash($tag) {
$insta_source =
file_get_contents('https://www.instagram.com/explore/tags/'.$tag.'/');
$shards = explode('window._sharedData = ', $insta_source);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
return $insta_array;
}
$tag = 'paris';
$results_array = scrape_insta_hash($tag);
//$limit = 7;
$limit = 15;
$image_array= array();
for ($i=0; $i < $limit; $i++) {
$latest_array = $results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'][$i]['node'];
$image_data = '<img height="150" width="150" src="'.$latest_array['thumbnail_src'].'">'; // thumbnail and same sizes
//$image_data = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes
array_push($image_array, $image_data);
}
foreach ($image_array as $image) {
echo $image;
}
?>
To process all the returned data, change your for loop into a foreach loop:
foreach ($results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'] as $latest_array) {
$image_data = '<img height="150" width="150" src="'.$latest_array['node']['thumbnail_src'].'">'; // thumbnail and same sizes
//$image_data = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes
array_push($image_array, $image_data);
}
(notice the change to the assignment of $image_data as well)
You can still limit the number of results from this loop like this (just set $limit = 0; to not limit the output):
$limit = 15;
$count = 0;
foreach ($results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'] as $latest_array) {
$image_data = '<img height="150" width="150" src="'.$latest_array['node']['thumbnail_src'].'">'; // thumbnail and same sizes
//$image_data = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes
array_push($image_array, $image_data);
$count++;
if ($count == $limit) break;
}
you can try to get count of array and assign it to limit
it will be something like this
$limit = count($results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges']);
it will give you count of all images in that tag
SO here is my code:
<?php
$image = '';
if (getUserChar($_SESSION['user_login']) == 1) {
$image = 'warrior';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 2) {
$image = 'mage';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 3) {
$image = 'archer';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
?>
With that, the class seems to work fine. but with regard to the variables top_pos -- it doesnt.
I suspect your $top_pos and $left_pos variables are not being set or being overwritten somewhere.
<?php
function getUserChar($input) {
return $input;
}
// I suspect you have not set these anywhere or are being over written somewhere
$top_pos = 123;
$left_pos = 321;
$image = '';
$_SESSION['user_login'] = 1;
$input = getUserChar($_SESSION['user_login']);
if ($input == 1) {
$image = 'warrior';
} elseif ($input == 2) {
$image = 'mage';
} elseif ($input == 3) {
$image = 'archer';
}
echo '<div class="'.$image.'" style="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
http://sandbox.onlinephpfunctions.com/code/184e6935fa39e733c145441c6a24be6651f41c25
I have the following php script display all images in a directory
<?php
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" height ="400"/><br />';
}
?>
I want to modify this so that when you visit the page it shows the last modified image on top. Could someone help me how I would go about doing that?
You can use the filemtime() function to find the modification date of each file. This can be used as a key to sort the array using uksort() before it is processed in the loop.
This will put the array in ascending order of file modification time, i.e. those with the earliest mtime first. You can then either reverse the array, or iterate through it backwards.
<?php
function mtimecmp($a, $b) {
$mt_a = filemtime($a);
$mt_b = filemtime($b);
if ($mt_a == $mt_b)
return 0;
else if ($mt_a < $mt_b)
return -1;
else
return 1;
}
$images = glob($dirname."*.jpg");
usort($images, "mtimecmp");
$images=array_reverse($images);
foreach ($images as $image) {
echo '<img src="'.$image.'" height ="400"/><br />';
}
?>
(Iterating backwards is more efficient...)
// ...
usort($images, "mtimecmp");
for ($i = count($images) - 1; $i >= 0; $i--) {
$image = $images[$i];
echo '<img src="'.$image.'" height ="400"/><br />';
}
You'll need to perform in two steps:
(a) read the directory contents and note last modified info
(b) render the result
$images = glob($dirname . '*.jpg');
$mostrecent = 0;
$mostrecentimg = null;
// scan
foreach ($images as $image) {
$imagemod = filemtime($image);
if ($mostrecent < $imagemod) {
$mostrecentimg = $image;
$mostrecent = $imagemod;
}
}
// display
echo '<img src="' . $mostrecentimg . '" height="400"/><br />';
foreach($images as $image) {
// the most recent was already output above so skip remainder this iteration
if ($image == $mostrecentimg) continue;
echo '<img src="' . $image . '" height="400"/><br />';
}
I have created a simple html table within PHP. Here is my code:
<div id="wrapper">
<div class="chart">
<h2>Files Uploaded to Knowledge Base</h2>
<table id="data-table" border="1" cellpadding="10" cellspacing="0">
<tr id=header>
<td>Users</td>
<td id=center>Project Files</td>
<td id=center>Process Files</td>
<td id=center>System Files</td>
<td id=center>Total Files</td>
</tr>
<?php
$di = new RecursiveDirectoryIterator('upload/project/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 15;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file1 != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM Project WHERE location = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance1 = $row['uploader'];
$array1[] = $occurance1;
}
}
}
$di = new RecursiveDirectoryIterator('upload/process/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 15;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM Process WHERE processlocation = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance2 = $row['uploader'];
$array2[] = $occurance2;
}
}
}
$di = new RecursiveDirectoryIterator('upload/system/');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
$pos = 14;
$file = substr("$filename", +$pos);
$lenght = strlen($file);
$pos = strpos($file, "/");
$file = substr("$file",0,$pos);
if($file != '.DS_Store'){
$serverfiles = mysql_query("SELECT uploader FROM System WHERE location = '$file'");
while($row = mysql_fetch_array($serverfiles)) {
$occurance3 = $row['uploader'];
$array3[] = $occurance3;
}
}
}
$table_rows = array();
$counter = 0;
$uploader = mysql_query("Select username from members");
while($Load = mysql_fetch_array($uploader)){
$value = $Load['username'];
$tmp = array_count_values($array1);
$cnt = $tmp[$value];
$tmp2 = array_count_values($array2);
$cnt2 = $tmp2[$value];
$tmp3 = array_count_values($array3);
$cnt3 = $tmp3[$value];
$total = $cnt + $cnt2 + $cnt3;
//putting the values into array
$counter++;
$table_rows[$counter] = array();
$table_rows[$counter]['username'] = $value;
$table_rows[$counter]['project'] = $cnt;
$table_rows[$counter]['process'] = $cnt2;
$table_rows[$counter]['system'] = $cnt3;
$table_rows[$counter]['total'] = $total;
}
//function to sort the highest total value
function cmp_rows($a,$b) {
if ($a['total'] == $b['total']) {
return 0;
}
return ($a['total'] > $b['total']) ? -1 : 1;
}
usort($table_rows, 'cmp_rows');
//loop that prints values
foreach($table_rows as $row) {
echo "<tr>";
echo "<td>{$row['username']}</td>";
echo"<td id=center>{$row['project']}</td>";
echo"<td id=center>{$row['process']}</td>";
echo "<td id=center>{$row['system']}</td>";
echo "<td id=center>{$row['total']}</td>";
}
?>
</table>
</div>
</body></html>
The users are populated from a database table. The file figures are populated by reading and counting the amount of files in the directory. The table is sorted by highest first. I would like the first, second and third rows to be a different colour than the rest.
I do not know how to do this. Can someone please guide me in the right direction?
Thanks!
Add a key to the foreach then create a simple if/else statement when you output the table row that says:
> foreach( $table_rows as $key => $row ) {
> if ( $key < 3 ) {
> echo "<tr style="background color here">;
> }
> else {
> echo "<tr>";
> }
> //Rest of Code Here }