簡體   English   中英

PHP變量構建HTML表

[英]PHP variables building HTML table

因此,我將在此向你們中的一位天才尋求幫助,因為我確定有人將能夠弄清楚我想做什么。

我有一個html表

    <body>
<div id="style">
<div class="disktext">
</p>
 <table width='190' border='0' cellspacing='0' cellpadding='1'>
        <tr><td BGCOLOR='#0066FF'><font color='white'>Disk Space</font></td></tr>
        <tr onclick="toggleNextRow(this)"><td BGCOLOR='#FFFFFF'`enter code here`><?php echo $drive1;?>\<?= $freeSpaceC ?> (of <?= $totalSpaceC ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidthC ?>px"><?= $diskStatusC->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive2;?>\<?= $freeSpace1 ?> (of <?= $totalSpace1 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth1 ?>px"><?= $diskStatus1->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive3;?>\<?= $freeSpace2 ?> (of <?= $totalSpace2 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth2 ?>px"><?= $diskStatus2->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive4;?>\<?= $freeSpace3 ?> (of <?= $totalSpace3 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth3 ?>px"><?= $diskStatus3->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive5;?>\<?= $freeSpace4 ?> (of <?= $totalSpace4 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth4 ?>px"><?= $diskStatus4->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive6;?>\<?= $freeSpace5 ?> (of <?= $totalSpace5 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth5 ?>px"><?= $diskStatus5->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive7;?>\<?= $freeSpace6 ?> (of <?= $totalSpace6 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth6 ?>px"><?= $diskStatus6->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
    </table>

林試圖做的是使用一個php變量來定義是否應顯示某些行“內置”

所以下面是我正在使用的if語句,它可以工作,但是我無法弄清楚如何在這種狀態下使用它。

    if ($drive1visible > "0") {
    try {
}

我想把桌子的線

<tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive2;?>\<?= $freeSpace1 ?> (of <?= $totalSpace1 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth1 ?>px"><?= $diskStatus1->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>

並使其僅在滿足可變條件的情況下才創建該行

 if ($drive1visible > "0") {
        try {
<tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive2;?>\<?= $freeSpace1 ?> (of <?= $totalSpace1 ?>)</td></tr>
            <tr class="hiddenRow">
                <td bgcolor='#FFFFFF'>
                    <div class="disk">
                        <div class="used" style="width: <?= $barWidth1 ?>px"><?= $diskStatus1->usedSpace() ?>%&nbsp;</div>
                    </div>
                </td>
            </tr>
}

但這行不通...

希望有人可以幫助我,或者告訴我這不可能,非常感謝!

更新-

這是我要修改的整個文件。希望這會有所幫助。

<!--Force IE6 into quirks mode with this comment tag-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/white.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="black" href="css/black.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="white" href="css/white.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="blue" href="css/blue.css" />

<!--This script should appear below your LINK stylesheet tags -->

<script src="styleswitch.js" type="text/javascript">

/***********************************************
* Style Sheet Switcher v1.1- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/

</script>
<script type="text/javascript">
window.onload=function(){
var formref=document.getElementById("switchform")
indicateSelected(formref.switchcontrol)
}
</script>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js" type="text/javascript"></script>
    <style>
        .hiddenRow { display: none; }
    </style>
    <script>
        function toggleNextRow(row) {
            var nextRow = $(row).next();
            nextRow.toggle();
        }
    </script>
</head>

<body>
<?php include 'config.php';?>
<?php

require_once 'DiskStatus.class.php';

if ($drive1visible > "0") {
    try {
  $diskStatusC = new DiskStatus("$drive1");

  $freeSpaceC = $diskStatusC->freeSpace();
  $totalSpaceC = $diskStatusC->totalSpace();
  $barWidthC = ($diskStatusC->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
} 
}

try {
  $diskStatus1 = new DiskStatus("$drive2");

  $freeSpace1 = $diskStatus1->freeSpace();
  $totalSpace1 = $diskStatus1->totalSpace();
  $barWidth1 = ($diskStatus1->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
}
try {
  $diskStatus2 = new DiskStatus("$drive3");

  $freeSpace2 = $diskStatus2->freeSpace();
  $totalSpace2 = $diskStatus2->totalSpace();
  $barWidth2 = ($diskStatus2->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
}

try {
  $diskStatus3 = new DiskStatus("$drive4");

  $freeSpace3 = $diskStatus3->freeSpace();
  $totalSpace3 = $diskStatus3->totalSpace();
  $barWidth3 = ($diskStatus3->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
}

try {
  $diskStatus4 = new DiskStatus("$drive5");

  $freeSpace4 = $diskStatus4->freeSpace();
  $totalSpace4 = $diskStatus4->totalSpace();
  $barWidth4 = ($diskStatus4->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
}

try {
  $diskStatus5 = new DiskStatus("$drive6");

  $freeSpace5 = $diskStatus5->freeSpace();
  $totalSpace5 = $diskStatus5->totalSpace();
  $barWidth5 = ($diskStatus5->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
}

try {
  $diskStatus6 = new DiskStatus("$drive7");

  $freeSpace6 = $diskStatus6->freeSpace();
  $totalSpace6 = $diskStatus6->totalSpace();
  $barWidth6 = ($diskStatus6->usedSpace()/100) * 150;

} catch (Exception $e) {
  echo 'Error ('.$e->getMessage().')';
  exit();
}

?>
<body>
<div id="style">
<div class="disktext">
</p>
 <table width='190' border='0' cellspacing='0' cellpadding='1'>
        <tr><td BGCOLOR='#0066FF'><font color='white'>Disk Space</font></td></tr>
        <tr onclick="toggleNextRow(this)"><td BGCOLOR='#FFFFFF'><?php echo $drive1;?>\<?= $freeSpaceC ?> (of <?= $totalSpaceC ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidthC ?>px"><?= $diskStatusC->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive2;?>\<?= $freeSpace1 ?> (of <?= $totalSpace1 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth1 ?>px"><?= $diskStatus1->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive3;?>\<?= $freeSpace2 ?> (of <?= $totalSpace2 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth2 ?>px"><?= $diskStatus2->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive4;?>\<?= $freeSpace3 ?> (of <?= $totalSpace3 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth3 ?>px"><?= $diskStatus3->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive5;?>\<?= $freeSpace4 ?> (of <?= $totalSpace4 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth4 ?>px"><?= $diskStatus4->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive6;?>\<?= $freeSpace5 ?> (of <?= $totalSpace5 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth5 ?>px"><?= $diskStatus5->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
        <tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive7;?>\<?= $freeSpace6 ?> (of <?= $totalSpace6 ?>)</td></tr>
        <tr class="hiddenRow">
            <td bgcolor='#FFFFFF'>
                <div class="disk">
                    <div class="used" style="width: <?= $barWidth6 ?>px"><?= $diskStatus6->usedSpace() ?>%&nbsp;</div>
                </div>
            </td>
        </tr>
    </table>
</div>
</div>
</body>

</html>

您以錯誤的方式嘗試了..嘗試一下..

<?php if ($drive1visible > 0) { ?>
<tr onclick="toggleNextRow(this)"><td bgcolor='#FFFFFF'><?php echo $drive2;?>\<?= $freeSpace1 ?> (of <?= $totalSpace1 ?>)</td></tr>
            <tr class="hiddenRow">
                <td bgcolor='#FFFFFF'>
                    <div class="disk">
                        <div class="used" style="width: <?= $barWidth1 ?>px"><?= $diskStatus1->usedSpace() ?>%&nbsp;</div>
                    </div>
                </td>
            </tr>
<? php  } ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM