php variable height inside div - php

I have the following php statement,
echo "<div style=\"background-image: url('$logo'); height: '$ratioxy';\"></div>";
I did this to make the height of the div variable depending on the background image. The code correctly inserts the $logo variable, but not the $ratioxy variable. The variable $ratioxy gives a value when I echo it, but it won't give the value inside the div. Is there any reason for this? Thanks in advance.

Try to use:
echo "<div style=\"background-image: url('{$logo}'); height: {$ratioxy}px;\"></div>";
Height should be specified with units (%, px, em, etc) because "just some digits" are not obvious for browser. Also it should be specified without quotes.

Related

Adding text below image, output depends on input language?

I'm trying to create a website such that users can upload images and captions of those images. I want to format those captions such that really long captions will not exceed the width of the image, but instead switch to lines. This is my CSS code:
<style type="text/css">
<!--
.figure{
display:table;
width:50px;
}
.figure.caption{
display:table-caption;
caption-side:bottom;
}
-->
</style>
and this is my php code to display images and captions:
<?php
include('db.php');
$result = mysql_query("SELECT * FROM photos");
while($row = mysql_fetch_array($result))
{
echo '<div class="figure">';
echo '<img src="'.$row['location'].'">';
echo '<p class="caption">'.$row['caption'].' </p>';
echo '</div>';
}
?>
I tested my code. Surprisingly, when I used Chinese as the language for the caption, the text switch lines and does not exceed width of image, but when I used English, the caption does not switch lines. This is the screenshot:
This is really interesting because a CSS code either "work" or "does not work", and it shouldn't selectively work for one language over the other.
Further testings reveal that as long as I have "width:50px" for the .figure tag, the caption in Chinese will switch lines, otherwise the caption in Chinese will exceed width of the image. Changing it to "width:20px" or "width:80px" has no impact on the output. Deleting or adding properties of width has no impact on the caption in English.
Can someone tell me what's wrong with my code and how to revise it such that it will work for both languages, or point out a way such that a caption entered by user will not exceed width of the images? Thank you in advance!
use the following css for the caption's text,
{
word-wrap: break-word;
}
As #Shoyeb Sheikh said you need word-wrap: break-word; to wrap the word. but you are missing the width to wrap the word which is the container width for those content. So I added width: inherit; to .caption and also added for img. check the example yourself.
also: hahahaha is not same as 哈哈哈哈, ha ha ha ha = 哈哈哈哈
you see the difference, because Chinese 哈 is a word.
https://jsfiddle.net/dalinhuang/9Lrmk9vu/1/

adding HTML image size in PHP

I'm new to PHP and coding in general so apologies for this likely silly question. I know the answer will be extremly simple but try and I might, I just can't see it.
I'm trying to pull image path data from my database and concatenate with the code below so that I can enventually display it on my site using <?php print.... ?>
I have successfully done this. The problem I have now is setting the size of this image.
Please see my code below.
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> ";
I will be indebted to anybody who can help on this.
I have researched this question and came across some answers but just could not make them work with my problem.
I's just the image size I have an issue with, nothing else.
I ended up using the code below and then using CSS. This was inspired by #Ndianz's answer.
$ImagePath = " <img src = db_images_product/".$row['ImagePath']." ".'Class="ProductImage"'."/> ";
I have ran all this code through an editor and it runs fine with no errors.
To illustrate concatenation here is a link with many scenario and explenations to help you,
Click Here
This is the way you currently have it,
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> "
This is the way you would write that,
$Image_Path = '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';
I suggest you do this,
$Image_Path = '<img src="db_images_product/'.$row['ImagePath'].'">';
Then in your css file add the style to your img tag like this,
img {
height: 100px; //or whatever you want it to be.
}
You can use getimagesize() to grab the width and height then set the property on the image tag. the $row['ImagePath'] needs to be reachable by the script so you might have to play with this and add some ../ to get to the relative path of the image.
list($width, $height) = getimagesize($row['ImagePath']);
$Image_Path .= "<img src='db_images_product/{$row['ImagePath']}' height='{$height}' >";
You've just got some syntax errors. When you concatenate be careful about where your quote marks go. I'd use single quotes to open and close your PHP string so as not to conflict with the double quotes in your HTML. So pay close attention -- the single quote closes the PHP string right before the dot concatenates the $row variable. Then after the variable you add another dot to concatenate the end of the PHP string.
$Image_Path .= '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';

Passing a PHP var to a JavaScript function

I went over many posts regarding this issue, yet can't figure out how to escape this syntax.
I'm simply trying to pop up an editing window via PHP echo of a JavaScript window.open and pass that window a prame from MySQL.
The code is echo'ed out along with a table that is dynamically generated via PHP.
Inside that echo the last line is the problematic one, that I can't seem to get to function properly.
E</td>
Note: I have went over many posts here dealing with this issue, and tried out many solutions, none of which worked out. I'm sure this is very simple to handle, yet I have been steering at this thing it is slipping my attention.
You can embed PHP into your HTML:
<a href="#"
onClick="window.open(
'popup.php?message=<?php echo $Url; ?>',
'myWindow',
'status = 1, height = 350, width = 1022, resizable = 0'
)"
class="bstyle">
E
</a>
P.S. The weird indentation is just so that you can read it here on SO. Don't actually use it in your code.
Use a backslash to escape characters in PHP.
echo 'E</td>';
echo '<a href="#" onClick="window.open(\'popup.php?message='.$Url.'\', \'myWindow\',
\'status = 1, height = 350, width = 1022, resizable = 0\')" class="bstyle">E</a></td>';
If you were writing html, and wrote this:
<a href="#" onClick="window.open( "popup.php?message=blah", "myWindow",
"status = 1, height = 350, width = 1022, resizable = 0 " )" class="bstyle">E</a></td>
your onclick wouldn't work because it would just hold the value window.open(. You can define the string within the string value of onclick using single quotes. And since in my case I (not sure if you are, but it looks like it) am defining my string to echo using single quotes, I need to escape the ones in my string with the \ character.
You can put a variable inside your HTML code by echo'ing the variable.
E</td>
I would also like to recommend urlencode in this case.
The reason behind this is, because you echo out the $Url variable.
Whenever $Url contains HTML characters like for example " or ' then your javascript call will break.
In your popup.php you can then urldecode the message so that it becomes normal again.

Set line height to text inside a variable

I have a variable, inside there's a long sentence:
$myvar = 'i am a quite long sentence, more than 500 chars';
With css, I echo it, and I get 3 lines of text.
echo '<p>'.$myvar.'</p>';
How do I set css line height to it, cause it seems when I echo the query my line-height of 12px is ignored.
Any ideas?
Not sure I was very clear with my q.
P.S. Yes, didn't know how to explain :). Sorry... The variable gets it's value from an xml field... and, how can i explain, its echo'ed like a big chunk of teext, ignoring the styles
There are several ways to set the line-height, the easiest way is
echo '<p style="line-height:12px">'.$myVar.'</p>';
A more elegant way would be to define a class or set for all your paragraph tags a line-height.
I. css global line-height for all paragraph tags
p {line-height: 12px}
II. css file with class for line height
p.foo {line-height: 12px}
corresponding php code
echo '<p class="foo">'.$myVar.'</p>';

How to let text adapt to a set table width instead of stretching the table?

I thought that this would make a table with a set width, and that text would automatically try to fit in by starting on a new line. However, the table still gets stretched by long lines of text.
<HTML><center><table width="300" border="1"><tr><td>
<?php
If (file_exists("file.txt")){
Echo nl2br(file_get_contents("file.txt"));
}Else{
Echo "File not found.";
}
?>
</td></tr></table></center></HTML>
I think I'm forgetting something absolutely essential here.. 0.o
Change the code to this:
echo nl2br(wordwrap(file_get_contents("file.txt")));
There is a built in function in PHP called wordwrap for such tasks.
The text needs to have spaces in order for it to fit in that width. If you have one extremly long word, it will be displayed entirely on one line. You could use the php function wordwrap, which allows you to set the width of the line to a certain number of characters (http://php.net/wordwrap)
Try do it by CSS:
word-wrap:break-word
Also change your width= to style="width: 300px"

Categories