How would I go about styling echo? - php

I was wondering how would i go about styling a echo in php? im trying to style the paragraph tag nested inside one of the echos, i'd be grateful if anyone can tell me how to do this. thanks
PHP
if(isset($_SESSION['sess_user_id']) && $_SESSION['sess_user_name'] != "") {
echo '<h1>Welcome '.$_SESSION['sess_user_name'].'</h1>';
echo "<p id="profileText"This is your personal profile page, from here you can edit events</p>";
echo '<h4>Logout</h4>';
}
else {
header('location:index.php');
CSS
#profileText {
top: 40%;
position: absolute;
color: blue;
}

Better yet, don't do presentation from within your php logic. Consider this:
<?php
if(!isset($_SESSION['sess_user_id']) || $_SESSION['sess_user_name'] == "") {
header('location:index.php');
}
// do any other php stuff...
?>
<h1>Welcome <?= $_SESSION['sess_user_name'] ?></h1>
<p id="profileText">This is your personal profile page, from here you can edit events</p>
<h4>Logout</h4>
This way,
Your logic and presentation are clearly separated
If you want to change the presentation, you don't have to mess around with your php code
You don't have to worry about single quote / double quote / escape quote / blah, blah, blah.
You don't have to worry about output before the header
The programmer that comes after you will be amazed at how clean your code is :)
It lets you easily see typos: <p id="profileText"This is your personal profile page, from here you can edit events</p>

You have syntax error.
echo "<p id="profileText"This is your personal profile page, from here you can edit events</p>";
Use single quote in your echo.
echo "<p id='profileText'>This is your personal profile page, from here you can edit events</p>";
If you are use double quote in your echo, you can't use another double quote in your string. You can do on this way:
echo "<p id=\"profileText\"> This is your personal profile page, from here you can edit events</p>";
Or you can use single quote on your string
echo "<p id='profileText'>This is your personal profile page, from here you can edit events</p>";

Related

