I have this code which I am using to display random images. But the images show up at the top left corner of the site. I want to be able to position the image as I wish. What are the changes I have to do in the code order to the above mentioned.
Here's the code--
/*
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to * appear: <?php include"randomimage.php"; ?>
*/
// Change this to the total number of images in the folder
$total = "2";
// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";
// Change to the location of the folder containing the images
$image_folder = "sample.url.com";
// You do not need to edit below this line
$start = "1";
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
?>
Thanks in advance
This would be a CSS solution, PHP can't position images. With CSS you can position things in many different ways:
Using margins (e.g., margin: top right bottom left;)
Using paddings (e.g., padding: top right bottom left;)
Using floats (e.g., float: right or left;)
Using positions (e.g., position: absolute or relative; and then using top/left and bottom/right to position).
For example, you can center your image to the middle of the page using margins. Add this to the top of your page:
<style type="text/css"> /*Initializing CSS code*/
img { margin: 0 auto; }
</style>
Or you can float the image to the far right of your page using a float, assuming the parent object has a width of 100%:
<style type="text/css"> /*Initializing CSS code*/
img { float: right; }
</style>
Or using an absolute position to position it at the bottom right:
<style type="text/css"> /*Initializing CSS code*/
img {
position: absolute;
right: 0px;
bottom: 0px;
}
</style>
You may want to read a CSS tutorial to learn the differences between all the positioning techniques and when to use them and where + little hacks, annoyances and incidents that come when you use each of them.
http://www.google.com/search?q=css+tutorial
You need to modify your html code.
In your case you need to change value of this string:
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
Like this:
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ style=\"Your css style goes here\"/>";
Please learn some of the basics before asking on Stack Overflow.
http://www.w3.org/Style/CSS/learning.en.html css guides.
While the CSS answers are good, not every project will need or want CSS. This alternative solution, given your code, works just fine within the PHP tags:
(I had to put a space after the first < or it wouldn't display properly on this page. Just remove that space from each line to make it work)
Align Right:
< right>
< img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
< /right>
Align Center:
< center>
< img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
< /center>
Align Left:
< left>
< img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
< /left>
This is technically an HTML solution, but since it works within PHP tags you can use this to effectively position anything you like.
Related
I am trying to create a three column layout with PHP/HTML/CSS. The left column contains an image then text underneath that image, each image and text is on a separate line. The right column is the same as the left column, but with different images and texts. The middle column contains images and texts NEXT to each other, so one image and one text is on one line, one image and one text is on the next line and so forth. I have a container div that will house these columns, the container is 85% of the page. So each column will be roughly 28%. Right now, I am focusing on just the left and right columns. These columns only have images and not text for simplicity sake (My output image). When I run the code, the output is each image sitting next to each other, instead of each image being on a separate line floating to their respective positions.
PHP/HTML
<?php
$resultSet = $db->query("SELECT * FROM table");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
$images = $rows["image"];
$text = $rows["text"];
echo "<div id=container>";
if ($id > 3 && $id < 8)
{
echo "<div id=left>";
echo "<img src=$images>";
echo "<p>$text</p>";
echo "</div>";
}
if ($id > 8)
{
echo "<div id=right>";
echo "<img src=$images>";
echo "<p>$text</p>";
echo "</div>";
}
echo "</div>";
}
}
?>
CSS
#container{
position: relative;
margin: 0 auto;
width: 85%;
height: auto;
}
#left{
float: left;
width: 28.33%;
}
#left img{
width: 100%;
}
#right{
float: right;
width: 28.33%;
}
#right img{
width: 100%;
}
This is how the output looks like:
Dataset:
<div id=container></div><div id=container></div><div id=container></div><div id=container><div id=left><img src=http://www.clker.com/cliparts/R/r/2/q/P/4/blue-number-one-hi.png><br><p>4</p></div></div><div id=container><div id=left><img src=http://bsccongress.com/im3/blue-number-two-clip-art.png><br><p>5</p></div></div><div id=container><div id=left><img src=http://www.clker.com/cliparts/L/H/T/b/g/N/three-md.png><br><p>6</p></div></div><div id=container><div id=left><img src=http://2.bp.blogspot.com/-x0uSxqUaYQA/UCEr1WV_1AI/AAAAAAAAC_0/QHrtbsfcK1s/s1600/blue-number-four-md.png><br><p>7</p></div></div><div id=container></div><div id=container><div id=right><img src=http://bsccongress.com/im7/blue-number-five-clip-art.png><p>9</p></div></div><div id=container><div id=right><img src=http://www.clipartbest.com/cliparts/Rid/gq7/Ridgq7AnT.png><p>10</p></div></div>
It is creating an inline of the images instead of putting them on separate lines. How can I correct this problem?
Grid layout
Since you are looking for a grid layout, you would find a framework such as Bootstrap very helpful. With Bootstrap, your HTML would be as simple as:
<div class="row">
<div class="col-xs-4">Left</div>
<div class="col-xs-4">Middle</div>
<div class="col-xs-4">Right</div>
</div>
If you don't want to use a library for some reason (why not?) then take a look at their CSS.
Don't repeat yourself
Create your class as a variable, then create the HTML:
$class = 'middle';
if( something_here ){
$class = 'left';
}
echo "<div class='$class'>Content</div>";
Convert special characters
Your database variables might contain characters that cause errors in your HTML. To prevent that, create a simple function:
function html($str){
return htmlspecialchars($str, ENT_QUOTES);
}
Then use:
<?php echo html($row['field_name']); ?>
Your images issue
If you want your images to be blocks, simply use a class called block on your images, and create this CSS:
.block{
display:block;
}
PHP templating
Instead of:
$this = $row['this'];
$that = $row['that'];
echo "<p>$this</p>";
echo "<div>$that</div>";
You can actually jump between PHP and HTML:
<?php if( something_here ){ ?>
<p><?=html($row['this'])?></p>
<div><?=html($row['that'])?></div>
<?php } ?>
Now your code is shorter, easier to edit, and easier to read, including your source code.
I have a php website which generate invoice in the form of html tables, I need to print these html tables to dot-matrix printer. I have tried to print webpage directly with browser print option, but it seems the printer treat it as image because it prints characters dot-by-dot instead of complete characters in a single pass like it would an ascii text file, which result in blurred characters.
Is there any way to make the printer treat it as text file? Or is there any way to convert html page to text file without losing the position styling(spacing, margins, etc)?
Or maybe there's an alternative approach I could use?
One thing to note is I can't use text-based browser to do this as it will be used by clients.
The invoice is a html table with small logo at the top-left, title, and description as thead, and simple table cell with borders as tbody.
I use Epson LX300+II printer.
<?php
// Download printer driver for dot matrix printer ex. 1979 Dot Matrix Regular or Consola
?>
<html>
<head>
<title>PHP to Dot Matrix Printer</title>
<style>
#font-face { font-family: kitfont; src: url('1979 Dot Matrix Regular.TTF'); }
.customFont { /* <div class="customFont" /> */
font-style: kitfont;
font-size:10;
}
#mainDiv {
height: 324px; /* height of receipt 4.5 inches*/
width: 618px; /* weight of receipt 8.6 inches*/
position:relative; /* positioned relative to its normal position */
}
#cqm { /* <img id="cqm" /> */
top: 10px; /* top is distance from top (x axis)*/
left: 105px; /* left is distance from left (y axis)*/
position:absolute; /* position absolute based on "top" and "left" parameters x and y */
}
#or_mto {
position: absolute;
left: 0px;
top: 0px;
z-index: -1; /*image */
}
#arpno {
top: 80px;
left: 10px;
position:absolute;
}
#payee {
top: 80px;
left: 200px;
position:absolute;
}
#credit {
top: 80px;
right: 30px; /* distance from right */
position:absolute;
}
#paydate {
top: 57px;
right: 120px;
position:absolute;
}
</style>
</head>
<body>
<?php
//sample data
$arpno = 1234567;
$payee = "Juan dela Cruz";
$credit = 10000;
$paydate = "Dec. 6, 2015" ;
?>
<div id="mainDiv"> <!-- invisible space -->
<div id="cqm" class="customFont">ABC TRADING</div>
<div id="arpno" class="customFont"><?php echo $arpno; ?></div>
<div id="payee" class="customFont"><?php echo $payee; ?></div>
<div id="credit" class="customFont"><?php echo $credit; ?></div>
<div id="paydate" class="customFont"><?php echo $paydate; ?></div>
<img id="or_mto" src="or_mto.jpg" /> <!---- sample for logo ---->
</div>
</body>
</html>
For anyone looking to print directly from browser to printer, and assuming you need to print text only, I found the answer in this post how-to-print-page-in-plain-text-format-for-matrix-dot-printer
Github for the application is here.
A simple tutorial can be found here.
Basically you install QZ Tray in your client machine and you add the script provided by them in your html.
Works OK in Windows 10 with a OKI 320 Turbo Dot Matrix Printer, but you have to change the driver for the printer to Generic / Text Only.
Then in javascript you just do something like this:
try {
await qz.websocket.connect();
console.log("connected")
const printer = await qz.printers.find("OKI");
console.log(`Printer ${printer} found !`);
const config = qz.configs.create(printer);
const data = [
'Some Data \n',
'More Data and More Data \n'
]
qz.print(config, data);
} catch(e) { console.log(e) }
Works like a charm!
We used to do this using a Java Applet that could write to the LPT port after due permissions on the browser and Java security.
However support for this has been phased out by all browsers. We are running it right now by using older versions of the browsers.
One way we are exploring is the possibility of a java service running on a port on the local PC and calling this service from the browser window.
an option to solve this problem is to download the TCPDF library https://tcpdf.org/docs/srcdoc/ and pass the reports to pdf but you have to download some dotmatrix type font and using the link http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf, convert fonts to the format that TCPDF uses.
I had almost same requirement like this, i didn't get a perfect solution, so I used an alternative solution,
converted the html to text with the help of "w3m"
send this text to a python script running on a client machine through socket communication
through the python script(used pyusb) i sent this text directly to the printer via USB and printed.
In this solution, can't get all the solution in print.
I'm creating a horizontally scrolling web site. There's a container div which I want to retain a fixed height but expand as required horizontally to fit the content inside it. At the moment the div only expands horizontally as far as the page width. There are actually 9 images to display but only the first 4 are shown. See code and image below. How do I make the container div expand horizontally to show all images please?
css:
body
{
background-color:#dbdbdb;
}
div.infinite-container
{
background-color:#db0080;
height:180px;
}
img.infinite-item
{
width="320";
height="180";
margin-right:8px;
margin-bottom:8px;
display:inline-block;
}
.infinite-more-link
{
visibility:hidden;
}
PHP:
<div class="infinite-container">');
if ($num_results > 0)
{
$array = array();
while ($row = mysql_fetch_assoc($result))
{
$array[] = $row;
}
for ($i = 0; $i < $numImagesPerPage; $i++)
{
$filePath = "animations/".$array[$i]['animationid'].".gif";
echo('<img class="infinite-item" src="'.$filePath.'"/>');
}
}
echo('</div>');
This screenshot is after the changes below suggested by Andrei. The pink area is the container div. The images appear to break out below it.
From the code you posted, doing something like this should work:
body
{
background-color:#dbdbdb;
overflow:auto;
}
div.infinite-container
{
background-color:#db0080;
height:180px;
display:inline-block;
white-space: nowrap;
}
img.infinite-item
{
width: 320px;
height: 180px;
margin-right:8px;
margin-bottom:8px;
display:inline-block;
}
jsFiddle example:
http://jsfiddle.net/S6Abd/
What this does is:
set the display mode to inline-block on the container. This way the container will be as large as needed to contain all items.
set overflow:auto on body to show scroll-bars.
correct the width and height of each item.
add white-space: nowrap; to the container to force the items to stay on one line.
Add this CSS style :)
div.infinite-container
{
width:2952px; /* (320 + 8) * 9 = 2952 */
}
But on the serious note - DIV shows (kind of) all your images, only images 5-9 are in next line and because container have fixed height, then they are hidden.
Okay, so I've been able to define the standard text color according to a database value by using this code:
<?php
echo '<div style="color: ' . $result->properties->fcolor . ';';<br />
echo 'background: ' . $result->properties->bgcolor;<br />
echo '">MY CONTENT</div>'
?>
The color that's set by
$results->properties->fcolor
works nicely, except for a:link, a:hover, a:visited, and a:active. Because it's only defining "color", the browsers default to their own link colors.
My users choose from a selection of background and font colors and Chrome's default blue link color doesn't exactly work with a dark purple background... Is it possible to set up a
<style type=text/css>
inside of my PHP file and have it reference the
$result->properties->fcolor
value that the normal part of the script pulls from my database?
This is my first big site so I'm not positive about anything but I vaguely remember enabling PHP in my external CSS file using .htaccess and it didn't successfully pull in the value of
$result->properties->fcolor
as far as I could tell.
What am I doing wrong?
Thanks in advance - amazing community here! :)
IF I'm you, I would create external css for links just to define rules not color.
for example if your links are inside div, what you can do is just define to use color of div
div a { color: inherit; }
div a:HOVER { color: inherit; }
so when you define your color to div through php logic your links inside div will inherit that color.
If you experience any problems with this add important to property so color of div will be used
div a {color: inherit !important; }
YOu can set you link colors via internal css
<style type="text/css">
a:link { color:#f00;}
a:visited { color : }
a:hover {}
a:active { }
</style>
Note:
1 orders are very important .
2 use hex value instead of color name.
Hi have you tried using " instead of ' and \" instead of "?
echo "<div style=\"color: " . $result->properties->fcolor . ";";
echo "background: " . $result->properties->bgcolor;
echo "\">MY CONTENT</div>";
And, you seem to be missing a semi colon at your 3rd echo.
I'm trying to create an unordered list of <a>text1 text2 text3</a> elements, with a while loop. This list is then styled using #sidebar li a in my CSS.
My problem is that the text1, text2, text3 that is passed into each <a> element in my while loop can take on different lengths and I would like for them to be spaced equally like a table. However, I CANNOT use a table, because to format like a table, requires me to do this....
<li><a><tr><td>text1</td> <td>text2</td> <td>text3</td></tr></a></li>...
and because of that, my CSS "background" image will repeat for EACH <td>, when I only want the background image ONCE for each <tr>...(using different style tags than shown below)
Is there a way to change my while loop to space my text1,text2,text3 evenly like a table (without using a table) and maintain my CSS background image ONCE per each <li>? Any help would be INCREDIBLY appreciated!
My PHP file
while($row = mysql_fetch_assoc($result))
{
echo "<ul id=\"sidebar\">";
echo "<li>" . $row['column1'] . " ". $row['column2']. " ". $row['column 3']."</li></ul>";
}
My CSS file
#sidebar li a {
background: url(../images/sidebar.gif) no-repeat 0px 0px;
}
while($row = mysql_fetch_assoc($result))
{
echo "<ul id=\"sidebar\">;
echo "<li><span class="psuedo-col">" . $row['column1'] . "</span> <span class="psuedo-col">". $row['column2']. "</span> <span class="psuedo-col">". $row['column 3']."</span></li></ul>";
}
Add <span>s around the content from the $row['...'], in order that the css has something to serve as a hook, and set an explicit width on those spans. Bearing in mind that if the content of the spans is too large it will either require an overflow rule (hidden, visible or auto) or your content will start to look odd.
As an example
span.psuedo-col {
display: inline-block;
width: 10em;
overflow: hidden;
}
Or you could use
`display: block;
/* other stuff */
float: left; /* or right, depending on your alignment requirements */
The floats, obviously, will take the contents of the spans out of the flow of the document, perhaps causing the <li> itself to collapse, sine it'll have no content.
Asfar as I understand your question, you want your LI elements to have a fixed width like TD in a table:
#sidebar li {
float:left;
width:33%; /* three columns with equal width */
}