Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count function not working in switch

My problem here is that the count function won't work inside this switch statement, and I'm not sure why. I know there's probably a better way to do this, but I'm just trying to figure this out specifically to get it to work.

switch(count($matches[1]))
{
case count($matches[1]) = 1:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />";
break;

case count($matches[1]) = 2:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />";
break;

case count($matches[1]) = 3:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />";
break;

default:
print "Error";
}

I know that the count function and $matches work. Here are the two pages I have right now for reference. First page:

<form action="emailform.php" method="post">
<textarea rows="20" cols="20" name="template[]"></textarea>
<input type="submit" name="submit" value="Store your template here" />
</form>

<p>This is where you enter your standard email.<br />
The words that need to change every time are variables using the parenthesis {{}} i.e.
{{Customer name}}, {{item}}, {{price}}</p>

Second page:

<?php

$pattern = "/\{{2}([a-zA-Z]*)\}{2}/";

$subject = $_POST["template"];

preg_match_all($pattern, $subject, $matches);


echo "<pre>";
var_dump($matches);
echo "</pre>";

echo count($matches[1]);

?>
<form action="emailform.php" method="post">
<?php
switch(count($matches[1]))
{
case count($matches[1]) = 1:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />";
break;

case count($matches[1]) = 2:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />";
break;

case count($matches[1]) = 3:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />";
break;

case count($matches[1]) = 4:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />
$matches[1][3] <input type=\"text\" name=\"$matches[1][3]\" />";
break;

default:
print "Error";
}

?>

<input type="submit" name="submit" />
</form>
like image 438
SkillSet12345 Avatar asked Jul 12 '26 12:07

SkillSet12345


1 Answers

Try this, You have used case count($matches[1]) in all case

switch(count($matches[1]))
{
 case  1:
...

instead of

switch(count($matches[1]))
{
case count($matches[1]) = 1:

Updates:

HTML:

<form action="emailform.php" method="post">
    <textarea rows="20" cols="20" name="template">Dear {{customer}}, Your {{item}} will cost price. Thank you.</textarea>
    <input type="submit" name="submit" value="Store your template here" />
</form>

PHP in emailform.php

 <?php
  if(isset($_POST['submit'])){
    $pattern = "/\{{2}([a-zA-Z]*)\}{2}/";   

    echo $subject = $_POST["template"];     
    preg_match_all($pattern, $subject, $matches);       

    echo "<pre>";
    var_dump($matches);
    echo "</pre>";

    foreach($matches[1] as $key=>$value){           
        print $value ."<input type=\"text\" name=\"$value\" /><br />";          
    }   
  }
 ?>
like image 135
Krish R Avatar answered Jul 14 '26 03:07

Krish R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!