I have a list of files from my database. I’m looking for a way to query them. but I’m receiving an error, which is… it only display the first item if my input matches the first. This is the form
JavaScript
x
5
1
<form>
2
<input type="text" placeholder="Filter" aria-label="Search"
3
onkeyup="myFunction()" id="myInput">
4
</form>
5
below is the php
JavaScript
1
19
19
1
<?php
2
$query = "SELECT classname,cdate FROM classnametb`";
3
$res = mysqli_query($db, $query);
4
while ($r = mysqli_fetch_assoc($res)){
5
$classname = $r['classname'];
6
$classdate = $r['cdate'];
7
$classdate = date("m/d/y", strtotime($classdate));
8
?>
9
<div id="myUL">
10
<b>
11
<a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
12
class="btn btn-primary btn-icon" style="width: 20% !important;">
13
<span class="btn-inner--text"><?php echo($classname . '(' . $classdate . ')'); ?></span>
14
15
</a>
16
<?php } ?>
17
</b>
18
</div>
19
and I have the js here
JavaScript
1
17
17
1
function myFunction() {
2
var input, filter, div, b, a, i, txtValue;
3
input = document.getElementById("myInput");
4
filter = input.value.toUpperCase();
5
div= document.getElementById("myUL");
6
b = div.getElementsByTagName("b");
7
for (i = 0; i < b.length; i++) {
8
a = b[i].getElementsByTagName("a")[0];
9
txtValue = a.textContent || a.innerText;
10
if (txtValue.toUpperCase().indexOf(filter) > -1) {
11
b[i].style.display = "";
12
} else {
13
b[i].style.display = "none";
14
}
15
}
16
}
17
Advertisement
Answer
You are generating mutliple myUl
divs inside while loop and you have not close </b>
& </div>
.Instead change your php code like below :
JavaScript
1
17
17
1
<div id="myUL"> <!--add opening tag here-->
2
<?php
3
$query = "SELECT classname,cdate FROM classnametb`";
4
$res = mysqli_query($db, $query);
5
while ($r = mysqli_fetch_assoc($res)){
6
//some codes..
7
?>
8
<b>
9
<a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
10
class="btn btn-primary btn-icon" style="width: 20% !important;">
11
<span class="btn-inner--text"><?php echo($classname . '(' . $classdate . ')'); ?></span>
12
13
</a>
14
</b> <!--close `b` tag-->
15
<?php } ?>
16
</div> <!--close `div` tag-->
17
Now, you can simply your js code by using .forEach
to iterate through your b
tag and hide/show it when match found.
Demo Code (Added comment in code) :
JavaScript
1
16
16
1
function myFunction() {
2
var input, filter;
3
input = document.getElementById("myInput");
4
filter = input.value.toUpperCase();
5
//loop through `b` tag
6
document.querySelectorAll('#myUL b').forEach(function(el) {
7
el.style.display = 'none'; // hide it
8
});
9
//loop through div -> `a` tag
10
document.querySelectorAll('#myUL a.btn-icon').forEach(function(el) {
11
//compare
12
if (el.textContent.toUpperCase().indexOf(filter) > -1) {
13
el.closest('b').style.display = "block"; //if match show that div
14
}
15
})
16
}
JavaScript
1
3
1
b {
2
display: block
3
}
JavaScript
1
31
31
1
<input type="text" placeholder="Filter" aria-label="Search" onkeyup="myFunction()" id="myInput">
2
<div id="myUL">
3
<b>
4
<a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
5
class="btn btn-primary btn-icon" style="width: 20% !important;">
6
<span class="btn-inner--text">abc</span>
7
8
</a>
9
</b>
10
<b>
11
<a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
12
class="btn btn-primary btn-icon" style="width: 20% !important;">
13
<span class="btn-inner--text">abc13</span>
14
15
</a>
16
</b>
17
<b>
18
<a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
19
class="btn btn-primary btn-icon" style="width: 20% !important;">
20
<span class="btn-inner--text">assee</span>
21
22
</a>
23
</b>
24
<b>
25
<a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
26
class="btn btn-primary btn-icon" style="width: 20% !important;">
27
<span class="btn-inner--text">apple</span>
28
29
</a>
30
</b>
31
</div>