Reputation:
I need one help.I have multiple text field inside a div and i need to count those using PHP.I am explaining my code below.
<div class="questionshowp">
<input name="optional_0_0_ans" id="optional_0_0_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
<input name="optional_0_1_ans" id="optional_0_1_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
<input name="optional_0_2_ans" id="optional_0_2_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
</div>
<?php
?>
Here 3 input fields are present inside a div.Here i need to echo how many nos of field is present using PHP.Please help me.
Upvotes: 0
Views: 162
Reputation:
I am assuming that the HTML is not built/rendered using PHP, so then you can use the PHP built-in DOM parser.
<?php
//** Load the HTML **//
//$html = file_get_contents('http://www.url.com'); //Load the HTML from external webpage.
$html = file_get_contents(__FILE__); //Load the HTML from the current webpage.
//** Load HTML contents into DOM tree **//
$dom = new DOMDocument();
$dom->loadHTML($html);
//** Initialize DOM Parser **//
$finder = new DomXPath($dom);
//** Parse DOM for all occurences of <input> with parent <div> with class ="questionshowp' **//
$inputs = $finder->query("/html/body/div[@class='questionshowp']/input");
//** Determine how many input fields have type='text' **//
$count = 0;
foreach ($inputs as $input) {
if ($input->getAttribute('type') === 'text') {
$count++;
}
}
//** Print Results! **//
echo "Number of <input> fields where @type = 'text' is -> " .$count ."\n";
REFERENCE
http://php.net/manual/en/book.dom.php
Upvotes: 1
Reputation: 632
Keep a flag $count and increment inside the for loop
$count = 0;
for loop {
$count++
}
echo $count;
or
$count = count($listValue);
Upvotes: 0