开发者

check if value exists

开发者 https://www.devze.com 2023-02-04 20:14 出处:网络
I am working in code igniter, code igniter has a function called table_exists(); where you pass it a table name, and, as expected it che开发者_C百科cks the database to see if it exists.

I am working in code igniter,

code igniter has a function called table_exists();

where you pass it a table name, and, as expected it che开发者_C百科cks the database to see if it exists.

What I want to do is,

start with tablename

if tablename exists, use tablename1, if tablename1 exists use tablename2 etc.

My question is,

what is the best way to write this?


$i = 1;
$table_name = 'table';
$table_name_test = $table_name;

while($this->db->table_exists($table_name_test)) {
    $table_name_test = $table_name . strval($i);
    $i++;
}

$tablename = $tablename_test;

Make sure you use the full $this->db->table_exists() CodeIgniter syntax


Probably something like this:

$tblname = "sometablename";
$i = 1;
$new_tblname = $tblname;
while ($this->db->table_exists($new_tblname)){
  $new_tblname = $tblname . $i++;
}
echo $new_tblname;
0

精彩评论

暂无评论...
验证码 换一张
取 消