2015-02-12 51 views
0

确定奇怪的问题。在两种数据类型之间计算MYSQL中的字段数

表由多个字段组成。一些数据类型为int(5),并用数据类型INT(11)

所以,让我们一排...

id =>int(5) 
var1 =>int(11) 
var2 =>int(11) 
var3 =>int(11) 
var4 =>int(11) 
var5 =>int(5) 
var6 =>int(11) 
var7 =>int(11) 
var8 =>int(11) 

我怎么能算领域中的PHP BETWEEN ID(INT(5休息))和var(int(5)) 我需要返回使用php之间的字段值...但我卡住了。坏桌子设计不帮助...但我坚持下去。

完整的情况是,我需要创建一个if语句它说,输出的名称和和数据INT(5)外地的如果有任何它和下一INT之间的字段(5)包含数据

对不起......我知道!

如果运行这个至今

$sql2 = 'SHOW COLUMNS from services2'; 
    $result2 = getDBResults($sql2, $level, $testing); 

    $total = count($result2); 


    $i=2; 
    while ($i<($total-1)) 
    { 
     $data = $result2[$i]['Field']; 

     if ($result2[$i][1]=='int(5)') 
     { 
      $html .= '<h2>'.preg_replace('/(?<!\)[A-Z]/', ' $0', $result2[$i]['Field']).'</h2>'; 
     } 
     else 
     { 
      ]; 
      // "$result - This is a seperate query run previously to get the row 
      if ($result[0][$data] == 1) 
      { 
       $html .= '<p>'.preg_replace('/(?<!\)[A-Z]/', ' $0', $data).'</p>'; 
      } 
     } 
     $i++; 
    } 
+1

你打算在PHP中做这个?如果这是一个数组,然后使用一些PHP的数组函数,slice/splice相应地进行一次尝试。 – Ghost 2015-02-12 00:32:37

+0

告诉我做一个尝试就像一个chocloate防火墙一样有用。 国家我使用PHP的...所以为什么这个问题。 我已经编写了各种数组解析脚本来获取字段名称和类型以及关联的数据......我问这个问题以获得人们如何解决问题的杠杆作用! – Will 2015-02-12 00:35:08

+0

为什么?做一个尝试有什么不对?它可以犯错误,这就是为什么我们在这里帮助。从起点到终点切片,计算剩余的排除开始和结束(从数组意义上)。 – Ghost 2015-02-12 00:39:39

回答

0

我还没有机会真正测试这一点,所以不要打我了,如果你需要tweek有点平滑掉任何粗糙的边缘。

但它是一个相当标准的(如果不是完全简单的话)处理循环中列名结果数组的过程,并且只记住变量中的最后一个int(5)列名,以便您可以使用它如果您在查看下一个int(5)列类型之前已经在任何int(11)列类型中找到了一些数据。

现在我明白为什么你在解释你想要的东西时遇到困难,我在描述解决方案时遇到同样的麻烦。

无论如何,看看这段代码,看看它是否对你有任何意义。

$sql2 = 'SHOW COLUMNS from services2'; 
$columns = getDBResults($sql2, $level, $testing); 

$total = count($columns); 

$print_column = null; 
$found_data = false; 

foreach ($columns as $idx => $column) { 
    // skip first 2 columns 
    if ($idx < 3) { continue; } 

    // first time thru loop so remember the first int5 column name we saw 
    if ($column['Type'] == 'int(5)' && empty($print_column)) { 
     $print_column = $column['Field']; 
     $found_data = false; 
     continue; 
    } 

    // found the next int5 column, do we need to print anything 
    if ($column['Type'] == 'int(5)' && $found_data) { 

     // This is where you would do any output creation, 
     // I just kept it simple. 
     echo $result[0][$print_column]; 

     // reset state and the next field that might be printed. 
     $found_data = false; 
     $print_column = $column['Field']; 
     continue; 
    } 

    // This may not need to be an if, but just in case you have other field types 
    // that you are not interested in processing I made it an if. 
    if ($column['Type'] == 'int(11)' && ! empty($result[0][$column['Field']])) { 
     $found_data = true; 
    } 

} // endforeach 
相关问题