I've a PHP file which has numerous echo statements for various checks like this;
if ($power != "1")
{
echo "Please contact administrator for assistance.";
exit;
}
if (!$uid)
{
echo "You do not have permissions to change your status.";
exit;
}
if (!$mybb->input['custom'])
{
echo "You've not added any status to change";
exit;
}
I want to give a similar CSS class to each echo statement. I tried this;
if ($power != "1")
{
echo "<div class='class_name'>Please contact administrator for assistance.</div>";
exit;
}
and it works, but my php file has dozens of echo's and I don't want to edit each and every echo statement. Is there any easy way to achieve this?
You could define a function to handle outputting the message. You'll have to update the existing code but in the future, you'll be able to change the CSS class name or HTML structure easily be modifying the function.
class Response
{
public static function output($message, $className = 'class_name')
{
echo "<div class='" . htmlspecialchars($className) . "'>" . $message. "</div>";
exit;
}
}
Usage:
if ($power != "1")
{
Response::output("Please contact administrator for assistance.");
}
Override the class name:
Response::output("Please contact administrator for assistance.", "other_class");
If you're having issues/errors in above answers then here is my answer, I hope it helps;
Add the following code just above the <?php of your PHP file;
<style type="text/css">
.error{
background: #FFC6C6;
color: #000;
font-size: 13px;
font-family: Tahoma;
border: 1px solid #F58686;
padding: 3px 5px;
}
</style>
Next change each echo statement to something like this;
echo "<div class='error'>Write error code here.</div>";
exit;
You can easily find and replace the echo statements if you're using Notepad++
It should work. Also its somewhat similar to MrCode's answer however I think my answer is easily understandable and may be easy to implement.
There's no there way, unless you define once css class and put all your echo statements inside it.
<div class = "name">
<?php
echo ...
...
...
?>
</div>
Try to change each echo to a fixed variable name:
if ($power != "1")
{
$msg = "Please contact administrator for assistance.";
}
if (!$uid)
{
$msg = "You do not have permissions to change your status.";
}
if (!$mybb->input['custom'])
{
$msg = "You've not added any status to change";
}
Then write a function for giving style to the out put:
function stylizing($msg, $class="")
{
if($class != "")
$msg = "<div class='{$class}'>{$msg}</div>";
echo $msg;
}
Then you can use stylizing($msg, "class_name"); where you want to print out the result.
Do you mean something like this?
$message = '';
if ($power != "1") $message .= "<div class='one'>Please contact administrator for assistance.</div>";
elseif (!$uid) $message .= "<div class='two'>You do not have permissions to change your status.</div>";
elseif (!$mybb->input['custom']) $message .= "<div class='three'>You've not added any status to change.</div>";
echo $message;
Related
i am trying to apply the background color in echo using an inline style but it is not applying background color in however changes only the text color. i want to change background color in a particular part of the code
echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>"
program code is
class db_access
{
private $_uname;
private $_pass;
private $_db;
private $_server;
//_construct connects databaseand fetchest he result
public function __construct($server,$user_name,$password,$d_b)
{
$this->_server=$server;
$this->_pass=$password;
$this->_uname=$user_name;
$this->_db=$d_b;
$con=mysql_connect($this->_server,$this->_uname,$this->_pass);
$db_found=mysql_select_db($this->_db,$con);
if($db_found)
{
$sql="SELECT * FROM login";
$result = mysql_query($sql);
if ($result)
{
while ( $db_field = mysql_fetch_assoc($result) )
{
static $rec_num=1;
//inline css
echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>";
print $db_field['ID'] . "<BR>";
print $db_field['u_name'] . "<BR>";
print $db_field['pass'] . "<BR>";
print $db_field['email'] . "<BR><br><br>";
$rec_num++;
}
//returns the connection name that is used as a resource id in __destruct function
return $this->_con=$con;
}
else {die(mysql_error());}
}
else
{return die(mysql_error());}
}
// destruct function closes database
public function __destruct()
{
$close=mysql_close($this->_con);
if($close)
{print "connection closed";}
else {die(mysql_error());}
}
}
$db=new db_access("127.0.0.1","root","","fabeeno");
//var_dump($db);
Try like
echo "<p style='color:orange;background-color:red;'>record number: ".$rec_num. "</p><br>";
You have to end ' single quote after the background-color style.
try
echo "<div style='background-color:red;'><p style='color:orange'>"."record number: ".$rec_num. "</p></div>"."<br>";
Or you can ajust only the print lines with styles
print "<p style='color: red;border: 1px solid black;height:20px;box-shadow: 1px 1px 7px;'>"."message!: ".$db_field['TEXTAREA1'] . "<br>";
after this i got an fancy box around the text =) i love boxes in many ways
so no echo where placed only print {} function
Many thanx and good luck
ps
['textarea1'] <-- is assigned to database so it reads only that table
I need to add a line break after each row retrieved;
I hav tried adding in every possible way.. No luck..
while ($line = mysql_fetch_array($result))
{
?>
<style>
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
</style>
<?php
$sender=$line["sender"];
$classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
$q=mysql_query("select * from users where u_id='$sender'");
$row=mysql_fetch_array($q);
$receiver=$row['firstname'];
//if($receiver!= $line["receiver"])
$msg = $msg . "<tr class='$classy'>" .
"<td>" . $receiver . ": </td>" .
"<td>" . $line["msg"] . "</td>"."<td class='date'>" . $line["chattime"] . "</td></tr>";
}
$msg=$msg . "</table>";
echo $msg;
I need to add a break after each sender+msg+time.//like in a usual chat.
Your problem come from float declarations. They are useless in your code :
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
Remove them and it will work as expected.
You don't need them in your code if you use <table> and <tr>. Default behaviour of <tr> is to create a new "line" each time you add it, but with adding float you're breaking this behaviour.
Also, follow #bulforce recommendation to move your CSS before loop : your code add it as many time you have messages in your document (if you have 10 messages, css declarations will be added 10 times).
<style type="text/css">
.whiteBackground { background-color:#F5ECCE; }
.grayBackground { background-color:#CED8F6; }
.date { font-size:10px; color:#627d79; }
</style>
<?php
while ($line = mysql_fetch_array($result))
{
// ...
}
?>
Just try to add a <tr><td colspan="3"> </td></tr> after </tr> inside the while loop
First consider moving the css declaration before the loop, there is no point to have it in the loop body.
Second the second query in the loop should also be moved before the loop and rework this a bit.
Now to your question, one option would be to change the table with divs and spans.. something like so:
<div class="message-row">
<span class="message-user">Name</span>
<span class="message-text">Message</span>
</div>
Then style as you did before, and add margin-bottom: 15px; to the message-row definition.
before while
$msg="<table>";
Inside while
$msg.= "<tr class='$classy'>" in place of $msg = $msg . "<tr class='$classy'>"
Will work for you.
I solved it by replacing table tags with div tags.
Thnku #bulforce; now I will completely follow ur recommendations.
$msg="<div>";
$date_temp="nil";
while ($line = mysql_fetch_array($result))
{
?>
<style>
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
</style>
<?php
$sender=$line["sender"];
$classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
$q=mysql_query("select * from users where u_id='$sender'");
$row=mysql_fetch_array($q);
$receiver=$row['firstname'];
//if($receiver!= $line["receiver"])
/*if($date_temp!=$line["chatdate"]){
$date_temp=$line["chatdate"];
$msg=$msg.$line["chatdate"];
}*/
$msg = $msg . "<div class='$classy'>" . $receiver.": " . $line["msg"] ."<div class='date'>" . $line["chattime"] . "</div></div></br></br>";
}
$msg=$msg . "</div>";
echo $msg;
i am trying to apply the background color in echo using an inline style but it is not applying background color in however changes only the text color. i want to change background color in a particular part of the code
echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>"
program code is
class db_access
{
private $_uname;
private $_pass;
private $_db;
private $_server;
//_construct connects databaseand fetchest he result
public function __construct($server,$user_name,$password,$d_b)
{
$this->_server=$server;
$this->_pass=$password;
$this->_uname=$user_name;
$this->_db=$d_b;
$con=mysql_connect($this->_server,$this->_uname,$this->_pass);
$db_found=mysql_select_db($this->_db,$con);
if($db_found)
{
$sql="SELECT * FROM login";
$result = mysql_query($sql);
if ($result)
{
while ( $db_field = mysql_fetch_assoc($result) )
{
static $rec_num=1;
//inline css
echo "<p style='color:orange';background-color:red;>"."record number: ".$rec_num. "</p>"."<br>";
print $db_field['ID'] . "<BR>";
print $db_field['u_name'] . "<BR>";
print $db_field['pass'] . "<BR>";
print $db_field['email'] . "<BR><br><br>";
$rec_num++;
}
//returns the connection name that is used as a resource id in __destruct function
return $this->_con=$con;
}
else {die(mysql_error());}
}
else
{return die(mysql_error());}
}
// destruct function closes database
public function __destruct()
{
$close=mysql_close($this->_con);
if($close)
{print "connection closed";}
else {die(mysql_error());}
}
}
$db=new db_access("127.0.0.1","root","","fabeeno");
//var_dump($db);
Try like
echo "<p style='color:orange;background-color:red;'>record number: ".$rec_num. "</p><br>";
You have to end ' single quote after the background-color style.
try
echo "<div style='background-color:red;'><p style='color:orange'>"."record number: ".$rec_num. "</p></div>"."<br>";
Or you can ajust only the print lines with styles
print "<p style='color: red;border: 1px solid black;height:20px;box-shadow: 1px 1px 7px;'>"."message!: ".$db_field['TEXTAREA1'] . "<br>";
after this i got an fancy box around the text =) i love boxes in many ways
so no echo where placed only print {} function
Many thanx and good luck
ps
['textarea1'] <-- is assigned to database so it reads only that table
I've been working on this list that will provide information from an xml file. The markup I'm working with is as follows:
<?php
$xml_file = '/path';
$xml = simplexml_load_file($xml_file);
foreach($xml->Continent as $continent) {
echo "<div class='continent'>".$continent['Name']."<span class='status'>".$continent['Status']."</span>";
foreach($continent->Country as $country) {
echo "<div class='country'>".$country['Name']."<span class='status'>".$country['Status']."</span>";
foreach($country->City as $city) {
echo "<div class='city'>".$city['Name']."<span class='status'>".$city['Status']."</span></div>";
}
echo "</div>"; // close Country div
}
echo "</div>"; // close Continent div
}
?>
What I'm trying to accomplish is changing the 'Status'. Regardless of the element, the 'Status' only has two values and will always be either "Red" or "Blue". What I'm trying to do is go through the whole document, find the "Status" attribute, see if the value is "Red", then display an icon or some text, but if it's blue, display nothing. I've come across different examples, but none that show this type of generality. Most of the examples were for locating a specific node and then displaying it's attributes. I'm also trying to avoid, if possible, having to create this rule within each loop, but instead applying it to the whole document (if this seems like the best method.)
Use css selectors instead of javascript. That will make your code lighter and simple!
For example:
<?php
$xml_file = '/path';
$xml = simplexml_load_file($xml_file);
foreach($xml->Continent as $continent) {
echo "<div class='continent'>".$continent['Name']."<span class='status ". cssForStatus($continent['Status'])."'></span>";
foreach($continent->Country as $country) {
echo "<div class='country'>".$country['Name']."<span class='status ". cssForStatus($country['Status'])."'></span>";
foreach($country->City as $city) {
echo "<div class='city'>".$city['Name']."<span class='status ". cssForStatus($city['Status'])."'></span></div>";
}
echo "</div>"; // close Country div
}
echo "</div>"; // close Continent div
}
function cssForStatus($status='')
{
switch ($status) {
case 'red':
$css = "red";
break;
default:
$css = "blue";
break;
}
return $css;
}
while your css can be:
span.status {
height: 10px;
width: 10px;
}
span.status.red {
background-color: "red";
}
span.status.blue {
background-color: "blue";
}
we are using the concept of multiple css classes
Just change your code slightly to add status as your div class.
Example:
echo "<div class='continent status-{$continent['Status']}'>{$continent['Name']}</div>";
This will output (in case of a red continent named Europe)
<div class='continent status-red'>Europe</div>
then add this CSS to your html:
.status-red {
background-color: red;
}
.status-blue {
background-color: blue;
}
Or somehting along these lines
$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
$dellink="Delete";
$editlink="Edit";
$admin = "MJH";
if(($instby == $username)||($instby == $admin))
{
echo "<div id=\"editcont\">$editlink $dellink</div>";
}
else if($inst == $username)
{
echo "<div id=\"editcont\">$editlink <font face=\"Trebuchet MS, Arial, Helvetica, sans-serif\" size=\"2\">Delete</font></div>"
}
else
{
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
.in my code above what i want to do is to show a containing links depending on who is logged in.. however, when the admin logs in he will be able to see the containing $editlink and $dellink.
.I can't seem to find where exactly have i gone wrong.. please help me with this guys! TIA! More Power!
.this is what my code looks like.
if(($instby == $username)||($username == $admin))
$admin = "MJH";
if(($instby == $username)||($instby == $admin)) {
if($inst == $username) {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcont\">$editlink <font>Delete</font></div>";
}
else {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
}
Thats how i percieved you wanted it, but really, it is very hard to understand whats going on here. Are $inst and $instby meant to be different variables? Also, out of curiousity, why have you double bracketed your if(); statements?
Hope I deciphered it correctly.
PS. I don't think echo();ing html is good practice.