Forum Moderators: coopster

Message Too Old, No Replies

Help, a function that thinks post variable is array

         

PieSocial

10:19 am on May 1, 2005 (gmt 0)

10+ Year Member



Hello, what a fantastic site. I hope you can help me with my problem.

I have a function findvalue();

//Function: Find value
function findvalue($element, $key=0) {
$count = count($_POST[$element]);

if($count==1) {
//Single item only
$value = $_POST[$element];
}

elseif($count>1) {
//Element is in an array
$value = $_POST[$element][$key];
}

//Return value
if($value) {

if(get_magic_quotes_gpc()) {
//No slashes needed
$result = "'".trim($value)."'";
}

else {
//Add slashes
$result = "'".addslashes(trim($value))."'";
}

}

else {
//No result to return
$result = 'NULL';
}

return $result;
}

The script is supposed to find the value specified and return it for insertion into a database. I can pass a post variable name into it and optionally a key for post array instances.

I hope I am being clear.

I am trying to call findvalue() like this:

$name = findvalue('name');
$textbase = findvalue('textbase');
$query = "UPDATE helpitems SET name=".$name.", textbase=".$textbase." WHERE id=".$itemid;

Here is my HTML.

<form action="" method="POST" id="edititems">
<table>
<tr>
<td>Help item for: <?php print($titlehere);?><br /><br />
<input type="text" name="name" class="wide" value="New Name" maxlength="45" /><br /><br />
<textarea name="textbase" rows="4" class="wide"></textarea><br /><br /></td>
</tr>
</table>
</form>

The problem is that it grabs and inserts textbase properly but it doesn't work with any input type=text's. It returns the word "Array" instead of what's in the input box. Can you tell me what I am doing wrong?

Please it's been driving me crazy all day.

When it's inside a foreach loop like I'm using it on a different page it works perfectly:

foreach($_POST['itemid'] as $i => $itemid) {
$name = findvalue('name', $i);
$textbase = findvalue('textbase', $i);
...

That's weird, and when I try and use just zero instead of $i it doesn't work. Same problem where the individual input type=text box is passed as an array. Please help.

PieSocial

9:46 pm on May 1, 2005 (gmt 0)

10+ Year Member



Posting solution:

Change

$count = count($_POST[$element]);

if($count==1) {
//Single item only
$value = $_POST[$element];
}

elseif($count>1) {
//Element is in an array
$value = $_POST[$element][$key];
}

to

if(!is_array($_POST[$element])) {
//Single item only
$value = $_POST[$element];
}
else {
//Already in an array
$value = $_POST[$element][$key];
}

mcibor

10:25 pm on May 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi!
you forgot one else:

<?php
function findvalue($element, $key=0)
{
$post = $_POST[$element];
$count = count($post);
if($count==1)
{
//Single item only NOT TRUE! it may still be an array!
if(is_array($post)) $value = $post[0];
else $value = $post;
}
elseif($count>1)
{
//Element is in an array
$value = $post[$key];
}
else return false;

//Return value
if($value)
{
if(get_magic_quotes_gpc())
{
//No slashes needed
$result = "'".trim($value)."'";
}
else
{
//Add slashes
$result = "'".addslashes(trim($value))."'";
}
}
else {
//No result to return
$result = 'NULL';
}

return $result;
}


Best regards
Michal Cibor

PieSocial

4:17 pm on May 2, 2005 (gmt 0)

10+ Year Member



I suppose this is as good a place as any to ask this question. Since the $key value is defaulted to zero when the array is run, wouldn't it be cleaner and still run the same way if we used this instead:

//Function: Find value
function findvalue($element, $key=0) {
if(!is_array($_POST[$element])) {
//Single item only
$value = $_POST[$element];
}
else {
//Already in an array
$value = $_POST[$element][$key];
}

//Return value
blah blah blah blah...
}

Or else does a value of zero in $key get passed as NULL into the array and thusly not work when we ask for $_POST[$element][$key]?

Thanks!

mcibor

8:09 pm on May 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will work.

However remember not to exceed the key value in calling the function - no idea what it might return. I didn't write the best code, I just corrected your, so you will know in the future where to look to find errors:) .

Good luck!
Michal Cibor