Posted by Randy Johnson on 02/11/05 05:38
I like to do this:
foreach( $row as $key=>$val) {
$$key = $row[$key];
}
John Holmes wrote:
> Ben Edwards (lists) wrote:
>
>> I have the following code;_
>>
>> $sql = "select * from text where id = '$id' ";
>>
>> $row = fetch_row_row( $sql, $db );
>>
>> $img_loc = $row["img_loc"];
>> $text_type = $row["text_type"];
>> $seq = $row["seq"];
>> $rec_type = $row["rec_type"];
>> $section = $row["section"];
>> $code = $row["code"];
>> $repeat = $row["repeat"];
>>
>> $description = $row["description"] );
>> $text = $row["text"] );
>>
>>
>> Was wondering if there was a clever way of doing this with foreach on
>> $row. something like
>>
>> foreach( $row as $index => value ) {
>> create_var( $index, $value );
>> }
>>
>> So the question is is there a function like create_var which takes a
>> string and a value and creates a variable?
>
>
> I think extract() is what you're after. Just note that the quickest way
> to do things isn't always the best.
>
> Is $description going to be shown to the users? How are you protecting
> it so that it doesn't contain HTML/JavaScript code? Is any of this going
> into form elements? How are you preparing it so that a double/single
> quote doesn't end the form element?
>
> Just pointing out that although you can quickly create the variables
> you're after with extract(), there may still be other things you should
> do before you use them.
>
> Also, why are you wasting processing time creating $text instead of just
> using $row['text'] in whatever output you have? It's a few more
> characters to type, but it takes away one level of possible confusion
> when you come back to this code later.
>
> Just my $0.02.
>
[Back to original message]
|