How to assign a variable to a GET value in PHP - php

<img src="phone01.jpg">
When I write the code like this it's working fine. But when I edit this to assign a php variable to GET value, it's not working.
<a href="Details.php?phoneid=<?php $Pid_arr[count($Pid_arr)-1]?> " class="product" title="Product 1">
Can someone pleas explain why ?
Thanks in advance.

You need to echo the variable:
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1]; ?>" >
You can also do it this way:
<?=$Pid_arr[count($Pid_arr)-1]?>

You have to either echo it, like this:
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1]; ?> " class="product" title="Product 1">
Or use shorttags:
<a href="Details.php?phoneid=<?=$Pid_arr[count($Pid_arr)-1]?> " class="product" title="Product 1">

Be sure to echo it:
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1] ?> class="product" title="Product 1">

Try;
<a href="Details.php?phoneid=<?php echo $Pid_arr[count($Pid_arr)-1]; ?> " class="product" title="Product 1">

I would try writing this with short tags. I believe it gives better readability
<a href="Details.php?phoneid=<?=$Pid_arr[count($Pid_arr)-1]?>" class=.......

Related

Style php echo with css and blank line

I´m trying to style the following php echo
<a href="" title="<?php
echo"
<div class='test'>".$name."</div>'/n'
<div class='test2'>".$itemLevel."</div>'/n'
";
?>">Test Link</a>
But i think i don´t insert the div class and the '/n' at the right place. Could anyone help me?
You don't echo the HTML content inside the a href's title.
This will work:
<a href=""><?php echo"
<div class='test'>".$name."</div>'/n'
<div class='test2'>".$itemLevel."</div>'/n'
";
?>Test Link</a>
Optimizing your code:
<?php echo '
<a href="">
<div class="test">'.$name.'</div>
<div class="test2">'.$itemLevel.'</div>
Test Link</a>';
You must encode your html in order to put it into the title property
<?php
$title = '<div class="test">'.$name.'</div>' . PHP_EOL .
'<div class="test2">'.$itemLevel.'</div>' . PHP_EOL;
?>
Test Link

Make a php a href link with variable clickable

i have a question.
I have this code to get an profile name as an link.
<a href=<?php echo $userpro->permalink( get_the_author_meta('ID') );
?>><?php echo $user_id=get_the_author_meta('display_name');?></a>
The link isnt clickable. I am a newbie in coding.
So maybe somebody can help me :)
Greetings
Try this:
<a href="<?php echo $userpro->permalink(get_the_author_meta('ID')); ?>">
<?php echo get_the_author_meta('display_name'); ?>
</a>
If it doesnt work... please supply the value and initialisation of $userpro!

Avoid showing icons that don't contain a value

I've already been searching for a couple of hours for a solution. What I want to do is make icons that don't got a value (in the php) not show up.
Here's an image of what I currently have.
So for instance if only twitter and facebook got values, they should only appear on the screen.
Below is the code, and I hope someone got a solution for this.
Cheers!
<ul class="social-buttons">
<div class="wrapping">
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<a class="social" href="http://facebook.com/<?php echo $profile_data['facebook']; ?>" target="_blank"><li class="facebook"></li></a>
<a class="social" href="skype:<?php echo $profile_data['skype']; ?>?chat"><li class="skype"></li></a>
<a class="social" href="http://instagram.com/<?php echo $profile_data['instagram']; ?>"><li class="instagram"></li></a>
<a class="social" href="http://dribbble.com/<?php echo $profile_data['dribbble']; ?>"><li class="dribbble"></li></a>
<a class="social" href="http://linkedin.com/in/<?php echo $profile_data['linkedin']; ?>"><li class="linkedin"></li></a>
</div>
</ul>
You need to use the if statement with !empty(). The !empty() checks if a variable is NOT empty. Than proceed with the code. Like the example given here:
<?php
if(!empty($profile_data['twitter'])){
?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php
}
?>
If the variable is empty, it wont give the outputed code, in your case the <a> with the icon.
i think you can do like:
<ul class="social-buttons">
<div class="wrapping">
<?php if $profile_data['twitter'] {?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php } ?>
....
</ul>
Imo, a probably better way to do this is to perform a pre-processing of the data i.e. $profile_data, before you use it in your view so the view would no longer need to handle the processing logic. After which, the view can output your links by using a more concise construct e.g. for loop, that does not use any conditional branching.

