繁体   English   中英

使用MySQL将数据从MySQL导入数组和表中?

[英]Importing data from MySQL into array and into table with PHP?

我有一个数据库,其中包含捐赠者信息,然后有PHP页面可以提取该数据并将其显示在捐赠者的特定类别下。 我使用的代码如下:

// 10,000 DONORS
echo "<p><b>$10,000 cont.</b></p>";

$sql        = "SELECT * FROM donor WHERE DonationAmount = 10000 AND Category  = '1' or DonationAmount = 10000 AND Category IS NULL ORDER BY LastName ASC LIMIT         10000 OFFSET 6";
$result     = mysqli_query($conn, $sql);
$i          = 0;
$total_rows = $result->num_rows;
echo "<table><tr>";
if (mysqli_num_rows($result) > 0) {
   // output data of each row
   while ($row = mysqli_fetch_assoc($result)) {

       // test if the DisplayName field is empty or not
       echo "<td>";
       if (empty($row['DisplayName'])) {
           // it's empty!
           if (empty($row['FirstName'])) {
               echo $row['LastName'];
           }

           else {
               echo $row["LastName"] . ", " . $row["FirstName"];
           }

       } else {
           // Do stuff with the field
           echo $row["DisplayName"] . "";
       }
       echo "</td>";
       $i++;
       if ($i % 2 == 0 && $i != $total_rows) {
           echo "</tr><tr>";
       }

   }
 } else {

} echo "</tr></table>";

我的问题如下:现在,名称以某种方式插入表中,使得读取的顺序为left-> right-> left-> right等。我需要像正常列表一样显示它们并从上到下读取,并告诉到(计数器?)滚动到下一列。 我怎样才能做到这一点? 这是页面外的预览

编辑:尝试此代码:

$sql = "SELECT * FROM donor WHERE DonationAmount = 1000 AND Category = '1' or     DonationAmount = 1000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
$i          = 0;
$count = 0;
$total_rows = $result->num_rows;
$halfsize = $total_rows / 2;
$FirstArray = array();
$SecondArray = array();


while ($row = mysqli_fetch_assoc($result)){
    while ($count <= $halfsize){
    $FirstArray[] = $row['DisplayName'];
    //echo "$FirstArray[$count]";
    $count++;
    }
    $SecondArray[] = $row['DisplayName'];
    //echo "$SecondArray[$count]";

}



echo "<table>";
for($j=0; $j<count($FirstArray); $j++){
   echo "<tr><td>". $FirstArray[$j] . "</td><td>" . $SecondArray[$j] . "</td>    </tr>";
}
echo "</table>";

我只是为第一列(“第一数组”)的所有字段获得了相同的字段(第一结果),然后第二列(SecondArray)包含了所有结果。

我不会为此使用表格。 可能还有其他一些CSS技巧。 但是出于疑问,我在回答。

创建两个数组。

使用mysqli_num_rows获取结果集的大小,然后将其除以二,然后遍历第一个数组并将它们移动到array1的一半大小。 然后将其保留到array2中。

最后,您将获得类似的内容

Array 1    Array 2
-------    -------
[0] Row 1  [0] Row 4
[1] Row 2  [1] Row 5
[2] Row 3

然后遍历它并填写您的html表。 (控制它们为空)

for($i=0; $i<count($array1); $i++){
   echo '<tr><td>'.$array1[$i].'</td><td>'.$array2[$i].'</td></tr>';
}

您的问题不是您的PHP或MySQL,而是您的输出处理。

因此,有一系列数据正在转储到HTML上,您需要对其进行排序以使其看起来不错。 两列以表格格式从左到右读取。

<table>标记非常适合此操作,虽然大多数人都讨厌table标记,但它对于表数据很有用。

值得注意的是,由于CSS / HTML的灵活性和适应性,这不是唯一正确的方法,而是可以解决此问题的众多方法之一

方法:

您有一个while循环一次输出一个单位,在这种情况下,一个单位是<td></td>块中的名称。 这就是您的单位:

while {
      print "unit";
} 

因此,如果要安排两列,则需要告诉while循环以区分第一列和第二列,这可以通过检测计数器(每个信号发送为+1)是奇数还是偶数来完成

您可以使用PHP中的模数除数来执行此操作:

while {
    counter++
    if (counter%2) == 1 ){
        //odd number. 
     }
} 

