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
Related
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;
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 have created a form that is handled by some PHP and echoed so it can be printed by the user. My problem is that when it prints it is printing everything. I would like to only print the text and not the print button or the url. I'll post the PHP code below.
<?php
$beq = $_REQUEST["beq"];
$blq = $_REQUEST["blq"];
$bwq = $_REQUEST["bwq"];
if ($beq>0)
{
echo ("Berry (SPR0110) " .$beq);
}
if ($blq>0)
{
echo ("<br />Black (SPA0212) " .$blq);
}
echo '<br /><br />Print';
?>
Quickest way:
<style type="text/css">
#media print {
.hide-on-print { display:none; }
}
</style>
<?
$beq = $_REQUEST["beq"];
$blq = $_REQUEST["blq"];
$bwq = $_REQUEST["bwq"];
if ($beq>0)
{
echo ("Berry (SPR0110) " .$beq);
}
if ($blq>0)
{
echo ("<br />Black (SPA0212) " .$blq);
}
echo '<div class="hide-on-print"><br /><br />Print</div>';
create a stylesheet with screen and print rules
Good rules for setting up print css?
this is by Far the weirdest thing i have ever seen and i am completely confused. please someone help me with this.
$variable=array();
$count=0;
// now im am going to loop through a resource that i made
while(!feof($job))
{
$data=fgets($job);
// i am search for different things below. search for name, date, employer
// i am using regex to search btw
// presume object in class works fine, and they do.
if(search for eg name in $data, storing in $variable[$count].first($match))
// the problem is at this point i will have access to
// $variable[$count].getFirst(returns value set by first) which was set above;
if(search for eg Employer in $data, storing in variable[$count].next($match))
// i will have access here as well
// $variable[$count].getFirst(returns value set by first) which was set above
if(search for 3rd search in $data, storing in variable[$count].name($match))
// down here after the second if i am not able to see any of my variables set more than 2 if statements ago????
// $variable[$count].getFirst(does not returns the value set by first()) which was set above
if(search for 4th search in $data, storing in variable[$count].foo($match))
// check if everything is set then count++;
}
Now each one of these methods are completely dependent from the next but after 2 if statements. I am just not able to access $variable[count]->getfirst()
the answer is null;
edited
this is the actual code
require "functions/decodeEncodedUrl.php";
require "objects/jobObject.php";
$url=decodeEncodedUrl();
$profile=array();
$companies=0;
$url_search='http://www.jobbank.gc.ca/';
$startReading=0;
$job=fopen($url['url'], 'r')or die("JobBanks is failing to respond.<br>Please Try again Later");
while(!feof($job))
{
set_time_limit(500);
$profile[$companies]= new jobProfile();
$trash=fgets($job);
if(!$startReading)
{
if(preg_match('~RepeaterSearchResults_hypJobItem_[0-9]+~',$trash,$matches))
{
$startReading=true;
}
}
if($startReading)
{
$data=$trash;
if(preg_match("~href=\".*\"~",$data,$matches))
{
$temp=preg_replace("~href=~",'',$matches[0]);
$temp=preg_replace("~\"~",'',$temp);
$profile[$companies]->setLink($url_search.$temp);
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~>[A-Za-z-, ]+\(~",$data,$matches))
{
$temp=preg_replace("~>|\(~",'',$matches[0]);
$profile[$companies]->setPosition(ucfirst($temp));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~# *[0-9]+~",$data,$matches))
{
$profile[$companies]->setOrderNum(preg_replace("~#| ~",'',$matches[0]));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~Employer:</strong>.*~",$data,$matches))
{
$temp=preg_replace("~Employer:</strong> ~",'',$matches[0]);
$temp=preg_replace("~<br.*~",'',$temp);
$temp=ucfirst($temp);
$profile[$companies]->setEmployer($temp);
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~[$][0-9]+.*~",$data,$matches))
{
$temp=preg_replace("~/.*~",'',$matches[0]);
$profile[$companies]->setSalary(preg_replace("~[$]~","$ ",$temp));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~[$][0-9]+.*~",$data,$matches))
{
$temp=preg_replace('~[$A-Za-z0-9. ]*[/] ?~','',$matches[0]);
$profile[$companies]->setRate(preg_replace('~<.*~','',$temp));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~Location:.*~",$data,$matches))
{
$temp=preg_replace('~.*;~','',$matches[0]);
$temp=preg_replace('~^ |,~','',$temp);
$profile[$companies]->setCity(ucfirst($temp));
//echo ucfirst($temp)."<br>";
}
if(preg_match("~Location[:<>/\,A-Za-z ]*~",$data,$matches))
{
$profile[$companies]->setProvince($matches[0]);
//echo " ".$matches[0]."<br>\n";
//echo $profile[$companies]->getLocation()."\n<br>";
}
if(preg_match("~[0-9]{4}/[0-9]{2}/[0-9]{1,2}~",$data,$matches))
{
echo $profile[$companies]->displayHTML();
$profile[$companies]->setDate($matches[0]);
if($profile[$companies]->allDataSet())
{
//echo "data was set"."<br>";
$startReading=false;
$companies++;
}
else
{
$startReading=false;
$companies++;
echo "Data was Not set";
}
}
}
}
fclose($job);
everything works except the $profile[number] doesn't store anything in it at all after the 3 rd if statement when the variable is stored.
If
{
//Profile[number] info stored
}
if
{
//Profile[number] info available
}
if
{
//profile[number] info available
}
if
{
//profile[number] info is gone
}
variable[$count].next($match)
the .next() moves the internal pointer to the next element in the array.
I have referred to similar questions like this and have done exactly the same way as it should have been but no success. Hence I would appreciate if some one can help me with this
I have a file called view.php which calls a function from a class based on a switch case condition. This function displays the output on the page with few links. When clicked on the link it calls another function which sits in my first function. But obviously when I click my link, nothing happens. That's my code.
view.php
require_once(..'/functions.php');
$functions = new myfile_functions();
<form method="post" action="view.php"><div>
<p><select name="potentialareas" id="potentialareas">
<option style="font-weight:bold;" value="-1">Select an area to view </option>
<?php
foreach($accessareas as $accessarea){
echo "<option value='".$accessarea->id."'>".$accessarea->name. " - " . $accessarea->reporttype. "</option>";
}
?>
</select>
</p>
<p><input name="view" id="view" type="submit" value="View" title="view" /><br /></p>
</div></form>
<div style="border-top:1px dotted #ccc;"></div>
<?php
if(isset($_POST['view']))
{
$hierarchyid = $_POST['potentialareas'];
$reporttype = $DB->get_record_sql("some query");
switch($reporttype->reporttype)
{
case "D Report":
$functions->getDepartmentReport($hierarchyid);
break;
case "S Report":
$functions->getSectionReport($hierarchyid);
break;
case "A Report":
$functions->getAreaReport($hierarchyid);
break;
}
}
functions.php
class myfile_functions(){
function getDepartmentReport($departmentid)
{
global $USER, $CFG, $DB;
$dname = $DB->get_record_sql("some query");
$output = "<div id='actualreport'><p><b>" . $dname->name. " Department Report</b></p>";
$output .= "<p>Activities started: </p>";
$output .= "<p>Activities Completed: </p></div>";
$output .= "<div id='separator' style='border-top:1px dotted #ccc;'></div>";
$output .= "<div id='listofsections'><p><b><i>Select the following for a more detailed report.</i></b></p>";
$snames = $DB->get_records_sql('some query');
foreach($snames as $sname)
{$output .= "<p>" .$sname->name. " <a href='view.php?section=" .$sname->id. "' name='section'><i>view report</i></a></p>";
}
$output .= "</div>";
if(isset($_GET['section']))
{
$this->getSectionReport($_GET['section']);
}
echo $output;
}
function getSectionReport($sectionid)
{
global $USER, $CFG, $DB;
$sname = $DB->get_record_sql("some query");
$output = "<div id='actualreport'><p><b>" . $sname->name. " Report</b></p>";
$output .= "<p>Num Users: </p>";
$output .= "<p>Activities Completed: </p></div>";
$output .= "<div id='separator' style='border-top:1px dotted #ccc;'></div>";
$output .= "<div id='listofareas'><p><b><i>Select the following for a more detailed report.</i></b></p>";
$anames = $DB->get_records_sql('some query');
foreach($anames as $aname)
{$output .= "<p>" .$aname->name. " <a href='view.php?area=" .$aname->id. "' name='view' id='view'><i>view report</i></a></p>";
}
$output .= "</div>";
if(isset($_GET['area']))
{
$areaid = $_GET['area'];
$this->getAreaReport($areaid);
}
echo $output;
}
Similarly another function calling the above function, n so on.
function getAreaReport($id)
{ .. same content but calling another function...}
So when ever I click my view report link, I get the id appended id in my querystring, something like
http://mydomain.com/view.php?section=5
Ideally the contents of getSectionReport() should get printed but its not. Please point out what is it that I am doing wrong.
Thanks in advance.
What's your main method for displaying everything? At the moment you have three functions, all of which link to eachother in various ways, but it doesn't look as if you're instantiating anything first. The PHP is being fed a parameter in your URL, sure, but if a class isn't instantiated and a method declared, how does it know what you want it to do?
For myfile.php, maybe you should do something like:
class MyFile
{
public function other_function()
{
// Various stuff
return 'stuff';
}
public function other_other_function()
{
// Various other stuff
return 'other stuff';
}
public function page_view($file_id)
{
$var = $this->other_function($file_id);
return $this->other_other_function($var);
}
}
$class = new MyFile;
echo $class->page_view($_GET['id']);
If the two functions are part of a class (as your comment above hints), then the call should be written as
$this->getUserReport($id);
If you're accessing a sibling function inside the same class, use:
class ClassName
{
public function sibling_function()
{
return 'Hey bro!';
}
public function my_function()
{
return $this->sibling_function();
}
}
If not, use the standard:
public function my_function()
{
return sibling_function();
}
If you're still having trouble, make sure your class is properly instantiated. There's no point calling another function if you're not even instantiating the class first:
$obj = new ClassName;
echo $obj->my_function();