Why does the br html tag is ignored in the browser?
<p>
<?php
$footer_1 = the_field('footer_1');
$footer_2 = the_field('footer_2');
$footer_3 = the_field('footer_3');
if (!empty($footer_1)) {
the_field('footer_1');
echo "<br />";
}
if (!empty($footer_2)) {
the_field('footer_2');
echo "<br />";
}
if (!empty($footer_3)) {
the_field('footer_3');
}
?>
</p>
Edit: The browser code outputs the p element as one piece of text. No br tag displayed there either. The three variables are text fields from Advanced Custom Fields.
the_field is used to actually echo out the custom field data, so you cannot assign it to a variable. Use get_field instead, like so:
<p>
<?php
$footer_1 = get_field('footer_1');
$footer_2 = get_field('footer_2');
$footer_3 = get_field('footer_3');
if (!empty($footer_1)) {
echo $footer_1 . '<br>';
}
if (!empty($footer_2)) {
echo $footer_2 . '<br>';
}
if (!empty($footer_3)) {
echo $footer_3;
}
?>
</p>
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 am trying to update a HTML DIV dynamically while updating some information in a MySQL database, without reloading the page. How do I get this HTML DIV to update?
echo "<td id=\"trade_refs_update\">".(($results[$k]["trade_references_supplied"] == "Yes") ? "<img src=\"images/checkmark.png\" width=\"20\" height=\"20\" />" : "")."</td>";
Here's the PHP code:
if(isset($_REQUEST["edit_app"]))
{
$columnname=$_REQUEST["column"];
$value=$_REQUEST["editval"];
if ($set_trade_references == 'Yes') {
$sql_update=('UPDATE apps_data set `'.$columnname.'` = "'.$value.'", "trade_refs" = "Yes" WHERE app_id='.$app_id.';');
echo "<script>$('#\"trade_refs_update\"').html('\"<img src=\"images/checkmark.png\" width=\"20\" height=\"20\" />\"');</script>";
} else {
$sql_update=('UPDATE apps_data set `'.$columnname.'` = "'.$value.'" WHERE app_id='.$app_id.';');
echo "<script>$('#\"trade_refs_update\"').html('\"<img src=\"images/error.png\" width=\"20\" height=\"20\" />\"');</script>";
}
$results = $db_handle->executeQuery($sql_update);
if ($DEBUG =='On'){
echo "columnname: ".$columnname."<BR>";
echo "value: ".$value."<BR>";
echo "sql_update: ".$sql_update."<BR>";
}
}
I think you're searching for Ajax! :)
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 this code.
<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("juliver", $con);
$result = mysql_query("SELECT * FROM hi");
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
mysql_close($con);
?>
</body>
</html>
the above code works.
now, I want to insert this
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
into the specified div or other element in the html, example, i will insert this
echo '<img src="'.$row['name'].'" />';
into the html element .
I dont know how to do this, please help me. Thanks
Juliver
if yo want to place in an div like
i have same work and i do it like
<div id="content>
<?php
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
?>
</div>
The only things I can think of are
including files
replacing elements within files using preg_match_all
using assigned variables
I have recently been using str_replace and setting text in the HTML portion like so
{{TEXT_TO_REPLACE}}
using file_get_contents() you can grab html data and then organise it how you like.
here is a demo
myReplacementCodeFunction(){
$text = '<img src="'.$row['name'].'" />';
$text .= "<div>".$row['name']."</div>";
$text .= "<div>".$row['title']."</div>";
$text .= "<div>".$row['description']."</div>";
$text .= "<div>".$row['link']."</div>";
$text .= "<br />";
return $text;
}
$htmlContents = file_get_contents("myhtmlfile.html");
$htmlContents = str_replace("{{TEXT_TO_REPLACE}}", myReplacementCodeFunction(), $htmlContents);
echo $htmlContents;
and now a demo html file:
<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
{{TEXT_TO_REPLACE}}
</body>
</html>
You can write the php code in another file and include it in the proper place where you want it.
AJAX is also used to display HTML content that is formed by PHP into a specified HTML tag.
Using jQuery:
$.ajax({url: "test.php"}).done(function( html ) {
$("#results").append(html);
});
Above code will execute test.php and result will be displayed in the element with id results.
There is no way that you can do it in PHP when HTML is already generated. What you can do is to use JavaScript or jQuery:
document.getElementById('//ID//').innerHTML="HTML CODE";
If you have to do it when your URI changes you can get the URI and then split it and then insert the HTML in script dynamically:
var url = document.URL;
// to get url and then use split() to check the parameter
refer to the basic.
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
there is no way to specifically target an element with php, you can either embed the php code between a div tag or use jquery which would be longer.
You can repeat it by fetching again the data
while($row = mysql_fetch_assoc($result)){
//another html element
<div>$row['name']</div>
<div>$row['title']</div>
//and so on
}
or you need to put it on the variable and call display it again on other html element
$name = $row['name'];
$title = $row['title']
//and so on
then put it on the other element, but if you want to call all the data of each id, you need to do the first code
Have you tried this?:
$string = '';
while($row = mysql_fetch_array($result))
{
//this will combine all the results into one string
$string .= '<img src="'.$row['name'].'" />
<div>'.$row['name'].'</div>
<div>'.$row['title'].'</div>
<div>'.$row['description'].'</div>
<div>'.$row['link'].'</div><br />';
//or this will add the individual result in an array
/*
$yourHtml[] = $row;
*/
}
then you echo the $tring to the place you want it to be
<div id="place_here">
<?php echo $string; ?>
<?php
//or
/*
echo '<img src="'.$yourHtml[0]['name'].'" />;//change the index, or you just foreach loop it
*/
?>
</div>
Well from your code its clear that $row['name'] is the location of the image on the file, try including the div tag like this
echo '<div>' .$row['name']. '</div>' ;
and do the same for others, let me know if it works because you said that one snippet of your code is giving the desired result so try this and if the div has some class specifier then do this
echo '<div class="whatever_it_is">' . $row['name'] . '</div'> ;
You have to put div with single quotation around it. ' ' the Div must be in the middle of single quotation . i'm gonna clear it with one example :
<?php
echo '<div id="output">'."Identify".'<br>'.'<br>';
echo 'Welcome '."$name";
echo "<br>";
echo 'Web Mail: '."$email";
echo "<br>";
echo 'Department of '."$dep";
echo "<br>";
echo "$maj";
'</div>'
?>
hope be useful.
I use this or similar code to inject PHP messages into a fixed DIV positioned in front of other elements (z-index: 9999) just for convenience at the development stage.
Each PHP message passes into my 'custom_message()' function and is further conveyed into the innard of preformatted DIV created by echoed JS.
There can be as many as it gets, all put inside that fixed DIV, one under the other.
<style>
#php_messages {
position: fixed;
left: 0;
top: 0;
z-index: 9999;
}
.php_message {
background-color: #333;
border: red solid 1px;
color: white;
font-family: "Courier New", Courier, monospace;
margin: 1em;
padding: 1em;
}
</style>
<div id="php_messages"></div>
<?php
function custom_message($output) {
echo
'
<script>
var
el = document.createElement("DIV");
el.classList.add("php_message");
el.innerHTML = \''.$output.'\';
document.getElementById("php_messages").appendChild(el);
</script>
';
}
?>