Strange lines with many divs - php

The problem can be shown here. I was experimenting with thousands of fixed div boxes to create a randomly generated background. I am aware this has a big impact on performance.
However, I was wondering if there was any solution to the strange white lines in the background of my webpage. I am almost 100% certain there is nothing wrong with my php & css that is determining where the boxes are placed in my background but, here it is just in-case.
define('ROWS', 100);
define('COLUMNS', 100);
$boxes = array();
for($i = 0; $i < ROWS; $i++) {
$boxes[] = array();
for($j = 0; $j < COLUMNS; $j++) {
$boxes[$i][$j] = randColor();
?>
#back<?php echo $i*COLUMNS + $j; ?> {
background: #<?php echo $boxes[$i][$j]; ?>;
width: <?php echo 100.0/COLUMNS ?>%;
height: <?php echo 100.0/ROWS ?>%;
left: <?php echo $j * 100.0/COLUMNS?>%;
top: <?php echo $i * 100.0/ROWS ?>%;
position: fixed;
z-index: -300;
}

Try instead using <canvas> and split the canvas up in pieces instead of divs. It is a rectangular area in a HTML file on which you can draw anything including parts.
Example: Draw a Circle
Javascript
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
Html
<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>

Related

How to generate a download button for each image

I have a script that gets images from a database and displays them on exploretest.php page. I need each image to have a separate download button under each image which allows people to download that image but am unsure on how to go about this. Any help would be appreciated.
<?php
display();
function display(){
$conn = mysqli_connect("localhost","root","","loginsystem");
$sql="select * from testimage";
$query=mysqli_query($conn, $sql);
$num=mysqli_num_rows($query);
for ($i=0; $i < $num; $i++) {
$result=mysqli_fetch_array($query);
$img=$result['image'];
echo '<img class="exploreimg" src="data:image;base64, '.$img.'" style="max-height:300px;max-width:300px; margin-left:50px; margin-right:50px; padding-bottom:20px; padding-top:30px;">';
}
}
?>
echo '<img src="img/downloadbutton.png" alt="Download" style="max-height:50px; max-width: 100px; float: center; line-height: 10; "/>';

Adding z-index to php while loop

I have a procedural while loop that echos out rows in my DB like:
echo '<tr align="left"> <td>'.$row['address'].','.$row['state'].'</td>'.
'<td><a href="'.$row['shopurl'].'">'.$row['datedesc'].'</td>'.
'</tr>';
And works great! The problem now is that I need to add a "SOLD OUT" png across the address & state, of only one (currently) of the rows. I have CSS:
#sold-out{
background-image: url("/graphics/png/soldout.png");
}
With the image as the back-ground.
How can I add a Z-Index to php inside a While loop?
Thanks in advance!
You do not add z-index to PHP, you add it to CSS. However, in this case, you can simply print out an absolutely positioned div with the image in the td that you want.
PHP
foreach ($rows as $row) {
echo '<tr align="left">
<td>';
//conditional to determine if row is sold out
if ($row['soldOut'] === 'yes') {
echo '<div class="sold-out"> </div>';
}
echo $row['address'].','.$row['state'].'</td>'.
'<td><a href="'.$row['shopurl'].'">'.$row['datedesc'].'</td>'.
'</tr>';
}
CSS
.sold-out {
position:absolute;
background-image: url("/graphics/png/soldout.png");
opacity: 0.8;
top: 0;
left: 0;
right: 0;
bottom: 0;

How do I make a bar chart in CSS using PHP variables?

I have an array of float values in PHP and I want to turn the array into a bar chart using CSS. Each float value represents a percentage, so if a value is 50.0 then the bar should be 50% of a certain height. I am new to CSS, so I was wondering if anyone could show me how I would go about doing this.
I believe it should look something like this. I doubt that this code would work, but it's an example.
<?php
$values = array(50.0, 20.0, 30.0, 45.0); //This is the array of percentage values
?>
<style>
<?php
for ($i = 0; $i < count($values); $i++) //Create a new bar for each value
{?>
#bar
{
height: <?php 300 * ($values[$i] / 100.0); ?> px; //Specify the height of each bar
width: 30px;
}
<?php
}?>
</style>
Edit:
I've added the code suggested by Tom, but my web page currently produces no bars. Here is the full code for my PHP file. Why does it not produce any CSS content?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Visualisation result</title>
<style>
.bar
{
color: #00ff21;
width: 30px;
}
</style>
</head>
<body>
<?php
$values = array(50.0, 20.0, 30.0, 45.0);
for ($i = 0; $i < count($values); $i++)
{
$currentHeight = 300 * $values[$i] / 100.0; ?>
<div class="bar" style="height: <?php echo($currentHeight); ?> "></div>
<?php
}
?>
</body>
</html>
Your approach won't work, as you will be writing out several different rules for the same selector. It is invalid in HTML to have multiple values with the same ID "bar" anyway.
I guess you could do something like this:
.bar {
width: 30px;
}
<?php
for ($i = 0; $i < count($values); $i++) { ?>
<div class="bar" style="height:<?= 300 * $values[$i] / 100.0 ?>"></div>
<?php
} ?>
This generates the elements using the loop, setting their height using the style attribute. I have used the shorthand <?= instead of <?php echo (you weren't doing either in your question).

Use php to generate div and select with jquery

I have some jquery question :
First my php code :
<?php
for($C=1; $C <= 9; $C++)
{
echo '<div id="couvert_'.$C.'" style="width:100px;height:30px;float:left;padding-top:10px;padding-left:0px;padding-right:10px;margin:4px 4px 4px 4px;cursor:pointer;text-align:center;background-color:#dfdfdf;">'.$C.'</div>';
}
?>
I generate 9 div element.
I would like to generate jquery script to :
- select one of those div
- change background color of my selected div with #FF7E15
- let the others div with background color #dfdfdf
DO you have an idea ?
Thank you very much.
at the end of your php script use this jquery script:
<script>
//now maybe you want to change the selected div with the mouse?
$("div").click(function(e){
//loop over the divs and set them to the std. color
$("div").each(function(e){
$(this).css("background-color","#dfdfdf");
});
//set the current clicked div to the right color
$(this).css("background-color","#ff7e15");
});
</script>
<script>
jQuery(document).ready(function(){
$('.convert').click(function(){
$(this).css('background-color', '#FF7E15');
});
})
</script>
<?php
for($C=1; $C <= 9; $C++)
{
echo '<div class="convert" id="couvert_'.$C.'" style="width:100px;height:30px;float:left;padding-top:10px;padding-left:0px;padding-right:10px;margin:4px 4px 4px 4px;cursor:pointer;text-align:center;background-color:#dfdfdf;">'.$C.'</div>';
}
?>
Mad answer
PHP:
<?php
for($C=1; $C <= 9; $C++)
{
echo '<div id="couvert_'.$C.'" style="width:100px;height:30px;float:left;padding-top:10px;padding-left:0px;padding-right:10px;margin:4px 4px 4px 4px;cursor:pointer;text-align:center;background-color:#dfdfdf;">'.$C.'</div>';
}
?>
jQuery + PHP:
<?php
$magicElement = 3;
echo "$(\"#couvert_" . $magicElement . "\").css(\"background-color\", \"#FF7E15\")";
?>

Programatically creating divs with different background colors from a php array

I have created divs in a MVC framework (Codeigniter).
From a data array, I am looping through the 'subject' array and making a div for each. I also added a header div and a content div for each subject. So, how do I make the background of the header in each subject have different colors from the colors array. They have to be different unless the number of the subjects is greater than the number of colors in the color array.
Here is the code for looping through each subject:
foreach ($userSubjects as $subject => $info) {
echo "<div class='subject paper'>";
echo "<div class='subjectHeader'>";
echo $info['subject_name'];
echo "</div>";
echo "<div class='subjectContent'>";
echo "</div>";
echo "</div>";
And here is the randColor array:
$colorSet = array(
'#1abc9c',
'#2ecc71',
'#3498db',
'#9b59b6',
'#34495e',
'#f1c40f',
'#e67e22',
'#e74c3c',
'#ecf0f1',
'#95a5a6'
);
I think the best trick is using CSS instead of PHP :
div.subject:nth-child(10n+1) > .subjectHeader { background: #1abc9c}
div.subject:nth-child(10n+2) > .subjectHeader { background: #2ecc71}
div.subject:nth-child(10n+3) > .subjectHeader { background: #3498db}
div.subject:nth-child(10n+4) > .subjectHeader { background: #9b59b6}
div.subject:nth-child(10n+5) > .subjectHeader { background: #34495e}
div.subject:nth-child(10n+6) > .subjectHeader { background: #f1c40f}
div.subject:nth-child(10n+7) > .subjectHeader { background: #e67e22}
div.subject:nth-child(10n+8) > .subjectHeader { background: #e74c3c}
div.subject:nth-child(10n+9) > .subjectHeader { background: #ecf0f1}
div.subject:nth-child(10n+10) > .subjectHeader { background: #95a5a6}
To make it easier to use, you can use this script to generate CSS script;
<style type="text/css">
<?php
$i = 1;
foreach($colorSet as $c)
{
echo "div.subject:nth-child(".count($colorSet)."n+".$i.") > .subjectHeader { background: ".$c."}";
$i++
}
?>
</style>
you can just have a counter variable and add the css as a style attribute.
saves you having to create a style tag for the css rules.
<?php
$colourSet = array(
'#1abc9c',
'#2ecc71',
'#3498db',
'#9b59b6',
'#34495e',
'#f1c40f',
'#e67e22',
'#e74c3c',
'#ecf0f1',
'#95a5a6'
);
$numColours = count($colourSet);
$colourInd = 0;
foreach ($userSubjects as $subject => $info) {
?>
<div class="subject paper">
<div class="subjectHeader" style="background: <?php echo $colourSet[$colourInd]; ?>">
<?php echo $info['subject_name']; ?>
</div>
<div class="subjectContent">
</div>
</div>
<?php
$colourInd = ($colourInd + 1) % $numColours;
}
?>
as this doesn't use nth-child it's backwards compatible with older browsers.
but considering it's only like older than IE8 that doesn't support that, it's not worth worrying about that really.
unless you're planning on dynamically loading these colours, then it's probably better to just define them as css rules like #Aldry-Wijaya suggested
The best way (IMO) would be to use CSS classes to represent the colours. Start by defining your CSS styles:
<style type="text/css">
<?php
foreach($colorSet as $key => $color) {
echo '.color' . $key . ' .subjectHeader { color: "' . $color . '"; }' . PHP_EOL;
}
?>
</style>
Then when you output your divs, output a color style with it:
$i = 0;
foreach ($userSubjects as $subject => $info) :
if(!isset($colorSet[$i]))
$i = 0; // reset to the start of the array if you reach the end
?>
<div class='subject paper color<?=$colorSet[$i++]?>'>
<div class='subjectHeader'>
<?=$info['subject_name']?>
</div>
<div class='subjectContent'>
</div>
</div>
<?php
endif;

Categories