Php 如何在每行打印数组中特定数量的对象
该程序接受用户输入的案例数量、每个案例的包装数量、每个包装的卡片数量,以及案例中罕见、罕见和传奇卡片的频率数量。它类似于“嘶嘶”测试 “客户”希望将每一组卡输出到自己的单独线路上。这就是我目前面临的问题 我还不熟悉php和面向对象编程 以下是我的全部代码:Php 如何在每行打印数组中特定数量的对象,php,Php,该程序接受用户输入的案例数量、每个案例的包装数量、每个包装的卡片数量,以及案例中罕见、罕见和传奇卡片的频率数量。它类似于“嘶嘶”测试 “客户”希望将每一组卡输出到自己的单独线路上。这就是我目前面临的问题 我还不熟悉php和面向对象编程 以下是我的全部代码: <form method="post" action="index.php"> Total Number of Cases: <input type="text" name="totNumCases" val
<form method="post" action="index.php">
Total Number of Cases:
<input type="text" name="totNumCases" value="" /><br/>
Number of Packs in Each Case:
<input type="text" name="packsInEachCase" value="" /><br/>
Base Number of Cards in Each Pack:
<input type="text" name="baseNumCards" value="" /><br/>
Frequency of "Uncommon" Cards:
<input type="text" name="freqUncommon" value="" /><br/>
Frequency of "Rare" Cards:
<input type="text" name="freqRare" value="" /><br/>
Frequency of "Legendary" Cards:
<input type="text" name="freqLegendary" value="" /><br/>
<input type="submit" name="submit" value ="Submit" /><br/>
<hr />
</form>
<?php
$cardsPerPack = $_POST['baseNumCards'];
$packsPerCase = $_POST['packsInEachCase'];
class CommonCards{
private $card;
public function __construct($card){
$this->card = 'COM';
}
public function __toString(){
return $this->card.' ';
}
public function process(CardCounter $f){
$result = $f->increment();
if($result){
$this->card = $result;
}
}
}
class CardCounter {
private $uncommonCount = 0;
private $rareCount = 0;
private$legendaryCount = 0;
public function increment(){
$this->uncommonCount++;
$this->rareCount++;
$this->legendaryCount++;
$out = '';
if($this->uncommonCount == $_POST['freqUncommon']){
$this->uncommonCount = 0;
$out.='UNC';
}
if($this->rareCount == $_POST['freqRare']){
$this->rareCount = 0;
$out.='RAR';
}
if($this->legendaryCount == $_POST['freqLegendary']){
$this->legendaryCount = 0;
$out.='LEG';
}
return $out;
}
}
$arr = array();
for($i = 1; $i <= ($cardsPerPack * $packsPerCase); $i++){
$arr[] = new CommonCards($i);//getting all cards, setting them all equal to COM. "common cards"
}
$f = new CardCounter();
foreach($arr as $commonCards){
$commonCards->process($f);//doing the 'fizzbuzz' process
echo $commonCards;// printing out the total number of cards.
}
在这里:
public function process(CardCounter $f){
$result = $f->increment();
if($result){
$this->card = $result;
}
}
但是所有的尝试都失败了
非常感谢您的帮助。您能使用嵌套的while语句吗?外部的while循环覆盖在包上,内部的while循环覆盖在每个包的卡上。我还没有找到任何可以使用while循环的东西。到目前为止:$arr=array();for($i=1;$i进程($f);//为($j=1;$j我有一个好主意,为什么它要打印10行5张卡,但我尝试的所有循环都打印出了比实际需要的更多的卡。这是因为我在循环,我正在获取和打印这些卡。知道我如何解决这个问题吗?所以你的数组是一个卡名列表,你想循环,打印吗先打印5张卡,然后换行,然后再打印5张卡,然后换行,直到你到达数组的末尾?这就是你想要做的吗?
public function process(CardCounter $f){
$result = $f->increment();
if($result){
$this->card = $result;
}
}