Javascript resize on every image load - php

I've got this code:
while ($row = mysql_fetch_assoc($result1)) {
echo "
<a href='#'id='VV".$row['id']."'>
<p>".$row['title']."</p>
<p>".$row['date']."</p>
<img onload='scale()' id='II".$row['id']."' src='../res/videos/img/".$row['img']."'/>
</a>
<script type='text/javascript'>
function scale(){
var image = document.getElementById('II".$row['id']."');
var width = image.width;
var height = image.height;
if (width > 150) {
image.style.width = '150px';
image.style.height = 'auto';
}
width = image.width;
height = image.height;
if (height > 80) {
image.style.height = '80px';
image.style.width = 'auto';
}
width = image.width;
height = image.height;
}
VV".$row['id'].".onclick = function() {
var Result = '".$row['id']."';
location.href='loged.php?Result=' + Result;
}
</script>
";
}?>
I want the resize function be called on every image loaded, but it is called when the last image is loaded, and only the last image takes effect. What should i change?

You are placing the script for each $row. I would have 1 copy of the javascript function and then you can call that single function N times passing the id to the function. This way it will work for all rows, not just the last (since the last copy of the JS function will be the only one called as you've found out).
Short answer: make your function better by taking a id parameter and only put that function on the page once. allow you foreach to call said function with each id.

I think you are trying to fit an image in a 150x80 area. If that's the case, I suggest you use CSS background-size: contain to do the scaling for you.
while ($row = mysql_fetch_assoc($result1))
echo "<a href='#' id='VV" . $row['id'] . "'>" .
"<p>" . $row['title'] . "</p>" .
"<p>" . $row['date'] . "</p>" .
"<div id='II" . $row[id] . "' " .
"style='width: 150px; " .
"height: 80px; " .
"background: url(../res/videos/img/" . $row['img'] . "); " .
"background-size: contain; " .
"background-repeat: no-repeat;'>" .
"</div>" .
"</a>";
It'd be a good idea to abstract out the styling into a CSS file.
while ($row = mysql_fetch_assoc($result1))
echo "<a href='#' id='VV" . $row['id'] . "'>" .
"<p>" . $row['title'] . "</p>" .
"<p>" . $row['date'] . "</p>" .
"<div id='II" . $row[id] . "' " .
"class='my_img_box' " .
"style='background: url(../res/videos/img/" . $row['img'] . ");'>" .
"</div>" .
"</a>";
The CSS
div.my_img_box {
width: 150px;
height: 80px;
background-size: contain;
background-repeat: no-repeat;
}

Related

I need to refactor this php function

I need to refactor this code, Im not a php developer so i dont understand the syntax thats apparent here, what i want is this button to activate the moment the page is loaded.
<?php
$h = '';
if ($newsermonstomove) {
foreach ($newsermonstomove as $vettel) {
$h .= "<div style=\"padding: 5px;\">";
$h .= "<button id=\"btn_" . $vettel->ID . "\" class=\"btn btn-primary btn-xs\" onclick=\"doMe(" . $vettel->ID . ");\" >s3</button><span style=\"padding-left:5px;\">Channel: " . $vettel->ChannelID . ", Sermon: " . $vettel->ID . " " . $vettel->SermonTitle . "</span> <div style=\"display:none;color:blue;\" id=\"msg_" . $vettel->ID . "\"></div><br/>";
$h .= "</div>";
}
} else {
$h = "<h3>No new sermons.</h3>";
}
echo $h;
?>
From what i understand, the onclick has an escape in it onclick=\"doMe(" . $vettel->ID . ");\" In HTML i know that with a button if you do onclick=doMe its a reference to a function, which i feel like is the same thing thats happening here with the escape keys in it. its making a reference to the function doMe, but i want the function to fire automatically when the page loads.
Ok, i was able to figure out the answer I needed using JQuery.
$("document").ready(function() {
setTimeout(function() {
$(".btn").trigger('click');
},10);
});
essentially what this function does is wait for the page to load then after 10 milliseconds it targets all buttons on the screen with the class name btn and triggers the click functionality of the button, thus firing off the button on the page load.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<?php
if (!empty($newsermonstomove)) {
$h = '';
foreach ($newsermonstomove as $vettel) {
$h .= '<div style="padding: 5px;">';
$h .= '<button data-id="' . $vettel->ID . '" class="btn-vettel btn btn-primary btn-xs">s3</button><span style="padding-left:5px;">Channel: ' . $vettel->ChannelID . ', Sermon: ' . $vettel->ID . ' ' . $vettel->SermonTitle . '</span> <div style="display:none;color:blue;" id="msg_'.$vettel->ID.'"></div><br/>';
$h .= '</div>';
}
} else {
$h = '<h3>No new sermons.</h3>';
}
echo $h;
?>
<script>
$(function() {
$('.bt-vettel').on('click', function(e) {
var id = $(this).data('id');
doMe(id);
});
});
<script>

How can i make my paragraph go to the next line after a certain width

So a string is being entered from a database into a paragraph, when the paragraph has spaces the paragraph breaks perfectly but if the string has a word longer than the line it goes off the page as shown below, how can i make it go the the next line instead of off the page?
here is my php and html code:
<div id = "comment">
<?php
echo "<div style = 'border-bottom-style: solid;border-bottom-width:1px;'><p class = 'word'>" . $issue . "</p>";
echo "<p class = 'word info'>" . $author . ' ' . $date . "</p></div>";
$sql2 = "select * from Ticket_Message where Ticket_Main_id = '$MainId'";
$query2 = mysqli_query($con,$sql2);
while ($row = mysqli_fetch_array($query2)){
$message = $row['message'];
$auth = $row['auth'];
$date = $row['date'];
echo "<div style = 'border-bottom-style: solid; border-bottom-width: 1px;'>";
echo "<p class = 'word'>" . $message . "</p>";
echo "<p class = 'word info'>" . $auth . ' ' . $date . "</p>";
echo "</div>";
}
?>
</div>
and here is my css attempt to fix it :
p{max-width: 250px;}
basically what i need am trying to do is use css to prevent all paragraphs from extending off the page when the string is longer than the width of the page.
Try:
p { word-wrap: break-word; }
https://developer.mozilla.org/en-US/docs/Web/CSS/word-wrap
You don't need any php code here.
Use css property word-wrap
p {
word-wrap: break-word;
max-width: 250px;
}
Demo: http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_word-wrap

Highlighting results from db

I reading results from DB and i would like highlight (change color) by different value:
my reading function
{
...
...
echo "<td>" . $row['value1'] . "|</td>";
echo "<td>" . $row['value2'] . "|</td>";
}
and if value1 = 1 then all results in row is red if value1 =2 then this row is green etc?
it's posible to do with php or just javascript/jquery?
It's always better to use class attribute, because at this time you may want just change background color, but later also change color, font-size or anything...
my reading function
{
...
...
echo "<td class='custom-value1-{$row['value1']}'>" . $row['value1'] . "|</td>";
echo "<td class='custom-value2-{$row['value2']}'>" . $row['value2'] . "|</td>";
}
and css:
.custom-value1-1 {
background: lightblue;
...
}
.custom-value1-2 {
background: darkblue;
...
}
this way you make your work easier and clearer.
Yes you can do it like this:
my reading function //Change this to your loop or condition
{
...
...
$style = '';
switch($row['value1']){
case 1:
$style = 'background-color:#FF0000';
break;
case 2:
$style = 'background-color:#00FF00';
break;
}
echo "<tr style='". $style ."'>";
echo "<td>" . $row['value1'] . "|</td>";
echo "<td>" . $row['value2'] . "|</td>";
...
echo "</tr>";
}

Display number of files in the folder using PHP

I've this simple script for displaying all the images in the folder.
<?php
foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images)
{
$filecount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE));
if ($filecount >1)
{
echo "<img width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
else
{
echo "<img width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
}
?>
I want to display number of files in the folder.Giving below what i tried..My problem is that this shows the number of files before each image.
if ($filecount >1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
echo "<img width='75' height='auto' style='margin-right: 3px; alt='".$row["caption"] ."' src=\"".$images."\">";
}
How can I show number of files before the group of images displayed?
You need to display the number of files only after it has collected them.
You can do it by declaring the filecount variable before the loop, then you need to count and sum the value everytime the loop goes to the next result, and eventually display the total number of files.
Here is how the code should be written:
<?php
$filecount = 0;
foreach(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE) as $images)
{
$tempFileCount = count(glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE))
$filecount += $tempFileCount;
if ($tempFileCount > 1)
{
echo "<img width='75' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
else
{
echo "<img width='200' height='auto' style='margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$images."\">";
}
}
if($filecount > 1)
{
echo '' . $user . ' ' .'added ' . ' ' . $filecount . ' ' . 'new photos';
}
?>
First create a function that returns all images as array. This will throw an exception if there is an error with the glob php function (will mostly never happen, but worth it to know it when it happens). The function will give the sense that the code is a little bit cleaner.
<?php
function getAllImagesOnDirectory($directory) {
$imagesArray = glob("".$filePath."/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}",GLOB_BRACE);
if(!is_array($imagesArray)) {
throw new RuntimeException("There is a problem getting images on directory " . $directory);
}
return $imagesArray;
}
//Then we call this function to get all the images.
$allImagesOnFilePath = getAllImagesOnDirectory($filePath);
$numberOfImages = count($allImagesOnFilePath);
if($numberOfImages > 0) {
echo '<p>' . $user . ' added ' . $numberOfImages . ' new photos</p>';
//If the number of images is more than 0 then traverse all the images and echo them
foreach($allImagesOnFilePath as $oneImage) {
echo "<img style='max-width:100%;height:auto;margin-right: 3px; border:1px solid #dddddd' alt='".$row["caption"] ."' src=\"".$oneImage."\">";
}
}
else {
//The user has no uploaded images
echo '<p>' . $user . ' has not added new photos yet :(</p>';
}
This way we can use the $numberOfImages before using foreach and thus doing what you requested.
We also here use the style attribute to apply width and height to images. See this article to see how to make autoresizable images: http://unstoppablerobotninja.com/entry/fluid-images

