简体   繁体   English

计算两种数据类型之间MYSQL中的字段数

[英]Calculate no of fields in MYSQL between two data types

OK weird question. 好奇怪的问题。

Table consists of multiple fields. 表由多个字段组成。 Some with data type int(5) and the rest with datatype int(11) 一些具有int(5)数据类型,其余的具有int(11)数据类型

So lets take a row... 所以让我们继续...

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)

How can I count the fields in PHP BETWEEN id (int(5)) and var (int(5)) I need to return the values in the fields between using php... but Im stuck. 我如何计算PHP BETWEEN id(int(5))和var(int(5))中的字段我需要返回使用php之间的字段中的值...但我卡住了。 Bad table design doesnt help...but Im stuck with it. 糟糕的桌子设计并没有帮助...但我坚持下去。

The full scenario is that i need to create an if statement which says, output the name and and data of the int(5) field IF any of the fields between it and the next int(5) contain data 完整的场景是我需要创建一个if语句,它输出int(5)字段的名称和数据,如果它与下一个int(5)之间的任何字段包含数据

sorry... ugly I know!!! 对不起...丑陋我知道!!!

If run this so far 如果到目前为止运行

    $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++;
    }

I have not had a chance to actually test this, so dont beat me up if you need to tweek it a bit to smooth off any rough edges. 我没有机会对它进行实际测试,所以如果你需要稍微调整一下来平滑掉任何粗糙的边缘,就不要打败我。

But its a fairly standard, if not totally simple, process of processing over the column names result array in a loop and just remembering the last int(5) column name in a variable so you can use it, if and only if, you have found some data in any int(11) column types before seeing the next int(5) column type. 但它是一个相当标准的,如果不是非常简单的,处理列名称的过程在循环中产生数组,只记住变量中的最后一个int(5)列名,这样你就可以使用它,当且仅当你有在查看下一个int(5)列类型之前,在任何int(11)列类型中找到了一些数据。

Now I understand why you had trouble explaining what you wanted, I am having the same trouble describing a solution. 现在我明白为什么你在解释你想要的东西时遇到了麻烦,我在描述解决方案时遇到了同样的麻烦。

Anyway, have a look at this code and see if it makes any sence to you. 无论如何,看看这个代码,看看它是否给你任何意义。

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM