开发者

Syntax error help

I seem to have a syntax error and can't see it myself, co开发者_StackOverflowuld someone run over it for me please?

Thanks.

<script>
    var acurl_<?php echo $request_data['friendship_id']; ?> = "sn-include/create_bond_accept.php?friendship_id=<?php echo $request_data['friendship_id']; ?>&friend_id=<?php echo $fromuser['id']; ?>";
</script>


Because you got some answers that intended to show you how to improve your code, but actually don't do so (IMO), here is my attempt:

<?php 
    $acurl = array();
    $acurl[$request_data['friendship_id']] = sprintf('sn-include/create_bond_accept.php?friendship_id=%s&friend_id=%s', $request_data['friendship_id'], $fromuser['id']);
?>

<script>
    var acurl = <?php echo json_encode($acurl); ?>
</script>

I would not create dynamic variable names. This code would create a JS object, where the properties are the friendship IDs, something like:

{
    '42': 'sn-include/create_bond_accept...'
}

You can access these URLs more easily from JavaScript than if you have dynamic variable names.


David, on the bright side, you don't have a syntax error.

If you're developing PHP, I would recommend two things:

  1. Get a better IDE. Dreamweaver is TERRIBLE for working with PHP. I recommend NetBeans (it's awesome and free).
  2. Start breaking up your code into chunks. The big ball of html and PHP is hard to debug.

Check this out:

<?php
    // prepare output
    $segment  = '?friendship_id=' . $request_data['friendship_id'];
    $segment .= '&friend_id=' . $fromuser['id'] . '";' . "\n";

    $acurl  = 'var acurl_' . $request_data['friendship_id'];
    $acurl .= ' = "sn-include/create_bond_accept.php';
    $acurl .= $segment;

    $dnurl  = 'var dnurl_' . $request_data['friendship_id'];
    $dnurl .= ' = "sn-include/create_bond_deny.php';
    $dnurl .= $segment;
?>
<script type="text/javascript">
    <?php
        echo $acurl;
        echo $dnurl;
    ?>
</script>


Use here doc instead:

<?php
echo <<<JS
<script>
    var acurl_{$request_data['friendship_id']} = "sn-include/create_bond_accept.php?friendship_id={$request_data['friendship_id']}&friend_id={$fromuser['id']}";
</script>

<script>
    var dnurl_{$request_data['friendship_id']} = "sn-include/create_bond_deny.php?friendship_id={$request_data['friendship_id']}&friend_id={$fromuser['id']}";
</script>
JS;
?>

See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