douchang8758 2014-04-09 19:24
浏览 44
已采纳

检查是否存在具有匹配名称的PDF文件,并链接到该文件

I am working on a real estate website. We are drawing in real estate listings using MLS numbers and some of these listings have PDF brochures. I would like to check if a brochure exists for a listing - the PDF file is "mls#.pdf", with the MLS # matching the listing. If there is a PDF, I want to link to that. Otherwise the link should go to the MLS page for that listing.

I have tried this code below, but I don't know how to set it to search for/link to the filename that matches the listing. (The second echo is clearly wrong too, I don't know how to go back and forth from php in that way).

<?php
$inc_file = "pdf/$MLS_ACCT.pdf";
if (file_exists($inc_file)) {
echo '<a href="pdf/$MLS_ACCT.pdf">Click here for Brochure - Open PDF at 100%</a>';}
else {
echo '<a href="property-search-results.php?MLS_ACCT=<?php echo $MLS_ACCT ;?>" target="_top">Further Details >></a>'; }
?>
  • 写回答

1条回答 默认 最新

  • doufuxing8691 2014-04-09 19:28
    关注

    Your code that checks whether the file exists looks good. Here's a rundown of how PHP parses variables in strings:

    There are several ways to use variables in strings:

    $name = "Jeff";
    
    // parsing in double quotes
    $string = "My name is $name";
    
    // you can also use curly braces for better readability
    $string = "My name is {$name}";
    
    // or you can concatenate
    $string = "My name is " . $name;
    

    In single quotes, only the concatenation method works. You should do something like this:

    echo '<a href="pdf/' . $MLS_ACCT . '.pdf">Click here for Brochure - Open PDF at 100%</a>';
    

    and you should have the variable printing correctly.

    Finally, in your last echo statement, you have <?php ?> in your string. That will print literally - you only use those when you're switching to PHP from pure HTML. Instead, use the same string concatenation method described above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP