【问题标题】:PHP - Count string for occurrence which is printed [closed]PHP - 打印的计数字符串[关闭]
【发布时间】:2014-03-13 13:24:27
【问题描述】:

我有以下...

 <a href='Count.php?count=Red'><

但是我要怎么做才能让它不止一种颜色呢?

 <a href='Count.php?count=Red, Green, Blue'><

没有用,也没有用 && 替换 , 有什么想法吗?

这是执行计数的 PHP 代码:

<?php 
$lineNumber=0; 
$handle = fopen('shapeStorage.txt', 'r'); 
if ($handle) 
    { 
    while (($line = fgets($handle))!== false) 
        { 
        $lines[$lineNumber] = $line; 
        $lineNumber++; 
        } 
    } 
else 
    { 
    echo 'error, error, high voltage';
    } 
for($n=0; $n<sizeof($lines); $n++)
    { 
    if(strstr($lines[$n], $_GET["count"])) $colourCount++; 
    } 
echo '<h2>There are '.$colourCount.' '.$_GET["count"].' shape(s) stored within the site.</h2>'; 
?>

【问题讨论】:

  • 您是否要计算每种颜色出现的次数或颜色的数量?
  • 它在一个文本文件中出现了多少次。
  • 你的意思是要计算每个单词在文件中出现的次数?
  • 那么href的作用是什么?是指定要计数的颜色吗?
  • 因为它需要它到另一个页面来告诉你有多少他们是特定颜色的......我会在 php 方面发布

标签: php url count query-string


【解决方案1】:

我认为这是你应该做的:

<a href='Count.php?count=Red-Green-Blue'>

现在在 PHP 中,您可以使用连字符将其拆分,如下所示:

$Colours = explode("-", $_GET["count"]);

现在在您的代码中使用 $Colours 数组。

这不是您的计数算法的解决方案,但它是您关于如何包含多种颜色的问题的答案。

希望这会有所帮助。

【讨论】:

  • 我在哪里放置 $colours 行?
【解决方案2】:

变量名使用数组语法:

<a href='Count.php?count[]=Red&count[]=Green&count[]=Blue'>

然后在 PHP 端:

$colors = $_GET['count'];
print_r($colors);
/* OUTPUTS
Array
(
    [0] => Red
    [1] => Green
    [2] => Blue
)
*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多