2016-07-15 33 views
-2

如果我試圖顯示一個表(我用while循環完成),但也顯示一個計數。我添加另一個while循環嗎?還是單獨循環?我會怎麼做?我需要計算表演的數量(我得到了這份工作),但它不會計算阿什維爾的表演數量。我如何自己定位這個變量?程序中可以有多個while(!feof)循環嗎?

> <?php  print ("<h1>Upcoming Performances in 2015</h1>");  print 
> ("<table border =\"1\">");  print("<tr><th align = 
> \"left\">Date</th><th align = \"left\">Venue</th><th align = 
> \"left\">City</th><th align = \"right\">Ticket Price</th></tr>"); 
> 
>  $count = 0;   $ashevilleCount = 0;  $eventFile = 
> fopen("performances.txt", "r");  $schedule = fgets($eventFile); 
> 
>  
>    while(!feof($eventFile))  {   list($date, $venue, $city, $ticketPrice) =   explode(":", $schedule); 
>      print("<tr><td>$date</td>");   print("<td>$venue</td>");   print("<td>$city</td>");   print("<td>$ticketPrice</td>"); 
>      $schedule = fgets($eventFile); 
>     } 
>  
>       for($count = 1; $count <= 5; $count = $count + 1)  {   $total = fgets($eventFile);    $count = $count + $total; 
>     } 
>    if ($city == Asheville) 
>    $ashevilleCount = $ashevilleCount + $count; 
>  
> 
>  
>  
>  
>     fclose($eventFile); 
>    print ("</table>"); 
> 
>  print ("<p class=\"alert\">Lower cost venues are marked with 
> *</p>");  print ("<p>NUMBER OF PERFORMANCES: $count</p>");  print ("<p>NUMBER OF PERFORMANCES IN ASHEVILLE: $ashevilleCount</p>"); 
>  
>  
>    
> 
> ?> 
+3

你錯過括號'{}'你的周圍,如果/ ELSEIF塊 – j08691

回答

0

您需要查看您的if聲明。

if($condition === true){ 
    //executes if $condition is true 
} elseif($condition === 1) { 
    //executes if $condition is 1 
} elseif($condition === 2 || $condition === 3){ 
    //executes if $condition is 2 OR condition is 3 
} elseif($condition === 4 && $otherCondition !== "foo"){ 
    //executes if $condition is 4 AND $otherCondition is NOT "foo" 
} else { 
    //executes if no other statements are true 
} 

這段代碼:

elseif 
($charType == human or dwarf and $goldSpent <= 10) 
$supplyTokens = $_POST['supplyTokens'] + 10; 

需要看起來像:

} elseif(($charType=="human" || $charType=="dwarf") && $goldSpent <= 10) { 
     $supplyTokens = $_POST['supplyTokens'] + 10; 
} 

記住:

  • || = 「或」
  • && = 「和」
  • test = "test" - 確保您的字符串用引號括起來

參見:

0

這是您的代碼清理。做了什麼:

  • 改變了所有的print()命令級聯的echo秒。
  • 固定的條件語句
    • 你並不需要在elseif s到檢查$goldSpent <= 10因爲你已經檢查了由$goldSpent > 10
    • 是我個人比較喜歡||&&,而不是orand
  • 添加大括號{}

要考慮:是否有這些$_POST值是空的

  • ,會發生什麼?
<?php 

$charName = $_POST['charName']; 
$charType = $_POST['charType']; 
$healthTokens = $_POST['healthTokens']; 
$expTokens = $_POST['expTokens']; 
$supplyTokens = $_POST['supplyTokens']; 

$goldSpent = $healthTokens/10 + $expTokens/2 + $supplyTokens/25; 

if ($goldSpent > 10) { 
    echo "<h1>HEY THERE, $charName!</h1>" . 
     "<p>YOU SPENT MORE GOLD THAN YOU HAVE!</p>" . 
     "<p>GO BACK AND TRY THAT AGAIN - YOU HAVE 10 GOLD PIECES..</p>"; 
} elseif ($charType == 'elf') { 
    $healthTokens = $_POST['healthTokens'] + 5; 
} elseif ($charType == 'wizard') { 
    $expTokens = $_POST['expTokens'] + 2; 
} elseif ($charType == 'human' || $charType == 'dwarf') { 
    $supplyTokens = $_POST['supplyTokens'] + 10; 
} 

$totalGold = 10; 
$goldLeft = $totalGold - $goldSpent; 

echo "<h1>You have created $charName the $charType!</h1>" . 
    "<p>$charName has <strong>$healthTokens</strong> health tokens," . 
    "<strong>$expTokens</strong> experience tokens, and" . 
    "<strong>$supplyTokens</strong> supply tokens.</p>" . 
    "<p>You received some bonus tokens! :)</p>" .       
    "<p>$charName has spent <strong>$goldSpent</strong> gold pieces, " . 
    "and has <strong>$goldLeft</strong> gold pieces left.</p>"; 
+0

當我嘗試這個我得到的錯誤所有的地方。我在這門課上,因爲它對我的學位(在設計)的要求哈哈我有零的願望編程。我只是不明白這一點。我不知道回聲是什麼......我們沒有討論過它。這是第8章,即多選,嵌套,AND和ORS。所以我很確定我們必須使用我原來的結構。 – Sylvia585

+0

我最後忘了''''。你得到什麼錯誤? – mulquin

+0

好的,沒關係......他們是我的錯......但是即使金額超過了10,它仍然在做所有的計算......我如何得到它只是顯示錯誤信息? – Sylvia585

相關問題