Call specific variable based on what another variable is set to.(PHP)

I am not exactly sure how to word this in the form of a short question...the above called up many different answers that were not related, but I apologize if this has been asked before.
I am trying to make a variable name itself based the value of another.
I could simply create if statements for each one, but if I were able to declare the variable the way I want, it would save me about 20-30 lines of code, and make future additions much easier.
Here is a better description.
This is the code that I am using at the moment. It is within a shortcode function for wordpress, to create a button based on user-given parameters.
extract(shortcode_atts(array(
'size' => 'medium',
'link' => '#',
'text' => ''
), $atts));
$large_button_img = of_get_option('large_button_arrow_upload');
$button_pos = of_get_option('button_image_position');
if($button_pos == 'right' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'>" . $text . "<img src='" . $large_button_img . "' id='button_img' alt='button image' /></a>";
}elseif($button_pos == 'left' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'><img src='" . $large_button_img . "' id='button_img' alt='button image' />" . $text . "</a>";
}else
{
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
In the above:
The function pulls the value of "size", "link" and "text" from the user given shortcode and generates a button. It sets the class based on the size...
At the moment I have it to where the user can set different images for a large, medium and small button.
Question:
IS IT POSSIBLE
to return the name of the image source based on what size is set to.
soooo basically the equivalent of?
img src = '" . $($size)_button_img . "'
Where it places value of $size in the name of the variable that it pulls to tell it which image source to pull afterwards? (so the proper equivalent of the above would produce something like
img src= '" . $large_button_img ."'
or, if the user has medium selected
img src= '" . $medium_button_img . "'
If possible this save myself having to write if-statements for every possible option (basically copy the set of if-ifelse-else from above, everytime I have a new size setting available)...which eventually may become more of an efficiency issue.
Thanks in advance for any help that can be given :)
ALSO
Please ignore any syntax errors in the above set of code...I am working on this as you read this, so more than likely, if you see something wrong, it has already been fixed.
You can simple use function for your logic:
function somethingWithImage($img, $pos) {
if ($pos == 'right' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'>" . $text . "<img src='" . $img . "' id='button_img' alt='button image' /></a>";
} elseif ($pos == 'left' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'><img src='" . $img . "' id='button_img' alt='button image' />" . $text . "</a>";
} else {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
}
And just call it whenever you want with any arguments.
What you are looking for, are variable variable names.
Short example:
$hello_text = 'Hello World!';
$bye_text = 'Goodbye World!';
$varname = 'hello';
echo ${$varname}_text; // Hello World!
// Is the same as:
echo $hello_text; // Hello World!
As I mentioned, you can use interpolation to create variable variables:
$src = ${"{$size}_button_img"};
But may I suggest using arrays instead? You get cleaner and understandable code:
$sizes = array(
'large' => ...,
'medium' => ...,
);
if(isset($sizes[$size]))
$src = $sizes[$size];

Categories