PHP垂直表实验

时间:2017-11-03 01:30:17

标签: php mysql

-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Nov 03, 2017 at 02:26 AM
-- Server version: 10.1.21-MariaDB
-- PHP Version: 5.6.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `tobedev`
--

-- --------------------------------------------------------

--
-- Table structure for table `tb_product`
--

CREATE TABLE `tb_product` (
  `id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,
  `price` double(12,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tb_product`
--

INSERT INTO `tb_product` (`id`, `name`, `price`) VALUES
(1, 'Product 1', 100.00),
(2, 'Product 2', 100.00),
(3, 'Product 3', 100.00),
(4, 'Product 4', 100.00),
(5, 'Product 5', 100.00),
(6, 'Product 6', 100.00),
(7, 'Product 7', 100.00),
(8, 'Product 8', 100.00),
(9, 'Product 9', 100.00),
(10, 'Product 10', 100.00),
(11, 'Product 11', 100.00),
(12, 'Product 12', 100.00),
(13, 'Product 13', 100.00),
(14, 'Product 14', 100.00),
(15, 'Product 15', 100.00);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tb_product`
--
ALTER TABLE `tb_product`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tb_product`
--
ALTER TABLE `tb_product`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

嘿盖伊是否有人可以帮我修复错误?我是php的新手 这是代码。

当我试图运行它时,它给了我错误 警告:第61行的D:\ xampp \ htdocs \ vertical \ index.php中的非法字符串偏移'否'

注意:未初始化的字符串偏移量:在第61行的D:\ xampp \ htdocs \ vertical \ index.php中为0

警告:第62行的D:\ xampp \ htdocs \ vertical \ index.php中的非法字符串偏移'name'

注意:未初始化的字符串偏移量:D在第62行的D:\ xampp \ htdocs \ vertical \ index.php中

警告:第63行的D:\ xampp \ htdocs \ vertical \ index.php中的非法字符串偏移'价格'

注意:未初始化的字符串偏移量:在第63行的D:\ xampp \ htdocs \ vertical \ index.php中为0

我仍然无法弄清楚问题,因为我拿的代码的网站是泰国语言

<?php
$user = 'root';
$pass = 'ACCDBMS17';

try {
    $dbh = new PDO('mysql:host=localhost;dbname=tobedev', $user, $pass);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

$stm = $dbh->prepare("SELECT name, price FROM tb_product LIMIT 14");
$stm->execute();

$no        = 1;
$data     = array();
while($result = $stm->fetch(PDO::FETCH_ASSOC)){
    $result['no'] = $no++;
    $data[] = $result;
}
$stm = null;
//echo '<pre>',print_r($data,true), '</pre>';

$count         = count($data); //หาจำนวนข้อมูลทั้งหมด
$row_num     = 5; //กำหนดจำนวนแถวที่ต้องการ
$col_num     = ceil($count/$row_num); //จำนวนคอลัมน์ที่จะแสดง


//สร้างอาร์เรย์ชุดใหม่
$new_data = array();
for($i=0;$i<$row_num;$i++) //วนลูปตามจำนวนแถวที่ต้องการ
{
    $row = array();
    $index = $i;
    for($j=0;$j<$col_num;$j++) //วนลูปตามจำนวนคอลัมน์ที่คำนวณได้
    {
        $row[] = isset($data[$index]) ? $data[$index] : '';
        $index += $row_num;//เลื่อนลำดับข้อมูลของคอลัมน์ต่อไป
    }
    $new_data[] = $row;
}
//echo '<pre>',print_r($new_data,true), '</pre>';

//สร้างตารางข้อมูล
echo '<table border=1 cellpadding=5 cellspaceing=1>';

echo '<tr>';
for($i=0;$i<$col_num;$i++) //วนลูปตามจำนวนคอลัมน์ที่คำนวณได้
{
    echo "<th>No</th>
    <th>Name</th>
    <th>Price</th>";
}
echo '</tr>';

foreach ($new_data as $row) //วนลูปแสดงข้อมูลในอาร์เรย์ชุดใหม่ทีละแถว
{
    echo "<tr>";
    foreach ($row as $data) //แสดงอาร์เรย์แต่ละชุด
    {
        echo "<td style='background-color:#eeeeee'>$data[no]</td>
        <td>$data[name]</td>
        <td>$data[price]</td>";
    }
    echo "</tr>";
}
echo '</table>';

$dbh = null;
?>

1 个答案:

答案 0 :(得分:0)

问题是您的键根本没有初始化。 如果要使用它,则必须声明变量 $result = array('no'=>1, 'anotherno'=>2, 'etc'=>3); 那么您将能够访问$ result [&#39; no&#39;]但是如果尝试访问它 $ result [&#39; no&#39;] = 1您将获得非法的字符串偏移错误