Dynamically increment width of css element in a for loop - php

EDIT: i found out, itsin my other answer below.
I want to present a graph in php and can create the columns, but when i try to increment the percentage to correspond with the css width of the .inner class it wont increment for each element in he array. How can I increment the width for each element in the array?
$percentage = array(0.2,0.4,0.5,0.6,0.7,0.8);
for($i=0;$i<6;$i++){
echo "<div class='outter'>
<div class='inner'>"; echo $percentage[$i]; echo "
</div>
</div>";
echo "<style type='text/css'>";
echo ".outter{height:25px;width:500%;border-right:solid 1px #000;}";
echo ".inner{height:25px;width:";echo $percentage[i]; echo "%;border-right:solid 1px #000;background-color:#02abff;}";
echo "</style>";
}
i can do it in html and css but i want to do it php. Thanks.

Not sure if you have noticed, but the actual values you have there are 0.X%, and this is not exactly a valid width. You might want to multiply that value by 10 (or by 100) to get to 2% (or 20%).
$percentage = array(0.2,0.4,0.5,0.6,0.7,0.8);
foreach ($percentage as $key => $val) {
echo "<div class='outter'>
<div class='inner'>";
echo $val;
echo "</div>
</div>";
echo "<style type='text/css'>";
echo ".outter{height:25px;width:500%;border-right:solid 1px #000;}";
echo ".inner{height:25px;width:";
echo $val * 10;
echo "%;border-right:solid 1px #000;background-color:#02abff;}";
echo "</style>";
}
I also changed the for loop that you had there to foreach loop, which makes more sense when working with arrays.

I found out. I had to put the styling inside the html tag:
foreach($percentage as $percent){
echo "<div class='outter' style='height:25px;width:500%;border-right:solid 1px #000;'>
<div class='inner' style='height:25px;width:"; echo $percent; echo "%;border-right:solid 1px #000;background-color:#02abff;'>"; echo $percent; echo "
</div>
</div>";
}

Related

Hi, I am new to php and I cant find a way to make alternate table color in my code. It always show the wrong output that is required for me to follow

