Hi all,
I'm trying to develop a form for a user to provide some information that will be used when rendering a block. One of the things I would like a user to provide is the number of posts they would like to see in the block, i.e., any integer value. This is being collected in a menu.inc file. I am collecting the value using #default_value and the #type is a textfield.

With this information, I would like to pass it to another function in my .module file using the variable_get function. However, when I do this and use the value, it appears it is being passed as a string and not as an integer (or number). As a result, my function simply displays all the posts available and not the number of posts as provided by the user.

how do you collect an integer from a user using the form field or is there a way to convert a string to an integer? Sorry if this is a no-brainer, I'm still learning...

Thanks

Comments

nevets’s picture

How are you getting the lists of posts?

yaworsk’s picture

its actually a list of twitter tweets that I am pulling from their site... They are populating properly because if in my .module file I put in an actual number like 2, it will show 2 tweeks but if I replace the 2 with the variable, i get all the tweets.

nevets’s picture

Without seeing the code I can only guess. My guess is the value is 0 and twitter treats that as return all tweets. You could add something like this to you code to check the value.

drupal_set_message("The value is ($YOUR_VARIABLE_NAME_HERE)");

The message will show in the message area of the page.

yaworsk’s picture

great, that was something i was struggling with, how to just have the value returned - I was short on time when working this morning, I was going to use the dsm function through devel. I'll try this out tonight when i am back at home and will post the results.

Thanks for the help.

jaypan’s picture

To convert a string to an integer, you pass the datatype ahead of it.

$var = "3";
if(3 === $var)
{
  echo '$var is an integer<br />';
}
else
{
  echo '$var is a string<br />';
}
$var = (int) $var; // we convert the datatype here
if(3 === $var)
{
  echo '$var is an integer<br />';
}
else
{
  echo '$var is a string<br />';
}

note: The three equal signs (===) checks not just if the value is equal, but also if it is the same datatype.

Contact me to contract me for D7 -> D10/11 migrations.

yaworsk’s picture

Thanks for the help both of you. After I used the print statement I found out the variable was empty. After re-examining my code, I was using the variable_get() in the wrong location and it was outside of my function. After relocating it, my code is working properly.

Thanks very much for the help! As a side note, I didn't have to convert the value of the number, drupal just accepted it but its good to know how to change the data type, thanks for the info.

Pete