Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9512

Re: values not changing

Message-ID <2898756.xqxexv6urH@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-05-22 00:30 +0200
Subject Re: values not changing
Newsgroups comp.lang.php
References <14jsq8i4oxkxn.r427ym6k6zgn$.dlg@40tude.net> <kngo8r$e55$5@dont-email.me>

Show all headers | View raw


Denis McMahon wrote:

> The data you are assembling looks like json data. For json data, you need
> to quote the attribute name string. The json needs double quoted strings,
> so as you also need to use php double quoted strings to preserve variable
> substitution, you need to use escaped double quoted strings for the json
> string values.
> 
> echo "{\"image\":\"\",";
> echo "\"title\":\"{$vid[0]}\",";
> echo "\"file\":\"http://www.youtube.com/watch?v={$vid[2]}\",";
> echo "\"description\":\"{$year} # {$number} - {$vid[1]}\"},";

Assuming JSON, it is much better (if not even the only *correct* way) to 
write

  echo json_encode(array(
    'image' => '',
    'title' => $vid[0],
    'file'  => "http://www.youtube.com/watch?v={$vid[2]}",
    'description' => "{$year} # {$number} - {$vid[1]}"
  ));

Because you cannot simply insert arbitrary strings in a generated string 
literal and hope the outcome will be correct JSON; *at least* you would have 
to escape slashes in the inserted strings, and you do not have to do that by 
yourself if you use json_encode() in the first place.  AISB.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

values not changing richard <noreply@example.com> - 2013-05-21 14:17 -0400
  Re: values not changing Christoph Becker <cmbecker69@gmx.de> - 2013-05-21 20:45 +0200
  Re: values not changing Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-21 21:17 +0000
    Re: values not changing Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-22 00:22 +0200
    Re: values not changing Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-22 00:30 +0200
      Re: values not changing Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-22 00:32 +0200
  Re: values not changing Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-05-21 22:23 +0000
    Re: values not changing "Peter H. Coffin" <hellsop@ninehells.com> - 2013-05-21 21:46 -0500
    Re: values not changing richard <noreply@example.com> - 2013-05-21 23:40 -0400
      Re: values not changing Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-05-22 10:34 +0000
      Re: values not changing Scott Johnson <noonehome@chalupasworld.com> - 2013-05-22 05:41 -0700
  Re: values not changing David Robley <me@home.invalid> - 2013-05-22 10:02 +0930

csiph-web