Change text output position of PHP - php

I change a variable to $result and then do echo $result, the result gets displayed at the very first line of the site, is this display position somehow changeable? Like, printing the output of echo $result into a little box at the bottom
if (isset($_POST['test']))
{
$result = "itwerks";
echo $result;
}
HTML Part:
<form method="post">
<button name="test">-> Test! <-</button>
</form>

As noted in the comments...
Move your PHP down so that it's not at the top of the page.
You can display PHP anywhere on the page.
<?php $var = 'This is some text, it is in a PHP variable'; ?>
<body>
<div>
<p>
This is some text, not in PHP
</p>
<p>
<?php echo $var //display some PHP here?>
</p>
</div>
</body>

You can do something like this:
$result = '<div style="position:absolute; bottom:2px;">itwerks</div>';

You have to create a HTML-Tag on the body of your document. In the tag you echo the text, e.g. $result. With CSS you can display the HTML-Tag as a box at the bottom.

Related

some symbols not shown in div container but shown in textarea - php

I have some values in one mysql table:
Tom's SomeText
<html>
And some other text
I want to show this values in a div container and I can only see this:
Tom's SomeText
And some other text
When I want to show the same values in a textarea I get this:
Tom's SomeText
<html>
And some other text
I have mysql in UTF-8-general CI and my php files are UTF8 encoded.
Also I added
<meta charset="UTF-8">
just to be sure. ... but it doesn't work.
FIRST PHP (here '<'html'>' is not shown)
<?php
$sql="SELECT * FROM posts";
$result=$conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc())
{
?>
<div class="col span_2_of_3">
<div class="dl"><h2>EDIT</h2></div>
<p><?php echo nl2br($row["content"]); ?></p>
</div>
<?php
}
}
else{
echo "shit";
}
SECOND PHP (here '<'html'>' is shown)
$sql_query="SELECT * FROM posts WHERE id=".$edit['id'];
$edit_f=$conn->query($sql_query);
if($edit_f->num_rows>0) {
while($red = $edit_f->fetch_assoc()) {
echo '<textarea rows="1000" id="postcontent" name="postcontent">'.$red['content'].'</textarea><br>';
}
}
...
Even here (in stackoverflow's textarea, if I use this symbol '<' without '', it get's hidden :) )
How to fix my problem?
Big thanks in advance

Displaying a PHP result

I am trying to learn php together with MYSQL. I have built the database on on the from end I have written the code below to bring in the website title from the database which works. However, I have two questions.
How would I get the result to display in <h1> tags?
Is this the cleanest way of pulling this value through?
Any help or advise greatly appreciated
<?php
include 'connection.php';
$sql = "SELECT VariableValue FROM VariableValues WHERE VariableID = '1'";
$result = $conn->query($sql);
while($header = $result->fetch_assoc()) {
echo $header["VariableValue"]. "<br>";
}
?>
To get a single row from db, you could use this:
$header_title = implode(mysqli_fetch_assoc(mysqli_query($conn, "SELECT VariableValue FROM VariableValues WHERE id = 1 LIMIT 1")));
Put a php variable between a html tag :
<tag><?php echo $var; ?></tag>
Eg :
<title><?php echo $header_title; ?></title>
You can echo all types of tags in PHP echo
for example:
echo '<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div id="success_alert" align="center" class="alert alert-success">
<strong>Log in to download this chapter.</strong>
</div>
</div>';
I have echoed different types of bootstrap tags in echo. Similarly you can also echo all types of tags you want.
To displays it in tags, use <h1><?php echo $var; ?></h1> for example.

How can I mix PHP and HTML code?

I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>
If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>
> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.
I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>

Get content of multiple div tags with same class name

I read this article - Get DIV content from external Website . I get source of website with file_get_contents() function and I have to extract from it content of two divs with same class name.
I have very similar problem, but with divs with same class name. E.g. I have code like that:
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
I want to get both content of both these divs. Solution accepted in article I linked support only one. My expected result is array like this:
$divs[0] = "Some conete"
$divs[1] = "Second Content"
Please give me advice what to do. I read about DOMDocument class, but have no idea how to use it.
i have used the simple html dom parser and your content can be extracted as
$html = file_get_html('your html file link');
$k=1;
foreach($html->find('div.baaa') as $e){
$divs[$k]=$e;
$k++;
}
echo $divs[1]."<br>";
echo $divs[2];
You could use XPath. XPath is a query language for XML. There are PHP functions that support Xpath.
For you the example could be:
File test.html:
<html>
<body>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
</body>
</html>
The php code that extracts contents of divs with the class "baaa"
$xml = simplexml_load_file('test.html');
$data = $xml->xpath('//div[#class="baaa"]/text()');
foreach($data as $row) {
printf($row);
}
generates the following output:
Some conete
Second Content
Look for XPath tutorials if you need more complex searching or analyzing.
Try it with your data:
$file_contents = file_get_contents('http://address.com');
preg_match_all('/<div class=\"baaa\">(.*?)<\/div>/s',$file_contents,$matches);
print_r($matches);
BTW: Polska rządzi :)
<script type="text/javascript">
$(document).ready(function(){
$('.baaa').each(function(){
alert($(this).text());
});
});
</script>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>

echo content within a div tag on the same interface

I have a scenario like this: Simply a *.php page with a PHP content on top followed with a HTML content. Now with an application of PHP_SELF, I need to echo the message within HTML without loosing HTML interface once the message is echoed within the 'div1' tag. Is this possible?
I tried replacing echo with $msg['div1'] = 'Some message' and inside div1 tag <?= $msgxx['display_log'] ?> but doesn't seems to be a valid code. Please share your views. Code below just to explain my approach. Thanks.
<?php
echo 'some message';
?>
<html>
<form .....>
<div>
<div id="div1"></div>
<div><input type="submit" id="submit1"></div>
</div>
</html>
In php you $message='<b>Hello, world!</b>'. In html
<div id="div1"><?php echo $message ?></div>
You can include inline PHP in your html, perhaps that is where the confusion is coming from? It doesn't always have to be at the top of the page. just wrap your PHP with the <?php ... ?> tags.
In your case, if you wanted to echo some message inside of div1:
<div id="div1"><?php echo 'some message'; ?></div>
Are you trying to echo PHP data within HTML? If that is the case, keep in mind that you can just write your PHP and HTML code just mixed like this. Otherwise, please explain more what you are trying to do and consider posting more detailed code as it's not very clear to me.
<html>
<head>
...
</head>
<body>
<form>
<div id="div1"><?php echo 'some message'; ?></div>
<input type="submit" id="submit1">
</form>
</body>
</html>
You can also use <?= 'some message' ?> which is equivalent to <?php echo 'some message' =>.

Categories