php-general Digest 18 Dec 2011 19:40:37 -0000 Issue 7615

Topics (messages 316055 through 316060):

Re: dealing with this code $_POST['custom“]
        316055 by: Stuart Dallas

Re: Working on a Subsummary Report
        316056 by: DealTek
        316060 by: Jim Lucas

Re: Preferred Syntax
        316057 by: Ross McKay

Converting a Unicode code to related charachter
        316058 by: Ali Asghar Toraby Parizy
        316059 by: Fatih P.

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 17 Dec 2011, at 13:27, Marc Guay wrote:

> Just forwarding Carlos' response to the list...
> 
> 
> ---------- Forwarded message ----------
> From: Carlos Sura <carlos.su...@googlemail.com>
> Date: 17 December 2011 01:15
> Subject: Re: [PHP] dealing with this code $_POST['custom“]
> To: Marc Guay <marc.g...@gmail.com>
> 
> 
> 
> 
> On 16 December 2011 15:57, Marc Guay <marc.g...@gmail.com> wrote:
>> 
>>> $saving_list = $_POST['custom'] == 'FR' ? 148 : 147;
>> 
>> Hi there, here's a quick translatation of this code that might help
>> you understand it better:
>> 
>> if ($_POST['custom'] == 'FR'){
>>  $saving_list = 148;
>> }
>> else{
>>  $saving_list = 147;
>> }
> 
> 
> 
> Hello,
> 
> Thank you both for answer me.
> 
> Also I would like to thank you Marc Guay, because with your answer I
> figured out how to do it.
> 
> My code is like this:
> 
> //$saving_list = $_POST['custom'] == 'FR' ? 148 : 147;
>   if ($_POST['custom'] == 'FR'){
>                 $saving_list = 148;
>                }
>   if ($_POST['custom'] == 'EN'){
>                 $saving_list = 147;
>                }
>  else{
>  $saving_list = 152;
>  }
> 
> I'm not sure if that's the best way, but it is working for me as I
> expected, althought any other recommendation would be great.


In that case you haven't tested FR yet. FR will set it to 152, not 148. Try 
this...

if ($_POST['custom'] == 'FR') {
  $saving_list = 148;
} elseif ($_POST['custom'] == 'EN') {
  $saving_list = 147;
} else {
  $saving_list = 152;
}

Personally I'd recommend a switch for this type of logic...

switch ($_POST['custom']) {
  case 'FR':
    $saving_list = 148;
    break;
  case 'EN':
    $saving_list = 147;
    break;
  default:
    $saving_list = 152;
}

However, given the simplicity of what you're doing, an array would be a lot 
more efficient...

$saving_list_options = array(
  'FR' => 148,
  'EN' => 147,
);
$saving_list = isset($saving_list_options[$_POST['custom']]) ? 
$saving_list_options[$_POST['custom']] : 152;

Thinking a little beyond that, is that list of options really hard coded or do 
they exist elsewhere (e.g. in a database)? If they do then you really should be 
getting the value from there.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
>> 
> 
> for the above to work right, you will need to loop through the mysql result 
> set
> one time.  Placing all the results in to one large array.  Then you can loop
> through the array as many times as needed.
> 
> What you will probably find is that you can sort all the data into the proper
> order withing your SQL statement.  Then display the data looping through the
> mysql result set once without having to create the additional array mentioned 
> in
> my first statement above.



Thanks Jim,

I will look into how to make this work with arrays...

more beginner questions...

- as stated -  when I try to loop through 2x ...
- the table 2 only shows 1 record..
- Q: ... like the query number row number? needs to be reset - but how?

So is it not possible to loop more than 1x? Is that why we are loading into 
array?


--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-11]




--- End Message ---
--- Begin Message ---
On 12/17/2011 6:14 PM, DealTek wrote:
>>>
>>
>> for the above to work right, you will need to loop through the mysql result 
>> set
>> one time.  Placing all the results in to one large array.  Then you can loop
>> through the array as many times as needed.
>>
>> What you will probably find is that you can sort all the data into the proper
>> order withing your SQL statement.  Then display the data looping through the
>> mysql result set once without having to create the additional array 
>> mentioned in
>> my first statement above.
> 
> 
> 
> Thanks Jim,
> 
> I will look into how to make this work with arrays...
> 
> more beginner questions...
> 
> - as stated -  when I try to loop through 2x ...
> - the table 2 only shows 1 record..
> - Q: ... like the query number row number? needs to be reset - but how?
> 
> So is it not possible to loop more than 1x? Is that why we are loading into 
> array?

Exactly

> 
> 
> --
> Thanks,
> Dave - DealTek
> deal...@gmail.com
> [db-11]


-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

--- End Message ---
--- Begin Message ---
On Fri, 16 Dec 2011 23:53:46 -0500, Eric Butera wrote:

>To all the people who responded to this thread:
>It is 2011 - please stop writing code like this.
>
>To the OP:
>I'm glad you're asking questions and realizing you're not happy with
>your current abilities and suspect there's a better way.  I've read
>the replies in this thread and feel a bit let down.  Use a templating
>language - yes, I understand that is what php is for - but I won't go
>into it.  You should not be echoing, printing, or any other method of
>concatenating html dealing with escaping quotes inside your php logic
>code.  Please separate your concerns.

By and large, I agree with you, and certainly it is a good idea to raise
that with the OP since they were asking about recommended practices.
However, "should not" is not the same as "don't" and there are still
places where this is appropriate. 

Here's a chunk of real code from a WordPress plugin, taken from the
class that extends the Page admin form to provide a button for selecting
a custom associated image:

echo <<<HTML
<input type="button" class="button mmm-admin-image-button"
data-fieldname="$fieldName" value="Select"
id="mmm-admin-image-button-$fieldName" />
<input type="hidden" name="$fieldName" value="$fieldValue" />
<span id="mmm-admin-image-caption-$fieldName"
>{$this->getAttachmentCaption($fieldValue)}</span>
<input type="button" class="button mmm-admin-clear-button"
data-fieldname="$fieldName" value="Clear" />

HTML;

HEREDOC provides me a way to do templating in a simple way within a
WordPress hook function. The purpose of the hook is to provide a way to
augment WordPress Page editing by adding some HTML; sending a small
fragment of HTML to the browser is its function. I gain nothing by
spinning that fragment out to a template file somewhere.

For other hook functions (e.g. representing shortcodes), it is
beneficial to use templating, and I do so -- but horses for courses.

>Not sure what that means?  That's OK!  If you want to move forward,
>look up how modern frameworks deal with this issue using their views
>or template views.  You don't have to use a framework if you do not
>want to, that's perfectly fine.  If it works, it works.  But in the
>end, it the separation of logic and html is essential to code
>maintenance.

Applause! :)
-- 
Ross McKay, Toronto, NSW Australia
"The chief cause of problems is solutions" -Eric Sevareid

--- End Message ---
--- Begin Message ---
Hi.
As I was writing a php code last week, I struggled with a function that was
supposed to convert Unicode code to a character. At last I wrote this
function:

function unicodeToChr($param) {

        $a =json_decode('{"t":"'.$param.'"}');

        return $a->t;
}


In this function the $param should be something like '\u0627'. I want to
know if it's acceptable to use this function? Does exist any better
solution to do that?
Thanks.

--- End Message ---
--- Begin Message ---
there is iconv to do this. you should better check that.



On Sun, Dec 18, 2011 at 11:33 AM, Ali Asghar Toraby Parizy <
aliasghar.tor...@gmail.com> wrote:

> Hi.
> As I was writing a php code last week, I struggled with a function that was
> supposed to convert Unicode code to a character. At last I wrote this
> function:
>
> function unicodeToChr($param) {
>
>        $a =json_decode('{"t":"'.$param.'"}');
>
>        return $a->t;
> }
>
>
> In this function the $param should be something like '\u0627'. I want to
> know if it's acceptable to use this function? Does exist any better
> solution to do that?
> Thanks.
>

--- End Message ---

Reply via email to