Quick PHP echo help

What is wrong with this?
echo '<a title="Last Chance" href="'.the_permalink().'" class="status open">Last Chance</a>';
As it's putting the the_permalink() before the <a instead of inside it.
Wordpress often echo's the content out of the function instead of returning it.
Use get_permalink() instead.
echo '<a title="Last Chance" href="'.get_permalink().'" class="status open">Last Chance</a>';
http://codex.wordpress.org/Function_Reference/get_permalink
http://codex.wordpress.org/Function_Reference/the_permalink
Actually it looks good to me (but see my edit comment).
Better is to embed PHP into HTML:
<a title="Last Chance" href="<?php the_permalink(); ?>" class="status open">
Last Chance
</a>
Edit: As #Marwelln found out, the_permalink() is already echoing data. Still, this is a better solution than echoing the HTML.
I guess you have echo "abc" in the the_permalink function. In order for this to work as you wish, you have to return "abc" instead of using echo.
Use it like this (not inside echo )
<a title="Last Chance" href=" <?php the_permalink() ?> " class="status open">Last Chance</a>
See http://codex.wordpress.org/Function_Reference/the_permalink for more details.

Is there a better way to write this php code?

return'
<li>
<img class="avatar" src="images/' . $picture . '" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>' . $username . '</strong> '. autolink($tweet).'
<div class="date">'.relativeTime($dt).'</div><div class="date">'. $reply_info . '</div> <a class ="reply" href="home.php?replyto=#'. $username .'&status_id='. $id .'&reply_name=' .$username.'"> reply </a>
</div>
<div class="clear"></div>
</li>';
I was wondering if there is a cleaner way to write this code, and taking in mind processing time, if that really means anything.
p.s. this code is part of a function, this is the return statement!
Yes. Use double quotes for the PHP string (and single quotes for the HTML attributes), then you can just use PHP variables in the string, like so:
"<a href='nano.com/$username'>";
Is processing time really an issue? I doubt it, but profile to be sure.
Edit: If anyone is unsure about using single quotes in HTML attributes, have a look at this question. It's pretty unanimously agreed that single quotes are fine. If anyone can give a decent counter-argument I'd be happy to hear it.
You could use HEREDOC syntax :
$auto = autolink($tweet);
$rel = relativeTime($dt);
return <<<ENDOFRETURN
<li>
<img class="avatar" src="images/$picture" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>$username</strong> $auto
<div class="date">$rel</div><div class="date">$reply_info</div> <a class ="reply" href="home.php?replyto=#$username&status_id=$id&reply_name=$username"> reply </a>
</div>
<div class="clear"></div>
</li>
ENDOFRETURN;
Cleaner template and php code -> use MVC
Yes, there is one, and you don't need MVC (only a template):
<li>
<a href="nano.com/<?=$username ?>">
<img class="avatar" src="images/<?=$picture ?>" width="48" height="48" alt="avatar" />
</a>
<div class="tweetTxt">
<strong><?=$username ?></strong>
<? echo autolink($tweet) ?>
<div class="date"><?=relativeTime($dt) ?></div>
<div class="date"><?=$reply_info ?></div>
<a class="reply" href="home.php?replyto=#<?=$username?>&status_id=<?=$id?>&reply_name=<?=$username?>">
reply
</a>
</div>
<div class="clear"></div>
</li>
Must read: http://wiki.yet-another-project.com/php/the_one_single_step_for_a_cleaner_code . It describes how you have to use the code above.
I would cut in several pieces and use sprintf() to tie it all together.
You can use a template engine i.e. smarty, twig, ...

Categories