0

What I'm trying to do is function working like this:

function my_function($id){
   while($id){
      if($id = not free){
          $id=$id+'1';
      } else {
          return $id;
      }
   }
}

I'm new to php and drupal generally and can't figure out how to handle this cycle. For instance, 'if statement' should test if the id is already in database. If it is then +1 to id if not return this free id.

I'm using this to check whether the id is free or not:

$check_unique_station_id = 'SELECT field_station_id_value FROM {field_data_field_station_id} WHERE field_station_id_value = :input_id LIMIT 1';   
if (db_query($check_unique_station_id, array(':input_id'=>$input_id))->fetchField()){

}

Any help would be appreciated.

//EDIT

I guess I didn't explain it well. I have custom module and few custom fields. One of them is 'station_id' which is field for storing number (type) of station. Let's say it is a name of the station expressed in number. Here is an example of values I have in database in field_station_id:

  1. 22
  2. 45
  3. 89

Now what I want to do: when user input=22 then check if 22 already exists and if so check if exist 22+1. When input=45 then return 46 etc. Now I see that I explained it really bad. Hope that now you know what I mean.

// EDIT 2 // MY SOLUTION

I came up with this piece of code. However I was just wondering if there is some query function to do it (propably faster). As mentioned by Felix downthere (pick as the right answer) it seems that the only query function founds the maximum number in db and return it. Which is not what I want.

Here is my code:

function free_value($occupied_value){

// loads all used values from db
$query_all_occupied_values = 'SELECT field_station_value FROM {field_data_field_station_value} WHERE field_station_value >= :occupied_value ORDER BY field_station_value';
$all_occupied_values = db_query($query_all_occupied_values, array(':occupied_value'=>$occupied_value))->fetchAll();

// count number of rows of used station_values
$number_of_values = count($all_occupied_values)-1;

// finds first closest value which is not occupied in db
for($i=0; $i<=$number_of_values; $i++) {
    if($occupied_value == $all_occupied_values[$i]->field_station_value) {
        $occupied_value = $occupied_value+1;
    } else {
        break;
    }
}

$free_value = $occupied_value;

return $free_value;

}

6
  • Hello and welcome. Would you mind telling us what you are trying to accomplish? Drupal uses next free ID by default in all typical situations already.
    – Mołot
    Commented Mar 31, 2014 at 8:14
  • Hi. I have a custom module that adds my custom content with some fields. One of the field is 'station_id' which I want to test whether is already use or not. Is it enough? I can describe it more if neccesary.
    – hajnyon
    Commented Mar 31, 2014 at 8:18
  • But if you are using Field API to manage your fields, it will never be needed. And if you are not, you can make ID an auto-increment field in database and again, searching for next free value will never be needed. So I still don't know what and why you are doing.
    – Mołot
    Commented Mar 31, 2014 at 8:28
  • I'm little confused now. Let's have an example: User wants to add 'station_id' number 33. But when 33 is already used before I want to check which next number is free and get it to the user. How can I manage it with Field API?
    – hajnyon
    Commented Mar 31, 2014 at 8:32
  • I could use somthing like this but still I want to offer to user next free id which can be used.
    – hajnyon
    Commented Mar 31, 2014 at 8:43

1 Answer 1

2

This will select the next free ID:

$next_free_id = db_query("SELECT MAX(field_name) + 1 FROM table_name")->fetchField();
1
  • Thanks for your answer. Please see edited question. I can't try it now but I suppose that your querry returns maxed value. In example I wrote in edited question it would return 89+1. (if I'm not wrong)
    – hajnyon
    Commented Mar 31, 2014 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.