【问题标题】:PHP table output partial output from MSSQL databasePHP表输出MSSQL数据库的部分输出
【发布时间】:2014-03-24 19:16:24
【问题描述】:

这是一个学校项目,我不知道为什么,但它只返回两列。我查询的数据库图片是:

.

自从截屏以来,我将 NULL 字段更改为 1,认为它不会打印 NULL。

这个项目的网页在my project

在建表步骤之前,我做了测试,看看数组返回了什么,这正是我所期望的。先前数据库中的 NULL 值存在空白。我在想它可能是阻止它返回其余数组的数字,但是我正在使用的命令 IIRC(如果我回忆)仅将字符串返回到数组。我可能不正确,但这是我的理解。

记住:链接的数据库图像中的 NULL 现在是 1(int 类型)。

我没有使用框架,我正在做所有内联编码(为了经验)。这是第一次使用 PHP。我查看了许多其他示例,但我似乎无法根据我自己的问题调整他们的解决方案。任何帮助将不胜感激!

编辑:我忘记打印我的测试中的数组输出。它是 (与上面几乎相同的 url,只需删除 main.php 并添加 test.php),我忘了提到 varchar 数据类型的 NULL 字段被替换为 NA 而不是我的 1编辑图像以反映这一点。

    <?php
    require_once(file is there...) // removed from example for security reasons.
    // Test message
    echo '<p>Connect please...</p>';

    try {
        //  Connection from PHP to MSSQL via PDO
        // DataBase Host
        $dbh = new PDO($dsn, $user, $password);

        //Sent/Send To Host and preparing the query to send.
        $sth = $dbh->prepare("SELECT * FROM Backbar");

        //Send that query!
        $result = $sth->execute();

        // Fetch all of the remaining rows in the result set 
        $row = $sth->fetchAll();

        // Used to move through array.
        $subarraypos = 0;

        // Start the table format extravaganza!
        print('<table border="1px">');
        print('<tr><th>ProductCompany</th>
               <th>ProductName</th>
               <th>Barcode#</th>
               <th>Color</th>
               <th>Perms</th>
               <th>Cost</th>
               <th>Volume</th>
               <th>Size</th>
               <th>Shelf Location</th>
               <th>Shelf Cap</th>
               <th>Quantity On Hand</th>
               <th>Storage Location</th>
               <th>Quantity In Storage</th>
               <th>Vendor</th>
               <th>Vendor Region</th>
               <th>Vendor Mileage</th>');
    foreach($row as $value) {
        if($subarraypos === 0) {    
            print('<tr><td>'.$value[$subarraypos].'</td>');
            $subarraypos++;
        }

        //  16 will reset the number of columns from the
        //  database so each entry is correctly shown.
        //  Else of first if(...) (Elif)

        if($subarraypos <=15) {
            print('<td>'.$value[$subarraypos].'</td>');
            $subarraypos++;
        }
        //  Else of second if(...)  (Else)
        $subarraypos = 0;
        print('</tr>');
    }
    print('</table>');
    print('<p>'.$row.'</p>');
    print('<p>'.$value.'</p>');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
echo "<p>You should see a table, unless I broke it.</p>";
unset($dbh);
?>

【问题讨论】:

  • 您正在调用 foreach 循环来循环遍历行,而不是遍历每一行的内容。

标签: php arrays sql-server html-table


【解决方案1】:

看起来您忘记循环遍历子数组了。它将调用 subarraypos = 0 和 subarraypos = 1,然后移动到表的下一行。

foreach ($row as $value) {
    print('<tr>')
    for ($subarraypos = 0; $subarraypos < 16; $subarraypos++) {
        print('<td>' . $value[$subarraypos] . '</td>')
    }
    print('</tr>')
}

【讨论】:

  • 上面的输出是什么?您是否尝试在 fetchAll() 调用后添加 print_r($row) 以查看您正在打印的数组中实际有什么?
  • 是的,我之前在测试中打印过。 This is the page。以上内容甚至没有到达“请连接...”
【解决方案2】:

我最终在另一个 for 循环(for(for())) 中使用了一个 for 循环,到目前为止,这会将所有内容打印在两个表中。我实际上更改了它而不是 PHP print(...) 中的 foreach(for())。现在它只是我制作的一个功能,它可以 100% 工作。

我现在唯一遇到的问题是输出应该通过 jQueryUI/jQuery 在选项卡式窗格中。两者都已链接 .js 和 .css 已链接,当然一次一个。如果你知道我该怎么做,URL和上面一样。(main.php)

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2012-08-31
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多