I want my website to have a random background every time it is refreshed, but after someone logs in, I want this background to stay the same the rest of the session. I have found the following code for the random background:
<?php
$bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg', 'bg-08.jpg', 'bg-09.jpg', 'bg-10.jpg', 'bg-11.jpg', 'bg-12.jpg', 'bg-13.jpg');
$n = mt_rand(0, count($bg)-1);
$selectedBg = "$bg[$n]";
?>
For your interest: this is how I made sure the selected bg was truly the background of the page (this is put in the <head> section):
<style type="text/css">
body{
background: url(../afbeeldingen/<?php echo $selectedBg; ?>) ; background-size: 100%; background-attachment: fixed;
}
</style>
This works fine. but now I want to put the output in a session. At first i thought this would work:
$_SESSION['background']= "$selectedBg";
This does put a random number in a session but that number is not the same as outputted with the mt_rand before. I have also been struggling with the following JavaScript
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 100) + 1;
</script>
but I could not match the output to $bg array presented before.
I am still a beginner in PHP.
Do it like this,
session_start();
if(!isset($_SESSION['logged_in'])){
$selectedBg = "bg-".rand(1, 13).".jpg";
$_SESSION['background'] = $selectedBg;
echo $_SESSION['background'];
}else{
echo $_SESSION['background'];
}
When the guest is here, they will get a new background and you can use rand() function rather than the array you have, it will help you,
And second you check if the user is logged in you give them the last background they got, if not, change the background.
I hope this helps you,
Related
On my main page website, I have 6 tiles and I would like to change the background on the hover effect. I found PHP script below but something is not working properly
In my index.php file below tag, U put below code
<?php
$bg = array('nasa1-400.jpg', 'nasa1-1200.jpg', 'nasa1-640.jpg'); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
and in my CSS file, I use the below code
div.zsm_block_news > div.zsm_block_content > div:nth-child(1) > div:nth-child(1):hover
{
background: url(images/bg/<?php echo $selectedBg; ?>) no-repeat;
}
Do I miss something?
That css code does not work in css file.
because there is some php code in there.
so if you want to run it, you must use css code in the php/html file.
example.php:
<html>
<head>
...
<style>
div.zsm_block_news > div.zsm_block_content > div:nth-child(1) > div:nth-child(1):hover
{
background: url(images/bg/<?php echo $selectedBg; ?>) no-repeat;
}
</style>
</head>
<body>
so your index.php page could be like this:
<html>
<head>
<?php
$bg = array('nasa1-400.jpg', 'nasa1-1200.jpg', 'nasa1-640.jpg'); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<style>
div.zsm_block_news > div.zsm_block_content > div:nth-child(1) > div:nth-child(1):hover
{
background: url(images/bg/<?php echo $selectedBg; ?>) no-repeat;
}
</style>
</head>
<body>
This code will show you a new background image whenever you refresh the page.
If you want to show a new image in every hover without needing to refresh the page, so you have to either use javascript code or javascript + ajax(for fetch new images as live).
EDITED:
for that purpose first build a page named rmdimage.php
<?php
$bg = array('nasa1-400.jpg', 'nasa1-1200.jpg', 'nasa1-640.jpg'); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
echo $selectedBg;
and you index.php file:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$("div.zsm_block_news > div.zsm_block_content > div:nth-child(1) > div:nth-child(1)").hover(function(){
$.get("[path-to-php-folder]/rmdimage.php", function(data, status){
$("div.zsm_block_news > div.zsm_block_content > div:nth-child(1) > div:nth-child(1)").css('background', 'url(images/bg/' + data +') no-repeat;');
//alert("Data: " + data + "\nStatus: " + status);
});
});
</script>
</head>
<body>
<div class="my-element" style="width:100px; height:100px;">this is my fav element!</div>
you must change [path-to-php-folder] to relation rmdimage.php file.
in the rndimage.php file you can dynamically moderate image array ($bg)
you can fetch them from mysql or a folder , ...
I am building a site on Magento2 platform.
I need to add images for painting done by Artists for sale.
what is required is that in addition to image being uploaded, I want to add 2-3 pre-defined background images to the painting.
for example - Dining Room, Living Room.
I am able to change background image on per page load(randomly) using this code.
<head>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<?php
$bg = array('SET_A_01_Living.jpg', 'SET_A_02_Bed.jpg', 'SET_A_03_Study.jpg', 'SET_A_04_Dining.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
but how I add the background to the image and in Magento Gallery, I am not able to make out.
some guidance/hint on this will help me achieve, what I am trying to do.
you have to write php above the head tag like below :-
<?php
$bg = array('SET_A_01_Living.jpg', 'SET_A_02_Bed.jpg', 'SET_A_03_Study.jpg', 'SET_A_04_Dining.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = $bg[$i]; // set variable equal to which random filename was chosen
?>
<head>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
I'd like to get this random background image coding to work: http://css-tricks.com/snippets/php/randomize-background-image/
I've double checked file names, paths, all the code and the instructions. I think there's something I don't understand.
You can see the page here:
http://agentboris.com/newwebsite/index.html
Thanks for your help.
Your file at http://agentboris.com/newwebsite/index.html is in .html, it should be in .php for you be able to use php code from tutorial.
Try with this simple code (don't forget to save your index file as php extension):
<?php
$bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<!DOCTYPE html>
<html>
<head>
<title>Tests purposes</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<body>
</body>
</html>
I am making a website that has two background images, one of them is always the same i.e.:
background-image: url("../images/centre-image.jpg");
and the other is generated via php to show a background image at random:
<?php
$bg = array('large-top1.jpg', 'large-top2.jpg', 'large-top3.jpg', 'large-top4.jpg', 'large-top5.jpg', ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<img src="images/background-images/<?php echo $selectedBg; ?>" class="bg">
Making the first background the full size of the DIV no matter what size of the screen was easy enough:
div.centre-options {
background-size: 100% auto;
}
however if I try to add similar CSS to the random generated image it doesn't work (just shows it at its full jpg size and then repeats) and not sure show to solve the problem.
As far as i can tell the image generated isn't set as a background but as a image to display, therefore you can't apply background styling to it.
There is 2 things you can do either apply styling directly to the image tag i.e.
img.bg{
width:100%;
height:100%;
}
or alternatively create an element and add the generated image to it's styling
would look somthing like this:
<?php
$bg = array('large-top1.jpg', 'large-top2.jpg', 'large-top3.jpg', 'large-top4.jpg', 'large-top5.jpg', ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<style>
div.bg{
background-image: url("images/background-images/<?php echo $selectedBg; ?>");
background-size: 100% auto;
}
</style>
<div class="bg"></div>
Preferable put the styling between the head tags
How can I set my Iframe height to be dynamic to its content.
The content is only PHP code, nothing else.
I use the php to query database, and then show some ads. The more ads, the higher the iframe should be.
I thought height='100%' should do it, but no...
I have read other Q but their solutions doesn't work for me.
Suggestions ?
Thanks
You have to set height of iframe in javascript. JavaScript should be in page inside the iframe (must be if page with iframe and page in iframe are from different domains).
a.html:
<iframe src="b.html" id="myiframe"></iframe>
b.html:
<div style="height: 500px; background:magenta;"></div>
<script>
parent.document.getElementById('myiframe').style.height = document.body.offsetHeight+'px';
</script>
If the ads are just text, you can use line height * number of lines as the height. I assume you know the font size, and number of lines can be found by counting <br />'s:
<?php
// $content is your ads
// assuming ads.php returns an ad with an id, this example uses ad #10
// We are reading it with a full url because we don't want to read it as just
// a text file (we want it to be interpreted by php before we load it)
$content = file_get_contents('http://www.example.com/ads.php?id=10');
$line_height = 12; // line-height: 12px; is set in CSS
$number_of_lines = substr_count($content,'<br />'); // you can search for <br>
// and <br/> as well if you
// need to
$height = $line_height * $number_of_lines; // You may also want to have padding
?>
<iframe src="ads.php?id=10" height="<?php echo $height; ?>" />
EDIT: changed $font_size to $line_height