因此,总而言之,并给您一个基本的例子:

 $output = "";
 $rowsTotal = mysqli_num_rows($result); //from your original code. 
 $counter = 0;
   while ($row = mysqli_fetch_assoc($result)) {
       $counter++; //plus 1 each itteration
       if (($counter%2) == 1){
            /// is odd so open the row of the table.
           $output .= "<tr>";
       }
       $output .= "<td>";
       if (empty($row['DisplayName'])) {
           // it's empty!
           if (empty($row['FirstName'])) {
               $output .= $row['LastName'];
           }
           else {
               $output .= $row["LastName"] . ", " . $row["FirstName"];
           }

       } 
       else {
           // Do stuff with the field
           $output .= $row["DisplayName"] . "";
       }
       $output .= "</td>";
       if (($counter%2) == 0){
          // if even so close the row of the table. 
          $output .= "</tr>";       
       }
      // special case: If there area total of odd number of outputs
      if($counter == $rowsTotal && $counter%2 == 1){
           // so the last counter value is an odd number so force closure 
           // of </tr> (or add a blank space)
            $output .= "<td>&nbsp;</td></tr> 
            }
   } //end while. 



...
print "<table>";
print $output;
print "</table>";

我不想深入研究CSS本身的创意,但是您可以使用CSS细化并改进一些层叠样式表,以改善核心HTML输出设置,但是上面是您可以使用的排序方法的粗略概述为脚本模拟基本级别的智能,以在所需的布局中输出名称。

祝好运。

CSS范例:

table td {
    background-color:#000;
    color: #fff;
    font-size:1.5rem;
    text-align:left;
    width:49%; /* 50% sometimes causes overflow, so set a tiny bit smaller */
    padding:0.25rem;
    margin:0;
    box-sizing: border-box;
}

版本2

从注释中,OP需要SQL查询的结果是两列,而不是两行。 这意味着表(行结构)不适用于此输出,因此我们应该使用更自定义的CSS / div标签集:

布局将是两列,每列大约49%的宽度,然后每一列包含块元素。然后,需要将该块Blob分成两半,并添加一个分隔符,以从一个长的一列中生成两个较短的列。

块元素在foreach循环中输出,并且一旦满足中点,则将插入额外的HTML代码以将元素分成第二个<div> 在我看来,这有点古怪,但是确实可以。

要求:结果行的预定义计数器。 一个很容易产生。 并且已经使用MySQL ORDER BY以预定的方式对SQL查询进行了排序。

半伪代码:

$array = SQL data result.
$counter = counter of how many array rows are returned.
$divisorCount = ceil($counter /2);
$foreachCount = 1
foreach ($array as $row){
    $foreachCount++;
    $block[] = "<div class='block'>".$row['data']."</div>\n";
    if($foreachCount > $divisorCount){
         $foreachCount = 0; //reset, as this will not be true again.
         end($block);        
         $key = key($block); //most recent block array reference.
         $block[$key] .= "</div><div class='column'>"; //the insert.
    }

}
unset($row,$key,$foreachCount,$divisorCount); //tidyup.

上面的代码生成了一堆数组元素,每个名称一个。 有一个插入的拆分器,该拆分器在第一列结束并在第二列开始,最后,我们将整个块数组包装到最终的<div>包装器中。

$output = "<div class='tableContainer'>
 <div class='column'>".implode($block)."</div>
 </div>";  

上面的输出将是第一列的打开器,但将是第二列的关闭器。 在输出中,我们现在具有包含在Div Container元素中的所有结果的两列的完整列。

以下CSS类需要进行一些调整才能正确使用,但应该是一个开始:

CSS

.block{
    line-height: 2rem;
    display:block;
    padding:0.25rem;
    box-sizing:border-box;
}
.column{
    width: 49%;
    min-width:150px; /* or whatever. */
    display:inline-block; 
}
.tableContainer{
    max-width: 800px; /* or whatever. */
    min-width: 300px; /* or whatever. */
    width:100%;
    margin:auto; //centres it. 
}

CSS会是49%而不是50%,因为框的大小可能会在不同的浏览器(尤其是Firefox)上溢出,但是$output最终将是: HTML

<div class='tableContainer'>
<div class='column'><div class='block'>My Name</div>
<div class='block'>Your Name</div>
<div class='block'>His Name</div></div>
<div class='column'><div class='block'>Their Name</div>
<div class='block'>Her Name</div>
<div class='block'>Smacked HorseBombs</div>
</div>
</div>

您也可以用display:inline-block代替float:left ,但这就是基础知识, 可以根据需要在jsfiddle上进行调整。

暂无
暂无

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

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