display none if php function is empty

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;
I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.
<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
Why not completely not echo "advert" element?
if ( !empty($_GET['details']) ){
echo '<div class="advert">add text</div>';
}
if you really want to just hide, you can assign hide class
<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>
then you would need to add "hide" class with display:none in your style.css
Above is shorthand/ternary if/else statement used, its great if you need output some string.
And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.
Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().
This should do it for you
<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
$advert_display_string = "style='display: none'";
}
?>
<div class="advert" <?php echo $advert_display_string ; ?> ></div>`
but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below
<?php
if (isset($_GET['details'])) {
?>
<div class="advert"></div>
<?
}
?>

php - formatting a link correctly

I have a question about an optical problem I have with php. I'm echoing a filename with this function:
echo $name."<a href='download.php?dow=$path'>Download</a><br>";
The download link appears directly behind the file name like this test.txtDownload. How do I add spaces there or how is it possible to put the download link into the 'next column' so there are listed correctly among themselves?
you can play around this:
<?php echo $name."<a class='link' href='download.php?dow=$path'>Download</a><br>";
?>
style:
<style type="text/css">
a.link {
margin-left: 10px;
}
</style>
or simply :
<?php echo $name." <a class='link' href='download.php?dow=$path'>Download</a><br>"; ?>
But I would suggest you to use css so that it can be managed, while normal space will not.
Just add a blank space in your string:
echo $name." <a href='download.php?dow=$path'>Download</a><br>";
Another option would be use some css to set the appropriate space between link name and button
Try this
echo $name." "."<a href='download.php?dow=$path'>Download</a><br>";
use the following code. Download link appears in down
echo $name.<br>.<a href='download.php?dow=$path'>Download</a><br>
instead of adding space use &nbsp instead of <br> tag.It will give you one tab space.

disable a href links using PHP

I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo 'Link';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">

Problem escaping php variable

I'm having trouble escaping the PHP variable inside the getItems function:
while($row = mysql_fetch_array( $data ))
{
echo "<div class='favorite'>";
echo "<div style='display: inline;'>".$row['Item']."</div>";
if ($row['UID'] = $uid) {
echo "<div id='unlock'>Info</div>";
} else {
echo "<div id='unlock' onclick='getItems('".$row['Item']."')'>Unlock</div>";
}
echo "</div>";
}
When rendered (is render the word?) anyway, when I see it on my site it says:
onclick="getItems(" whatever')'
What am I doing wrong?
You can see the code here:
http://www.chusmix.com/game/insert/get-items.php?user=19
Your problem is that your attribute values are surrounded by single quotes, but you're also using single quotes in your javascript.
You'll have to use double quotes in your javascript. However, since the whole string (in PHP) is surrounded by double quotes, you'll have to escape them. Hence:
echo "<div id='unlock' onclick='getItems(\"".$row['Item']."\")' style='display: inline; float: right;'>Unlock</div>";
Or like this:
echo "<div id='unlock' onclick='getItems(\"{$row['Item']}\")' style='display: inline; float: right;'>Unlock</div>";
To clarify what the curly braces do (from the PHP docs):
Complex (curly) syntax
This isn't called complex because the syntax is complex, but because
it allows for the use of complex expressions.
Any scalar variable, array element or object property with a string
representation can be included via this syntax. Simply write the
expression the same way as it would appear outside the string, and
then wrap it in { and }.
To further explain, let's say we have the following scenario:
$name = 'Apple';
$sentence = "$names are my favorite fruit";
What I'm trying to get is: Apples are my favorite fruit. However, this won't work. PHP will instead be looking for a variable called $names, and when it doesn't find it, it'll complain.
So, to remedy this, we can surround our variable in curly braces:
$name = 'Apple';
$sentence = "{$name}s are my favorite fruit";
Great! Now PHP will know where the variable name ends and the string starts.
On a side note: You might consider switching to double-quoting your attributes, since the way you do it now is not valid xHTML (unless you don't care).
Yes, there is a problem with your quotes. It should be this:
echo "<div id='unlock' onclick='getItems(\"".$row['Item']."\");' style='display: inline; float: right;'>Unlock</div>";
The problem is that your opening quotes for onclick and the quotes around the function arguement have to be a different kind of quote.
This is much easier though to do with html and then just insert the variable like this:
<div id="unlock" onclick="getItems('<?=$row['Item'];?>');" style="display: inline; float: right;">Unlock</div>
Doing things this way instead of echoing HTML when possible will save you tons of time and confusion, and you won't have to worry about all the escaping of quotes
The ' inside onclick is closing the onclick itself. Change it to:
onclick='getItems(\"".$row['Item']."\")'
That way, in JS, it uses a different type of quote.
Even better... you can leave PHP, and have one less type of quote to worry about.
else { ?>
<div id='unlock' onclick='getItems("<?=$row['Item'];?>")' style='display: inline; float: right;'>Unlock</div>
<?php
}
or like so:
echo '<div id="unlock" onclick="getItems('."'".$row['Item']."'".')" style="display: inline; float: right;">Unlock</div>';
If I had to do this, it would have looked like:
<?php while(true) :?>
<div class="favorite">
<div style="display: inline;"><?php echo $row['Item'];?></div>
<?php if ($row['UID'] = $uid):?>
<div id="unlock">Info</div>
<?php else: ?>
<div id="unlock" onclick="getItems('<?php echo $row['Item']; ?>)">Unlock</div>
<?php endif;?>
</div>
<?php endwhile;?>
try the following .
edit: changed to make sure quotes were escaped correctly
echo "<div id='unlock' onclick=\"getItems('{$nameArray[0]}')\" ></div>";

Complicated case of escaping quotation marks

Here is the code I am trying to make work. As you can see, the code as it stands will call a javascript window and that works well. I now want to add the lines that are commented out, in order to make the window display or not display conditionally. I have tried many combinations of escaping quotation marks, or substituting them, but nothing works properly. Usually I lose the link action along with a display of some of the code attached to the link. Could someone have a look and see if they can propose a code solution.
<?php
//if ($review != 0)
//$yes2 =
if ("{$row['passState']}" == 0) {echo "<a href='javascript<b></b>:void(0);'NAME='var basestring='window'+new Date().getTime();' title=' Results of quiz 'onClick=window.open('check/check.php?quizTitle=". urlencode($quizTitle) ."', 'width=1100, height=510, resizable=yes, menubar=no, status=0, scrollbars=1');> <p>Check your answers</p> </a><br />\n";}
//echo $yes2;
//if ($review != 1)
//echo "";
?>
If it is any help, I'm using this code elsewhere on the page and it works fine.
<?php
if ($review != 0)
$yes2 = "REVIEW";
echo $yes2;
if ($review != 1)
echo "";
?>
if ("{$row['passState']}" == 0) {echo "<a href='javascript<b></b>:void(0);' NAME=\"var basestring='window'+new Date().getTime();\" title=' Results of quiz ' onClick=window.open('check/check.php?quizTitle=\". urlencode($quizTitle) .\"', 'width=1100, height=510, resizable=yes, menubar=no, status=0, scrollbars=1');> <p>Check your answers</p> </a><br />\n";}
Copy this as it is....
There's no need for all the escapes, you should embed the PHP code in the HTML, not the other way around. Example:
<?php if ($row['passState'] == 0): ?>
<p>
<a href="javascript:your_code()">
Check your answers
</a>
</p>
<br />
<?php endif; ?>
I had a hard time deciphering what your javascript was trying to actually do, but this will make it a lot easier for you to read and write HTML without worrying about escaping quotes, using \n characters, etc.
http://php.net/manual/en/control-structures.alternative-syntax.php
You can use addslashes method to escape quotes
PHP Manual
Your onClick needs to have a quote(s) after the =:
...f quiz' onClick=\'window.open('check/check.php?quizTitle=". urlencode($quizTitle) ."',

Categories