Concatenate in php inside HTML - php

i am unable to concatenate i am not so good at it wondering how would i concatenate in this scenario
<?php $id = getfield('id'); ?>// this is a function to get fields from sql
<html>
<?php <a class="profile" href="profile.php?='$id' ">
<echo ucfirst ($firstname);</a> ?>//i cant seem to get this part
</html>
This is what i have tried so far i did try some other ways to do it but none of them seem to work

This is probably what you are looking for:
<?php $id = getfield('id'); ?> // this is a function to get fields from sql
<html>
<a class="profile" href="profile.php?id=<?php echo $id ?>">
<?php echo ucfirst ($firstname) ?>
</a>
</html>
Or, more compact if you have "short tags" enabled inside php:
<?php $id = getfield('id'); ?> // this is a function to get fields from sql
<html>
<a class="profile" href="profile.php?id=<?= $id ?>">
<?= ucfirst ($firstname) ?>
</a>
</html>
And finally you could inline the assignment, since the variable is used only once:
<html>
<a class="profile" href="profile.php?id=<?= getfield('id') ?>">
<?= ucfirst ($firstname) ?>
</a>
</html>

This should do the trick :
<?php
$id = getfield('id'); // this is a function to get fields from sql
echo '<html>
<a class="profile" href="profile.php?='.$id.' ">'.ucfirst ($firstname).'</a>
</html>';
?>

Related

How to pass variable between two web pages with PHP without using session

I need to know How to pass variable between two web pages with PHP without using session , have a DIV link in which inside there is id i want to pass to another page when a link is clicked but i fail to do it. please help me:
firstpage.php ( where there is a link) :
......
<?php
$identity = $_POST['book_id'];
while($ResultsRow=mysql_fetch_array($res)) {
?>
<div class="col-md-2 offset-md-2">
<div class='thumbnail'>
<a href="shopping.php?<?php echo $identity?>" target="blank">
<input type="text" name="book_id" value="<?php echo
$ResultsRow['id'] ?>">
</div>
</div>
</a> <!-- end of link -->
..........
Secondpage.php (where i want to retrieve book id):
....
<p> <?php $myvarC = $_POST['identity'];
echo $myvarC;
?> </p>
....
I think you should try something like below:
firstpage.php
<a href="shopping.php?identity=<?php echo $identity?>" target="blank">
secondpage.php
<p> <?php $myvarC = $_GET['identity'];
echo $myvarC;
?> </p>
You can include it in the urlquery like
shopping.php?id=1234
and then read it on the other side.
You can do this by $_GET;
First Page
<a href="shopping.php?id=<?php echo $identity?>" target="blank">
Second Page
<?php $myvarC = $_GET['id'];
echo $myvarC;
?>
Create a correct url with parameters
<a href="shopping.php?id=<?php echo $identity?>" target="_blank">
And get that value using
<?php
$myVarC = $_GET['id'];
echo $myVarC
?>
First Page
<a href="shopping.php?identity=<?php echo $identity?>" target="blank">
Second Page
<p> <?php $myvarC = $_REQUEST['identity'];
echo $myvarC;
?> </p>

Using if-statements in PHP to print HTML

I'm fairly new to PHP and I've been trying to construct some code to print basic HTML, however the code causes an error 500 whenever used. I am guessing it is a syntax error since I've tried the code in a couple of forms and nothing seems to work (including removing the database lookup and just trying to compare to set values to each other). The script needs to get a variable from the db, compare it to a set value and print the HTML if true, here is the code I am trying:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult(); ?>
<?php if strcmp($category,"Blog")==0 : ?>
<div style="display: -webkit-inline-box" class="sharelogos">
<img src="/images/sharing-icons/facebook.png" width="30px" alt="Facebook" />
</div>
<?php endif; ?>
Any help will be appreciated, thanks!
You if is incorrect, try like this
<?php if (strcmp($category,"Blog")==0) { ?>
<div style="display: -webkit-inline-box" class="sharelogos">
<img src="/images/sharing-icons/facebook.png" width="30px" alt="Facebook" />
</div>
<?php } ?>

HTML inside php condition

The following works fine to show a source image.
<html>
<h3>First Test</h3>
<img src="example1.php" />
</html>
But I wanted to validate user, then only show source image like following,
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch)
<img src="example1.php" />
?>
</html>
When I try the same it simply doesn't show image and doesn't accept <img src="example1.php" /> inside the PHP code.
I am a beginner and just learning php and html.
Could you please guide me how to make it work?
Thanks.
You have to switch in and out of PHP mode
<html>
<h3>First Test</h3>
<?php if($usermatch) { ?>
<img src="example1.php" />
<?php } ?>
</html>
And some like to use echo statements, but you'll see that you get less help from editors when editing the HTML
<html>
<h3>First Test</h3>
<?php
if($usermatch)
echo '<img src="example1.php" />';
?>
A little change to your code, although this is rather an ugly way to do it.
<?php
some logic = $usermatch
if($usermatch) {
?>
<img src="example1.php" />
<?php
}
?>
Don't miss the php closing and opening tags. Try something like this
<?php
some logic = $usermatch
if($usermatch) : ?>
<img src="example1.php" />
<?php
endif;
?>
Try this:
<?php
some logic = $usermatch
if($usermatch) { ?>
<img src="example1.php" />
<?php
}
?>
The problem is that you are mixing your HTML & PHP code. It is perfectly aceptable to have HTML code in your PHP file, but you need to close the PHP code block before so that the interpreter understands that it is not code you intend to run.
Might as well add to all of the answers:
<html>
<h3>First Test</h3>
<?php echo $usermatch ? '<img src="example1.php" />' : ''; ?>
</html>
Use the following:
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch) {
print '<img src="example1.php" />'
}
?>
</html>
or you can use another way to print html tags in php:
<html>
<h3>First Test</h3>
<?php
some logic = $usermatch
if($usermatch) {
?>
<img src="example1.php" />
<?php } ?>
</html>

php inside <a> of HTML

I am a beginner and trying to use PHP statement inside <a> tag of HTML. I don't know whether it is possible or not and I tried to search it on Google, but I couldn't find any answer. Below is the code I am trying to execute.
Whenever I run this code, I do not get any error but the my browser does not display the value of $link1, $link2 and $link3 which I put inside the <a> tag of HTML.
I saved the document as index.php
<?php
$title = 'Shellitic';
$link1 = 'Home';
$link2 = 'Contact';
$link3 = 'About';
?>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1> Welcome to <?php echo $title; ?> </h1>
<p></br></p>
<p>To visit our home page, click on the <?php $link1; ?> button</p>
<p>To visit our contact page, click on the <?php $link2; ?> button</p>
<p>To visit our About page, click on the <?php $link3; ?> button</p>
</body>
You need to echo the values:-
<?php echo $link1; ?>
Some servers may accept this
<?= $link1; ?>
But it is typically safer to use
<?php echo $link1; ?>
Add echo command.
Like this:
<?php echo $link1; ?>

if statement in anchor

I am trying add an if statement to an anchor like so.
<div class="box-button">
<a href="
<?php $header_button = of_get_option('header_text'); ?>
<?php if($header_button){?>
<?php echo of_get_option('header_text'); ?>
<?php } else { ?>
#
<?php } ?>
" class="button">Connect Now</a>
</div>
I can click the button on the homepage and I will get linked to "#" so it is as if wordpress doesn't recognize as a theme option. Is my syntax the problem?
You know you can do the logic outside the html and then insert the result into the href
<?php
$header_button = of_get_option('header_text');
$link = (!empty($header_button)?$header_button:'#');
?>
<div class="box-button">
Connect Now
</div>

Categories