Feabionsu Posted December 30, 2010 Share Posted December 30, 2010 Hi there, I am trying to have a PHP variable in my link, so that my final link would look like this: http://www.example.com/eformat/variable-goes-here.rtf I've managed to get http://www.example.com/variable-goes-here using the code below, but I can't seem to insert the "eformat/" and ".rtf" parts. I really don't know PHP very well, so help would be greatly appreciated. Please excuse my beginner level. Thank you! <?php $eformat = $post–>post_name; echo "<a href=\"$eformat\">Download this blog post in eFormat (.rtf)</a>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/ Share on other sites More sharing options...
Maq Posted December 30, 2010 Share Posted December 30, 2010 echo " Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153086 Share on other sites More sharing options...
karimali831 Posted December 30, 2010 Share Posted December 30, 2010 try: echo '<a href="'.$eformat.'rtf">Download this blog post in eFormat (.rtf)</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153088 Share on other sites More sharing options...
Maq Posted December 30, 2010 Share Posted December 30, 2010 I prefer to have the master string in double quotes because it allows variables to interpolate. For attributes, I use single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153095 Share on other sites More sharing options...
Feabionsu Posted December 30, 2010 Author Share Posted December 30, 2010 Karim, your code only gave me http://www.example.com/rtf. What do you think? Maq, I implemented your code but it only gave me http://www.example.com/eformat/.rtf. I'm not sure why it refuses to recognize the variable $eformat, since the code in my first post makes it work... any ideas? <?php $eformat = $post–>post_name; echo "<a href='eformat/{$eformat}.rtf'>Link</a>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153100 Share on other sites More sharing options...
Maq Posted December 30, 2010 Share Posted December 30, 2010 What is $post–>post_name;? Does it return anything? Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153103 Share on other sites More sharing options...
Feabionsu Posted December 30, 2010 Author Share Posted December 30, 2010 It is variable that returns the post slug in Wordpress. So for example, a blog post that has the URL http://www.example.com/example-post would return as "example-post" (without quotes) when using that variable. Same thing with http://www.example.com/2010/12/example-post. My purpose is to make the blog post downloadable in rtf format. I want to have a direct link to the .rtf file of each blog post. I figure that if I make a folder located at http://www.example.com/eformat and put .rtf files with the same names as the post slugs of the blog posts, it would work. I could try an alternative variable, such as the post title or the post number... EDIT: Here is the list of the variables I can use. I would still prefer to use the post slug though. Makes things simpler and better for the reader, than something called 001.rtf. I'm assuming I can't just use $post–>post_name since it always gives me errors... that's why I switched to using $eformat. But like I said, I virtually know no PHP, so I'm not really knowledgeable on what works and what doesn't, what code is correct and what is incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153106 Share on other sites More sharing options...
Feabionsu Posted December 30, 2010 Author Share Posted December 30, 2010 Wow, two solutions have been found on two other forums. And here I was trying to figure out even one solution by myself. <?php echo "<a href='eformat/{$post->post_name}.rtf'>Download this blog post in eFormat (.rtf)</a>" ?> <?php $var = $post->post_name; echo '<a href="eformat/' . $var . '.rtf">Download this blog post in eFormat (.rtf)</a>'; ?> Maq, thank you for your help. Your code was essentially the same as the solution; it was just me who thought I couldn't use plain old $post–>post_name with it. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153125 Share on other sites More sharing options...
Maq Posted December 30, 2010 Share Posted December 30, 2010 Glad you got it working. Weird that either of our solutions were successful because, like you said, they are essentially the same. Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153132 Share on other sites More sharing options...
Feabionsu Posted December 30, 2010 Author Share Posted December 30, 2010 Just another quick question: how would I include a title tag in your solution with the title also being a variable? Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153162 Share on other sites More sharing options...
Maq Posted December 30, 2010 Share Posted December 30, 2010 Something like this: $var = $post->post_name; $title = $post->post_title; echo "Download this blog post in eFormat (.rtf) - {$title}';"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153166 Share on other sites More sharing options...
Feabionsu Posted December 30, 2010 Author Share Posted December 30, 2010 Sorry, I meant the title attribute, as in <a href="" title="">. Following your example though, I've devised this: <?php echo "<a href='eformat/{$post->post_name}.rtf' title='{$post->post_title} (.rtf)'>(.rtf)</a>" ?> It works perfectly; thanks again! =D Which solution (the 'var' one or this one) would be considered a more clean code? Which one should I use? I like the one above since it essentially takes up less space, but perhaps you consider the other to be more organized and easy-to-read? Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153177 Share on other sites More sharing options...
Maq Posted December 31, 2010 Share Posted December 31, 2010 IMO, the 'var'. I generally like to define variables before they are used so if you have to make a change to them you only have to do it in a single place. Quote Link to comment https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/#findComment-1153263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.