I did the nth child rule in css but it the colors are not alternate. Colors are all the same each rows and columns. My code is a multiplication table with a supposed alternate color. Im having a hard time knowing what I should do to modify or add in my php code to create the desired output (alternate colors in rows and columns in the multiplication table).
Below is my php code:
<?php
for($row=0; $row<=10; $row++){
echo "<tr>";
for($column=0; $column<=10; $column++){
if($row==0 && $column==0){
echo "<td></td>";
}
else{
echo "<td>". $row * $column. "</td>";
}
}
}
echo "</tr>";
?>
CSS code:
table, th, td {
border: 1px solid black;
}
td {
padding: 15px;
}
.bold{
font-weight: bold;
font-size: 1.3em;
}
table tr:nth-child(odd) {
background-color: yellow;
}
table tr:nth-child(even) {
background-color: red;
}
you forget to use the <table> tag and your CSS address it from the table.
<?php
echo "<table>";
for($row=0; $row<=10; $row++){
echo "<tr>";
for($column=0; $column<=10; $column++){
if($row==0 && $column==0){
echo "<td></td>";
}
else{
echo "<td>". $row * $column. "</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>

CSS for cell padding

I want to make all the rows in the same size, but rows height with data is large. I want to fix this row size for empty rows. Table class name is demo.
Demo
<?php
while($fet=mysql_fetch_assoc($sql1))
{
$id=$fet['c_id'];
$address=$fet['address'];
$chk=$fet["c_name"];
$in=$in+1;
echo "<tr>";
echo "<td style='align:center'><a class='astext' href='client_view.php?cid=".$fet["c_id"]."'>".$fet["c_name"]."</a></td>";
echo "<td style='align:center'><a class='ima' href='client_details.php?cid=".$fet["c_id"]."'><img src='image/edit1.png' alt='edit' style='width:20px; height:20px' title=Edit></a></td><td style='align:center'>
<a class='ima' href='clients.php?del=".$fet["c_id"]."'><img src='image/delete1.png' alt='delete' style='width:20px;height:20px' title=Delete></a></td>";
echo "</tr>";
}
if ($in < 10) {
$empty_rows = 10 - mysql_num_rows($sql1);
for ($m = 0; $m < $empty_rows; $m++) {
echo '<tr><td></td><td></td><td></td></tr>';
}
}
?>
You need to add following CSS in your code.
.demo td{
height:20px;
}
Demo
Add fixed height to tr like
table tr {
height: 40px;
box-sizing: border-box;
}
check fiddle: http://jsfiddle.net/4gqgzdL8/1/
If you need box-sizing you can use it otherwise don't use
Try to set height and line-height for td like this: Demo
.demo td {
line-height:28px;
height:28px;
}

Filepath from SQL database not looping a BG image

I have an Image uploaded to a folder with the filepath stored in a table along with other info (title, description). I would like this info looping inside a repeated div. Both other variables loop correctly, however the filepath variable needs to be used in a background URL and at the moment only echos the value of the last row. Thank you very much for your help, there has got to be a simple solution! -Sean
<?php
$result = mysql_query("SELECT * FROM `program`");
$values = mysql_fetch_array($result);
$globals['filepath'] = $row['filepath'];
echo "<div>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<div class=wrapper3 >
<h2>" . $row['program_name'] . "</h2>
<p>" . $row['program_description'] . "</p>
</div>
<p> </br> </p>";
$globals['bgimage'] = $row['filepath'];
}
echo "</div>";
?>
<style type="text/css">
.wrapper3{
width:85%;
margin:0 auto;
padding:20px;
height:auto;
color:#FFF;
background: url(/SMLC/<?php while ($row = mysql_fetch_array($result)){ echo $globals['bgimage'];} ?>
) no-repeat;
background-size:cover;
color:#000;
height:250px;
text-align:center;
background-color:#fff;
border-radius:6px;
border:1px solid #0FF;
}
</style>
<?php mysql_close();?>
Remove the background from the .wrapper3 in css, and add it to the element as an inline style.
<?php
$result = mysql_query("SELECT * FROM `program`");
echo "<div>"; // start a table tag in the HTML
while ($row = mysql_fetch_array($result)) { //Creates a loop to loop through results
$globals['filepath'] = $row['filepath'];
echo "<div class='wrapper3' style=\"background: url('/SMLC/".$row["filepath"]."');\">
<h2>" . $row['program_name'] . "</h2>
<p>" . $row['program_description'] . "</p>
</div>
<p></br></p>";
}
echo "</div>";
?>
NOTE: Use mysqli functions or PDO instead of mysql functions, since, mysql functions are deprecated.

how to show items in a row wise (i.e 5 items per rows)

i have a to show tags onto my view page from controller. I would like to show 5 tags per row ,currently with my below code its showing 1 tag per row. please help me how to do this?
$query = $this->tagsmodel->fetch_all_tags($postnumbers, $offset);
if (is_array($query))
{
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" id='.$row->tags_id.'>';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
}
}
Currently i am getting output like this
$count=0;
$query = $this->tagsmodel->fetch_all_tags($postnumbers, $offset);
if (is_array($query))
{
foreach ($query as $row)
{
$count++;
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" style='float:left;' id='.$row->tags_id.'>';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
if($count%5==0) {
echo "<div style='clear:both'></div>";
}
}
}
so now it will display 5 divs per row
You can do with using css
.main {
width : 400px;
}
.main .parent{
float : left;
margin-right : 10px;
}
Adjust the width as per you will get 5 tags in row.
echo "<div class='main'>";
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" id='.$row->tags_id.'>';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
}
echo '</div>';
Try to set floating for your div style="float: left;":
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<div class="parent" id='.$row->tags_id.' style="float: left;">';
echo '<a class="tagsbtn" href="" >'.$content.'</a>';
echo '</div>';
}
You can use Kasyx solution but you won't be able to control the number of tags displayed on each row. By default, it will fill your div and break to a new line each time the width of displayed tags will be to large.
Use a counter in the loop to control the exact number of tags displayed. You may need to set a fixed width for your div and/or play with the overflow.
Your DOM should looks like
<div id="main">
<div class="rowdiv">
<div class="parent"><a ... >foo</a></div>
<div class="parent"><a ... >bar</a></div>
...
</div>
and the css properties :
.parent { float : left, ...} .rowdiv{ clear : both, ...}
The only thing you have to do is to use modulo in your loop and create a new "rowdiv" each 5 iteration.
PHP
echo '<ul>';
foreach ($query as $row)
{
$content = substr(strip_tags($row->tags_name), 0, 180);
echo '<li id="'.$row->tags_id.'">';
echo '<a class="tagsbtn" href="#">'.$content.'</a>';
echo '</li>';
}
echo '</ul>';
CSS
ul {style-list-type:none}
ul li {display:block;float:left
ul li:nth-child(5n+1) {clear:left}
Update
This will allow your tag to have dynamic width
New Working Example
jsFiddle

css formatting in php

I have a css question.
I have the following php code which displays a name.
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['ship_name'] . "<BR>";
I'm trying to add some text style to it but I'm not that good in css and I'm somehow lost.
I'm trying to do something like <t> $db_field['ship_name'].<t/> but it gives me an error.
judging by your comment, probably you want
print "<span class='t'>".$db_field['ship_name']."</span><BR>";
and for your CSS file define
.t { font-size: 50pt; color:black; text-shadow: 0 1px 2px white; }
There are 2 ways of doing this. Either you embed html in PHP or write separate HTML snippet. I will show you the both ways :
The first one is already explained above in the answer by sinclairchase.
echo "<td>" . $db_field['ship_name'] . "</td>";
The other way is :
<?php while ($db_field = mysql_fetch_assoc($result)) {
?>
<td>
<?php print $db_field['ship_name'] . "<BR>"; ?>
</td>
<?php } ?>
I dont unsderstand why you write t in question it would be td.
This will echo out the data into a span tag:
echo "<span>" . $db_field['ship_name'] . "</span>";
To add a css class:
echo "<span class=\"class_name\">" . $db_field['ship_name'] . "</span>";
Then in your css file:
span.class_name { font-size:24px; color:#666; }

Categories