Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array becoming plain string 1

Status
Not open for further replies.

Ghodmode

Programmer
Feb 17, 2004
177
NZ
I'm trying to create an array, populating it with values from the $_POST variable. $_POST has all of the values I expect, but they're not becoming part of the array as I expect.

Code:
$review['prod_id'] = addSlashes( $_POST['prod_id'] );
$review['user_id'] = addSlashes( $_SESSION['current_user'] );
$review['rating']  = $_POST['rating'];
$review['review']  = htmlSpecialChars( addSlashes($_POST['review']) );

if ( empty($review['rating']) || ! is_numeric($review['rating']) ) {
    ?><pre style="text-align: left;"><?php
    echo "POST: \n";
    print_r( $_POST );

    echo "review: \n";
    print_r( $review );
    ?></pre><?php
}

output:
Code:
POST: 
Array
(
    [star] => images/16-star-hot.gif
    [nostar] => images/16-star-cold.gif
    [prod_id] => ASIAJEWELS
    [rating] => 1
    [review] => terrible
)
review: 
terrible

Can anyone tell what I'm doing wrong?

Thank you.

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Your script does not seem to show (and looks like that could be the problem) but did you create the $review array with the
Code:
$review = array();
If you don't do that, PHP cannot know that $review is supposed to be an array and stores values inside as if it is a string.
 
Vragabond:
First, I want to say thank you. You were right and it works now. But I'm still confused...

According to the documentation "If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array." (PHP: Arrays - Manual). So, that's why I didn't think to try it. I've used arrays in PHP before without first declaring them. I thought the array() declaration was usually only used to clear the values from an existing array.

I'm relatively new to PHP, but it seemed like a short learning curve.

I think this may also solve another problem I had in which I was trying to pass this same array to a function. I gave up and just used it as a global variable.

Thank you.

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Eureka!! I've got it!"

It is not necessary to declare an array with array() first.

The problem I was having occurred because of a stupid mistake I made in a function that used the array.

My $review array is populated by a table named review which also has a field named review (can you see it coming yet?). My form has an ID of review_form and each of the fields in the form have an id that matches a field in the table. Another function processes the form's data (from $_POST) and populates the $review array with associative keys. One of these keys is 'review'. Later, this array is processed in another function which is supposed to update the table with data from the $review array. For ease of use, I create a variable within the function for each named key. One of these becomes $review.

So, that was the problem. I was re-defining $review to be just a string. I've since changed the form and all of the keys to 'text'.

A clue to this that I wasn't picking up on happened later in the program. I was referencing one of the values of the array and all that was output was the first character of the last value in the array: the review. So, if the review started with the letter 'T', the output only had 'T'. I think this is because it was accessing the string as an array and resolving the index I gave to 0. The numeric value of any quoted string is 0, right?

For example:

Code:
<?php
$info['name'] = 'Ghodmode';
$info['rank'] = 'Benevolent Dictator for Life';

$info = "This is a different variable!!";
?>

<h3><?= $info['name'] ?></h3>
<h3><?= $info['rank'] ?></h3>
<h3><?= $info ?></h3>

produces this output:
Code:
T
T
This is a different variable!!

[thumbsup2]

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top