PHP script printing div - php

Is there a possible way to multiply code and print it out using PHP?
I am trying to make a script that checks users and draws a line depending on count.
CSS Code
#outer { // gray backround - always on.
background-color: #401800;
width: 150px;
height: 6px;
}
.inner { // used if there's less than 100 ppl online
width: 1px;
height: 6px;
display:block;
float:left;
}
.bigger { // used if 100+ people are online
width: 10px;
height: 6px;
display:block;
float:left;
}
}
.bigger.trys { // color for small px
background-color: green;
}
.inner.vienas { //color for big px
background-color: green;
}
PHP code ($rows = counted users):
if {$rows <= "0"
echo"<span class="inner vienas"></span>"; //draws small 1px width bar
}
elseif{$rows >= "10"
echo"<span class="inner vienas"></span><span class="inner vienas"> </span>"; // count =10 or greater - draws x2 bars.
}
elseif{$rows >= "100" // if users are 100 OR more counts new variable
$padala = $rows/10 //variable is users/10
echo 10*"<span class="bigger trys"></span>") //i want to print this out as 1px each 10 users
}
else {
echo "Script error";
}
Is there a way to do that?

Basicly you have lot of syntax error.
First, if statement need a condition:
Wrong:
if {
$rows <= "0"
echo"<span class="inner vienas"></span>"; //draws small 1px width bar
}
Correct (i don't know if $row have an integer or string, but if you use numbers, the correct is to storage an integer):
if ($rows <= 0) {
echo '<span class="inner vienas"></span>';
}
Second, echo:
You have an error with double quote marks when inserting HTML code, you don't have to cut the echo
Wrong:
echo"<span class="inner vienas"></span>";
Correct (using simple quotes):
echo '<span class="inner vienas"></span>';
Another error is that echo*10, you need to use a for loop.
for($i=0; $i<=9; $i++) {
echo '<span class="bigger trys"></span>';
}
Btw, I don't understand what you want, but the correct maybe is this:
if ($rows <= 0) {
echo '<span class="inner vienas"></span>';
} else if ($rows >= 10) {
echo '<span class="inner vienas"></span><span class="inner vienas"> </span>';
} else if ($rows >= 100) {
$padala = $rows / 10
for($i=0; $i<=9; $i++) {
echo '<span class="bigger trys"></span>';
}
} else {
echo "Script error";
}
Here you can read more about if statements and for:
PHP Manual - If Statements
PHP W3Schools - For loops

Without knowing more about your data, I'm not sure how I would go about doing what you want to accomplish. However, Here is your above code with the syntax fixes.
The conditions in the if statements should be wrapped in Parentheses, and the double quotes in the strings after the the echo functions should be escaped
if ($rows <= "0") {
echo"<span class=\"inner vienas\"></span>"; //draws small 1px width bar
} elseif($rows >= "10") {
echo"<span class=\"inner vienas\"></span><span class=\"inner vienas\"> </span>"; // count =10 or greater - draws x2 bars.
} elseif($rows >= "100") { // if users are 100 OR more counts new variable
$padala = $rows/10 //variable is users/10
echo 10*"<span class=\"bigger trys\"></span>") //i want to print this out as 1px each 10 users
} else {
echo "Script error";
}

Related

Create multiple strings based on elseif condition in php

While this code seems to work to change $tdstyle as anticipated on the td line, it does not update $font and $text appropriately based on the esleif conditions. It always seems to keep these strings the same based on the else condition alone even if it isn't true. Maybe just a syntax issue? I am certain there is a much better way to do this, but my limited knowledge has brought me to this point. Any suggestions?
<?php
$num = (float)$uvindex;
if($num >10) {
$tdstyle='#B567A4';
$font='white';
$text='Extreme';
} elseif($num >=8) {
$tdstyle='#E53210';
$font='white';
$text='Very High';
} elseif($num >=6) {
$tdstyle='#F18B00';
$font='black';
$text='High';
} elseif($num >=3) {
$tdstyle='#FFF300';
$font='black';
$text='Moderate';
} else $tdstyle='#3EA72D'; $font='black'; $text='Low';
?>
<td height="82" colspan="2" align="center" valign="middle" class="data1" style="text-align:center; background-color:<?php echo $tdstyle ?>; color:<?php echo $font ?>; border: 3px solid black; border-radius: 7px; font-size:12px;">
Your else is only containing the first statement (where you set $tdstyle).
This
// ...
} else $tdstyle='#3EA72D'; $font='black'; $text='Low';
is equivalent to
// ...
} else {
$tdstyle='#3EA72D';
}
$font='black'; // you probably want these in the
$text='Low'; // above else block
So $font and $text will always be 'black' and 'Low' respectively. Remember that ; terminates a statement, not a newline. Listen to the comments of Nigel: invest in a formatter to help you catch errors like these.

How to retrieve specific values from array with multiple PHP for loops?

