php function in html link - php

I am attempting to call a website root by using a PHP function within a HTML link.
I have created the function bloginfo() below and the correct link output is http://www.example.com/subdirectory/file.php.
The two methods of calling the function are similar but method 1 does not work and method 2 works.
Please can somebody explain why method 1 does not work and suggest a solution. Thanks.
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
echo "test link 1";
?>
<html>
<body>
<!-- Method 2 - works! -->
test link 2
</body>
</html>
Update
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">
Thanks for your help everyone. Unfortunately I could not get the common answer (above line of code) to work as for some reason 'www.example.com' would be printed but not as a link, and the link direction simply became '/subdirectory/file.php'.
To solve the problem I gave up on incorporating a function and decided to simply use the PHP Define method below which works for both methods.
<?php
//this line of code can be put into an external PHP file and called using the PHP Include method.
define("URL", "www.example.com", true);
// Method 1 - works!
echo "test link 1";
?>
<html>
<body>
<!-- Method 2 - works! -->
test link 2
</body>
</html>

The double quoted string is making your php block in the first method just a plain old text string. Try this:
echo "test link 1";

You've nested some php tags
echo "test link 1";

or this:
echo "test link 1";

It was because you were using within tags and not telling it to echo either. The below code will work - I have commented out your old line and added a new line that will work
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
//echo "test link 1";
echo "test link 1"; <--- changed the above line to this... now both this and the lower line work
?>
<html>
<body>
<!-- Method 2 - works! -->
test link 2
</body>
</html>

Related

echo php variabel in HTML(wordpress)

I have created a custom wordpress post type everything works but my client asked me to insert a function that doenst show the button if the link field is empty that is also working but when I want to display the tekst or link the part where the php is inserted just doesnt shows up what am I doing wrong
I am able to get the data on other parts of this php file but not in this part of the page
<?php
$linktitle = $day_aray=get_field("under_shoe_button_title");
$linkexist = get_field("under_shoe_button_link");
echo($linktitle);
if (empty($linkexist)) {
echo '<html> <p></p></html>' ;
}
else {
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html> <?php echo($linktitle); ?> <html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
}
?>
If you would look carefully, you would notice, that you are echoing a string where, inside the string, you are trying to echo again. Even with little programming knowledge, you should understand, that it is not logical to do that.
The same goes for php opening <?php tag. You opened the tag at start of the page and later on, inside a string, you are trying to open it again. This does not work.
Instead, close the string (or escape it) and then add the echo option.
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html>';
echo($linktitle);
echo '<html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
And please, read the comments to you question and learn basic HTML
There are so many things wrong in your code
Firstly you are using echo inside echo you should use concatenation instead.
so you want to echo it like this
echo '<your html code>'.$linktitle.'<your other html code>';
Also your html code is wrong coz u are using many html tags.

Bootstrap Modal Form in a PHP While

(first post ho yeah :) )
So,I have a very strange problem:
I have a <form> to edit some options for each users in a table.
So I use
while (blablabla req sql)
{
echo '<tr><td>['name']</td><td>['firstname']</td><td><button for modal appear></td></tr>';
echo 'my modal here';
}
(this is not the true code)
And... it work but it's look very very ugly (see image 1 when the form is out of the while and image 2 when I put the form into the while)
Any solution?
Code here >> http://paste.ideaslabs.com/show/Tn4cUpjjos
Code generated [only the end of one row] >> http://paste.ideaslabs.com/show/Df4gvrJoS
You have just messed up the HTML. Remember, PHP is just generating an HTML, that you did not do properly. Do something like:
while (blablabla req sql)
{
echo "<tr>";
echo '<td>['name']</td><td>['firstname']</td><td><button for modal appear>';
echo "</tr>";
echo 'my modal here';
}
More suggestions:
Inspect your generated HTML and try to figure out why wrong HTML was generated. Stop comparing UIs.
After one weekend of reflexion I'have finnaly find a way to solve this problem:
I put my modal in a exterior content (at the top of my php page) and in a new while, with the same request
2 same separate request like that
result3 = $bdd->query("SELECT * FROM TABLE_1");
result3 = $bdd->query("SELECT * FROM TABLE_1");
At the top of my .php page after <body>
<div header>
while ($ligne = $result3->fetch_assoc()){
echo '<form></form>';
}
</div>
And after, my table with:
while ($ligne = $result3->fetch_assoc()){
echo '<table></table>';
}
That's all !
Correct my english or ask me if you have a similar problem like that ;)

output 2 string in one function

Ok, I must admit, I am a rather PHP noob who struggles with what seems to me a simple issue.
What i want to accomplish is that I can put 2 strings in 1 function and echo them in different places.
In common.php i have this:
<?
function printHeader($titel) {
?>
Later on in the file i echo it between
And in index.php i have this:
<?
include "common.php";
printHeader('this is my title');
?>
This works alright.... now what i like to do is to ad another String to the printheader to not only echo the title but also the H1, so i tried this:
Common:
<?
function printHeader($titel . $headtitle) {
?>
Index:
<?
include "common.php";
printHeader('This is my title!' . 'This is my H1');
?>
This does not seem to work. Are there any phpsavvy guys out here who can help me wit this simple problem?
If it is not to much to ask I would also like to Echo some standard value if $headtitle is empty, but that is waaaay of my league :)
EDIT: thanks to you guys the first problem is fixed. Now i want to try and fix the IF empty part. So I came up with this:
<?
function printHeader($titel, $headtitle) {
if (empty($headtitle)) {
echo 'title is empty'; }
?>
html goes here + this: <h1><?=$headtitle; ?></h1> more HTML
<? } ?>
This does not seem to work...
any help would be appreciated!
Thanks in advance,
A simple webdesigner ;)
Function arguments should seperate by a comma ,
function printHeader($titel , $headtitle)
And obviously same for calling,
printHeader('This is my title!' , 'This is my H1');
You have to seperate the second the argument by , like this
function printHeader($titel , $headtitle)
and change while calling also
printHeader("string1","String2");
or keep like this
function printHeader($titel){..... }
printHeader("string1"."String2");
so $title1 will have appended string.

Can't use document.write more than one time in PHP

Here is my HTML and I call external PHP
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<script src="index.php"></script>
<p>My first paragraph.</p>
</body>
</html>
and my PHP Script
<?
$strFileName = "poom.bis";
$objFopen = fopen($strFileName, 'r');
if ($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
// echo $file;
echo "document.writeln('$file'+);";
}
fclose($objFopen);
}
$test = "hello world";
echo "document.writeln(
'<ul>'+
'<li>.$test.</li>'+
'<li>test2</li>'+
'<li>test3</li>'+
'</ul>'
);";
?>
It error when using document.write more than one time
What should I do to solve this problem
Please Advice
PS. use echo "document.writeln('$file'+);"; for one time there is no error and show a result
First error: your line
echo "document.writeln('$file'+);";
should be
echo "document.writeln('$file');";
(without the plus sign). Also make sure that the file poom.bis doesn't contain a newline, not even at the end. If it does, you have to strip them away (trim()).
Second error was (until you edited it) the use of document.writeIn (which doesn't exist) instead of document.writeln (which does).
Tested and it works.
Also, while I'm at it, since you asked for advice how to solve this problem: look at your browser's error console and try to debug it.
echo '<script>document.writeln(';
echo '"<ul><li>test1</li><li>test2</li><li>test3</li></ul>"';
echo ');</script>';
;

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

Categories