Hello could someone help me with something? I have this code:
<?php if($hashtagExist == true): ?>
<?php foreach($resultsTags as $tag): ?>
<?php echo $tag["Tags"];?>
<?php endforeach; ?>
<?php else: ?>
<?php endif; ?>
I'm testing out my function to print out hashtags, so if there's a hashtag it puts it in a tag. The results are correct, BUT the <a href> isn't a clickable link, but just plain text and I can't find the solution.
I prefer using php to echo html than to embed php into html tags or properties...
<?php
if($hashtagExist == true){
foreach($resultsTags as $tag){
$unhash = str_replace("#","",$tag); //Removes the hashes
echo ''.$unhash.''; //Display link with removed hashes
echo ''.$tag.''; //Retains the name of the hash with hash before the name i.e #hash
$out .= ''.$unhash.''; //in case you want to use this between some other tags
}
} else {
$out = '';
}
echo $out; //result variable can be used anywhere in the page
?>
Related
I have more GET variables. I want the variables to be added after clicking. And after clicking again I want them to be removed from GET. I am a beginner and I don't know how to do it.
<?php
$tab_array['name1']=1;
$tab_array['name2']=2;
$tab_array['name3']=3;
$tab_array['name4']=4;
$url=$_SERVER[QUERY_STRING];
if(!empty($url)){
$url="&".$url;
}
?>
<form action="" method="GET">
<?php
foreach($tab_array as $key => $val){
?>
<?php echo $key;?>
<?php
}
?>
</form>
Someone will help?
Sorry for my bad English...
Code
<?php
$tab_array['name1']=1;
$tab_array['name2']=2;
$tab_array['name3']=3;
$tab_array['name4']=4;
$url='?'; // this will call the current url
// $_GET is the array your submitted data will be strored in - initially empty []
?>
<form method='GET'><!-- you actually don't need a form, since only input, textarea, etc get submitted -->
<?php
// save current state, so you can modify it for every possible click
$current_get = $_GET;
foreach($tab_array as $key => $value) {
$new_get = $current_get;
// check if the current key is already in your set ($new_get)
if (array_key_exists($key, $new_get)) { // it is, so remove
unset($new_get[$key]);
} else { // it is not, so add
$new_get[$key] = $value;
}
// build your query_string (from $new_get array)
$query_string = http_build_query($new_get);
// you can use variables in string when its encapsulated in double quotes: "text $var"
echo "<a href='$url$query_string'>$key</a>";
// more readable:
// echo "<a href='{$url}{$query_string}'>{$key}</a>";
// not need but looks better :P
echo '<br />' . PHP_EOL;
}
?>
</form>
A little more
there is a shortcut for <php echo $var ?> which is just <?= $var ?>
Links
array_key_exists
unset
http_build_query
I'm trying to use the php get. everthing is working find exept the include page.
it's adding 1 at the end of include content
any ide how do i ride that?
example:
I love norway
I love norway1
Code:
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') $norge = include('norway.php');
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
We should use file_get_contents, instead of include.
$norge = file_get_contents("norway.php");
Use the output buffer functions to capture the output from the included script.
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') {
ob_start();
include("norway.php");
$norge = ob_get_clean();
}
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
if( $user->username == 'XYZ' )
{
echo "hello, XYZ";
}
else
{
echo "hello, guest";
}
In the above code, can i use pure html code which will get executed incase the IF statement is true instead of using echo ?
Yes, you can do it:
if( $user->username == 'XYZ' )
{
?>
hello, XYZ
<?
}
else
{
?>
hello, guest
<?
}
Sometimes it looks better and simplier. But in fact it is much better to put php and html code in different files (separate logic, styles and data).
if( $user->username == 'XYZ' )
{ ?>
<p>Hello <b><i>XYZ</i></b>
<?php }
else { ?>
<p>Hello <b><i>Guest</i></b>
<?php
}
I hope You Got it.
Of course you can write HTML directly by closing the <?php ?> tags.
<?php if( $user->username == 'XYZ') { ?>
hello, XYZ<br>
<?php } ?>
<?php else { ?>
hello, guest<br>
<?php } ?>
You can of course heredoc as well:
<?php
$str = <<<FOO
This is a
demo message
FOO;
echo $str;
?>
but i dont use it since i it messes with any highlighting editor (it thinks it's a text and i like my HTML highlighted)
What i like best is this, especially when my outputs are big:
<?php if( $user->username == 'XYZ') {
include("user_template.php");
}
<?php else { ?>
include("guest_template.php");
<?php } ?>
which are actually just rendering HTML contained there.
Can also be written like this, more readable for several lines.
<?php if ($user->username == 'XYZ') : ?>
Hello, XYZ
<?php else : ?>
Hello, Guest
<?php endif; ?>
I having issues getting a function to echo, where $lightbox_link1 = get_custom_field('lightbox_link1'). I'm fairly new to PHP.
Below is the defining function:
// Check for a lightbox link, if it exists, use that as the value.
// If it doesn't, use the featured image URL from above.
if(get_custom_field('lightbox_link1')) {
$lightbox_link1 = get_custom_field('lightbox_link1');
} else {
$lightbox_link1 = $image_full[0];
}
Echo Function:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '';
} ?>
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
should be
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
= is used for assignment
== is used for comparison
=== is used for typesafe comparison
also you can't declare <?php ... ?> inside another <?php ... ?>
to get something like <?php ... <?php ... ?> ... ?>
take a look at what you did up to here:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '<a href="<?php
Instead, using doublequotes in your echo statement will allow for the php variables inside to be parsed, so you could just do
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
to get
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
} ?>
I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1