I have been working on trying to retrieve specific results from a PHP for loop without success.
My objective is to run a loop on an array and to pull the specific value each time if it exists within the array. I am able to run the first loop and retrieve a value, however, when I run the second for function it retrieves the exact same number. I haven't been able to figure out why. E.g. The first loop returns the value of 6, but so does the second loop. I am trying to retrieve 6 if it exists from the first loop and 7 if it exists in the second loop. 7 is located in the array, so it should return 6 in the first for statement and 7 on the second.
Here is what I have done so far:
for ($i = 0; $i < count($final_data); $i++) {
$check_sector = $final_data[$i]['sector'];
$check_image = $final_data[$i]['image'];
if($final_data[$i]['sector'] == 6){
echo "<div id='6' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
. "<div id='ss6' style='position: absolute; width: 100px; height: 100px; background-image: url(" . $check_image . ");'></div>"
."<div id='s6' class='overlay' ></div></div>";
break;
}else{
echo "<div id='6' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
."<div id='s6' class='overlay' ></div></div>";
break;
}
}
for ($i = 0; $i < count($final_data); $i++) {
$check_sector = $final_data[$i]['sector'];
$check_image = $final_data[$i]['image'];
if($final_data[$i]['sector'] == 7){
echo "<div id='7' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
. "<div id='ss7' style='position: absolute; width: 100px; height: 100px; background-image: url(" . $check_image . ");'></div>"
."<div id='s7' class='overlay' ></div></div>";
break;
}else{
echo "<div id='7' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
."<div id='s7' class='overlay' ></div></div>";
break;
}
}
When you get to the second foreach loop, it’s hitting the first element in the array first.
So it’s saying is 6==7? No, so it goes to the else echo statement.
Then you have a break so it gives up and never checks the second element in the array which is the 7.

div and class not showing up

I'm testing a barcode on my website but for some reason the div with the class="b128" which stands for the barcodes is not showing up. If I test the same source code on another website it does show up but with different styling.
Does anyone know how I can make this source code show up and keep the original styling at the same time?
<style>
div.b128{
border-left: 1px black solid;
height: 60px;
}
</style>
<?php
global $char128asc,$char128charWidth;
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
$char128wid = array(
'212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9
'221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19
'221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29
'212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39
'231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49
'231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59
'314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69
'112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79
'111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89
'214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
'114131','311141','411131','211412','211214','211232','23311120' ); // 100-106
////Define Function
function bar128($text) { // Part 1, make list of widths
global $char128asc,$char128wid;
$w = $char128wid[$sum = 104]; // START symbol
$onChar=1;
for($x=0;$x<strlen($text);$x++) // GO THRU TEXT GET LETTERS
if (!( ($pos = strpos($char128asc,$text[$x])) === false )){ // SKIP NOT FOUND CHARS
$w.= $char128wid[$pos];
$sum += $onChar++ * $pos;
}
$w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
//Part 2, Write rows
$html="<table cellpadding=0 cellspacing=0><tr>";
for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
$html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div>";
return "$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=2><b>$text</table>";
}
?>
<?php
$test = "12345";
echo '<div style="border:3px double #ababab; padding:5px;margin:5px auto;width:135px;">';
echo bar128(stripslashes($test));
echo '</div>';
?>
You aren't closing most of your tags. Try this:
$html = "<table cellpadding=0 cellspacing=0><tr>";
for ($x = 0; $x < strlen($w); $x += 2) // code 128 widths: black border, then white space
$html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>";
return "$html</tr><tr><td colspan=" . strlen($w) . " align=center><font family=arial size=2><b>$text</b></font></td></tr></table>";

php query - Background color changes

What I want is to change the color of a cell in a table based on the value that is returned by the query.
what I have done is this --
In the style
.priority_1, priority_-1, priority_0{
background-color: green;
color:green;
}
.priority_4, .priority_5, .priority_6, .priority_7, .priority_-4, .priority_-5, .priority_-6, .priority_-7{
background-color: red;
color:red;
}
and in the body - cell
<?php
$result = mysqli_query($con,"SELECT SHOP, FORMAT(VARMP,0) AS value FROM recordstable WHERE SHOP='1' AND Month='1' AND Type='TCheck'");
while($row = mysqli_fetch_array($result)) {
$priority = $row['value'];
echo "<td class=\"priority_{$priority}\"><center>";
echo $priority . "";
}
?>
</td>
this gives me what i want however, what if the value falls outside the range - if i get a value of 43 I want it red. but the .priority will only chnage to red for 7 to -7. how can i do a range easy easy style. without doing priority 1 - 100 plus an minus.
No need to create that many classes. If I've understood correctly, you probably need something like:
CSS:
.priority_green{
background-color: green;
color:green;
}
.priority_red{
background-color: red;
color:red;
}
PHP:
while($row = mysqli_fetch_array($result)) {
$priority = $row['value'];
$class = 'green';
if($priority >= -7 and $priority <= 7){ $class = 'red'; } //from 7 to -7 only
echo "<td class=\"priority_$class\"><center>";
echo $priority . "";
//don't forget to close <center> and <td> and you also don't have rows - just a note
}

excluding a row from a mysql database outputting

Every third element extracted from the database will output in the large box, while every other element will output in the small box. What I need it to do is exclude the third element when the small box is outputted. Any ideas on how to accomplish this?
while($array = mysql_fetch_assoc($result)) {
if($i % 3 == 0) {
// large box
echo '<div class="box" style="width: 692px; height: 218px">' . $array['feedName'] . '</div>';
}
// small box
echo '<div class="box" style="width: 246px; height: 218px">' . $array['feedName'] . "<br></div>";
// exclude the third element
$i++;
}
}
If I understand what you want correctly (each third item is in the large box and kept out of the small box), you just use an else clause in your if.
while($array = mysql_fetch_assoc($result)) {
if($i % 3 == 0) {
// large box
echo '<div class="box" style="width: 692px; height: 218px">' . $array['feedName'] . '</div>';
}
else {
// small box
echo '<div class="box" style="width: 246px; height: 218px">' . $array['feedName'] . "<br></div>";
}
$i++;
}
divide by 6 and get the remainder (%)
if (remainder == 0 or 3) {
large box
} else if (remainder == 1 or 5) {
small box
}

Categories