Heb een script met zoekfunctie en wil graag melding laten zien dat gevraagde er niet

Status
Niet open voor verdere reacties.

richard5000

Gebruiker
Lid geworden
24 mrt 2012
Berichten
161
Hoi,

IK heb een script met zoekfunctie erin en als iemand in het zoekveld iets intoetst wat niet in de tabel staat, dan verbergt hij de rijen, op zich niets mis mee, maar dan zie je alleen maar de headers van de tabel.

Ik zou graag willen dat als iemand iets intoetst en hij geen rijen laat zien er een melding komt van zoiets als "Sorry dat wat u zoekt komt niet voor...." eneventueel daaronder een button met reset, of ga terug, in ieder geval iets waarmee hij de tabel weer volledig laat zien.

Ik weet niet waar ik dit in de PHp moet plaatsen, kan iemand mij hiermee helpen...?

Hieronder het script:

PHP:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>TJou Pagina naam</title>
        <style type="text/css">
		/*----------Text Styles----------*/
		.ws12 {font-size: 16px;}
		.wpmd {font-size: 13px;font-family: 'Arial';font-style: normal;font-weight: normal;}
		/*----------Para Styles----------*/
		DIV,UL,OL /* Left */
		{
		margin-top: 1px;
		margin-bottom: 0px;
		}
		TABLE
		{ border- collapse: separate; border-spacing: 10px 3px;} // Eerste is voor ruimte tabel links, tweede is voor ruimte tussen rijen
		{ text-allign: center }
        </style>
          
		<script language="javascript" type="text/javascript">
        //define the table search as an object, which can implement both functions and properties
        window.tableSearch = {};
 
        //initialize the search, setup the current object
        tableSearch.init = function() {
            //define the properties I want on the tableSearch object
            this.Rows = document.getElementById('data').getElementsByTagName('TR');
            this.RowsLength = tableSearch.Rows.length;
            this.RowsText = [];
            
            //loop through the table and add the data to
            for (var i = 0; i < tableSearch.RowsLength; i++) {
                this.RowsText[i] = (tableSearch.Rows[i].innerText) ? tableSearch.Rows[i].innerText.toUpperCase() : tableSearch.Rows[i].textContent.toUpperCase();
            }
        }
 
        //onlys shows the relevant rows as determined by the search string
        tableSearch.runSearch = function() {
            //get the search term
            this.Term = document.getElementById('textBoxSearch').value.toUpperCase();
            
            //loop through the rows and hide rows that do not match the search query
            for (var i = 0, row; row = this.Rows[i], rowText = this.RowsText[i]; i++) {
                row.style.display = ((rowText.indexOf(this.Term) != -1) || this.Term === '') ? '' : 'none';
            }
        }
 
        //runs the search
        tableSearch.search = function(e) {
            //checks if the user pressed the enter key, and if they did then run the search
            var keycode;
            if (window.event) { keycode = window.event.keyCode; }
            else if (e) { keycode = e.which; }
            else { return false; }
            if (keycode == 13) {
                tableSearch.runSearch();
            }
            else { return false; }
        }
		</script>
 </head>
 
<body onload="tableSearch.init();">
<table border="0" cellpadding="2" cellspacing="0">
	<tbody>
        <tr>
			<td>
			<input type="text" size="25" maxlength="1000" value="" id="textBoxSearch" onkeyup="tableSearch.search(event);" />
			<input type="button" value="Search" onclick="tableSearch.runSearch();" /> 
			</td>
        </tr>
    </tbody>
</table>

<div class="wpmd">
<div id="table" style="position:absolute; overflow:hidden; left:9px; top:50px; width:960px; height:300px; z-inde	x:0">
<table border="0" >
<tr valign=top>
		<td width=132 bgcolor="#FF0000">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Naam</B></font></div>
            </div>
            </td>
 
        <td width=132>
            </td>
                
            <td width=31>
            </td>
                
        <td width=132 bgcolor="#0000FF">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Achternaam</B></font></div>
            </div>
            </td>
            
        <td width=132>
            </td>
<tr><tr>
</tr>  
<tr valign=top>
        <td width=132 bgcolor="#FF0000">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Woonplaats</B></font></div>
            </div>
            </td>
 
        <td width=132 bgcolor="#FF0000">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Postcode</B></font></div>
            </div>
            </td>
                
            <td width=31>
            </td>
                
        <td width=132 bgcolor="#0000FF">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Art</B></font></div>
            </div>
            </td>
            
        <td width=132 bgcolor="#0000FF">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Type</B></font></div>
            </div>
            </td>
</tr>
 
<tbody id="data">
        <?php
        $con = mysql_connect('localhost','Username','Password');
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }
 
        mysql_select_db("testdatabase",$con);
 
        $result = mysql_query("SELECT * FROM testtabel");
 
        while ($row = mysql_fetch_array($result)) {
        ?>
        
        <tr>
            <td STYLE="text-align: center;"> <?php echo $row['naam']; ?></td>
			<td><?php echo $row['woonplaats']; ?></td>
			<td width=31></td>
			<td STYLE="text-align: center;"> <?php echo $row['naam2']; ?></td>
			<td><?php echo $row['woonplaats2']; ?></td>
        </tr>
        
        <?php 
        }
        
mysql_close($con);
        ?>
        </tbody>
        </table>
        </div>
    </body>
</html>
 
dat zal dan zo iets worden:

PHP:
$aantalrows = mysql_num_rows($result);

if($aantalrows == 0)
{
?>
<tr><td colspan="5">Geen resultaten gevonden</td>
<?php
}else{
        while ($row = mysql_fetch_array($result)) {
        ?>
        
        <tr>
            <td STYLE="text-align: center;"> <?php echo $row['naam']; ?></td>
            <td><?php echo $row['woonplaats']; ?></td>
            <td width=31></td>
            <td STYLE="text-align: center;"> <?php echo $row['naam2']; ?></td>
            <td><?php echo $row['woonplaats2']; ?></td>
        </tr>
        
        <?php 
        }
}
?>
 
Hoi klaaspeter, heb het zo overgenomen en werkt helaas niet, in ieder geval bedankt voor je moeite in deze.

Groet,
Richard
 
Heb het erin gezet en als je iets intoets wat er niet is laat hij geen boodschap zien, sorry.. Heb het even online gezet op http://www.rvvweb.nl/RJ/9000

Was zelf uiteraard ook aan het stoeien geweest, omdat ik ook nog steeds bezig ben om te zorgen dat hij alleen zoekt in de eerste twee kolommen, want als je intoets 4838 laat hij een rij zien waar dit getal voorkomt uit de 3e kolom.

Was nog aan het kijkenof ik iets kon met:
$data = mysql_query("SELECT * FROM testtabel WHERE upper($field) LIKE'%$find%'");

Het blijft een lastig searchscript, maar alles wat ik op het net zie is een search engine die resutaat laat zien 9heb ik wel werkend gekregen) maar niet een die in mijn tabel zoekt en de rijen filtert.

Maar oke ik blijf proberen en zoeken, toch..

Groet,
Richard
 
heb je keken krijg inderdaad niet de melding te zien vreemd

anders post even je gehele code nu.
 
Oke, had begrepen dat ik het moest zoeken in "passing $_GET variables", maar hier heb ik helemaal geen kaas van gegeten, maar eerst de code::

PHP:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>TJou Pagina naam</title>
        <style type="text/css">
		/*----------Text Styles----------*/
		.ws12 {font-size: 16px;}
		.wpmd {font-size: 13px;font-family: 'Arial';font-style: normal;font-weight: normal;}
		/*----------Para Styles----------*/
		DIV,UL,OL /* Left */
		{
		margin-top: 1px;
		margin-bottom: 0px;
		}
		TABLE
		{ border- collapse: separate; border-spacing: 10px 3px;} // Eerste is voor ruimte tabel links, tweede is voor ruimte tussen rijen
		{ text-allign: center }
        </style>
          
		<script language="javascript" type="text/javascript">
        //define the table search as an object, which can implement both functions and properties
        window.tableSearch = {};
 
        //initialize the search, setup the current object
        tableSearch.init = function() {
            //define the properties I want on the tableSearch object
            this.Rows = document.getElementById('data').getElementsByTagName('TR');
            this.RowsLength = tableSearch.Rows.length;
            this.RowsText = [];
            
            //loop through the table and add the data to
            for (var i = 0; i < tableSearch.RowsLength; i++) {
                this.RowsText[i] = (tableSearch.Rows[i].innerText) ? tableSearch.Rows[i].innerText.toUpperCase() : tableSearch.Rows[i].textContent.toUpperCase();
            }
        }
 
        //onlys shows the relevant rows as determined by the search string
        tableSearch.runSearch = function() {
            //get the search term
            this.Term = document.getElementById('textBoxSearch').value.toUpperCase();
            
            //loop through the rows and hide rows that do not match the search query
            for (var i = 0, row; row = this.Rows[i], rowText = this.RowsText[i]; i++) {
                row.style.display = ((rowText.indexOf(this.Term) != -1) || this.Term === '') ? '' : 'none';
            }
        }
 
        //runs the search
        tableSearch.search = function(e) {
            //checks if the user pressed the enter key, and if they did then run the search
            var keycode;
            if (window.event) { keycode = window.event.keyCode; }
            else if (e) { keycode = e.which; }
            else { return false; }
            if (keycode == 13) {
                tableSearch.runSearch();
            }
            else { return false; }
        }
		</script>
 </head>
 
<body onload="tableSearch.init();">
<form>
<table border="0" cellpadding="2" cellspacing="0">
	<tbody>
		<tr>
			<td>
			<input type="text" size="25" maxlength="1000" value="" id="textBoxSearch";" />
			<input type="button" value="Search" onclick="tableSearch.runSearch();"/> 
			<input type="button" value="Reset!" onclick="getElementById('textBoxSearch').value = ''; tableSearch.runSearch();" />
			</td>
        </tr>
    </tbody>
</table>
</form>

<div class="wpmd">
<div id="table" style="position:absolute; overflow:hidden; left:9px; top:50px; width:960px; height:300px; z-inde	x:0">
<table border="0" >
<tr valign=top>
		<td width=132 bgcolor="#FF0000">
            <div align=center><font color="#FFFFFF" class="ws12"><B>HIL</B></font></div>
            </div>
            </td>
 
        <td width=132>
            </td>
                
            <td width=31>
            </td>
                
        <td width=132 bgcolor="#0000FF">
            <div align=center><font color="#FFFFFF" class="ws12"><B>POW</B></font></div>
            </div>
            </td>
            
        <td width=132>
            </td>
<tr><tr>
</tr>  
<tr valign=top>
        <td width=132 bgcolor="#FF0000">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Art.Nr.</B></font></div>
            </div>
            </td>
 
        <td width=132 bgcolor="#FF0000">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Type</B></font></div>
            </div>
            </td>                
            <td width=31>
            </td>
                
        <td width=132 bgcolor="#0000FF">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Art.Nr.</B></font></div>
            </div>
            </td>
            
        <td width=132 bgcolor="#0000FF">
            <div align=center><font color="#FFFFFF" class="ws12"><B>Type</B></font></div>
            </div>
            </td>
</tr>
 
<tbody id="data">
        <?php
        $con = mysql_connect('localhost','Username','Password');
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }
 
        mysql_select_db("testdatabase",$con);
 
        $result = mysql_query("SELECT * FROM testtabel");
 
        $aantalrows = mysql_num_rows($result);
		
		if($aantalrows == 0)
		{
		?>
		<tr><td colspan="5">Geen resultaten gevonden</td>
		<?php
		}else{
		
		while ($row = mysql_fetch_array($result)) {
        ?>
        
        <tr>
            <td STYLE="text-align: center;"> <?php echo $row['naam']; ?></td>
			<td><?php echo $row['woonplaats']; ?></td>
			<td width=31></td>
			<td STYLE="text-align: center;"> <?php echo $row['naam2']; ?></td>
			<td><?php echo $row['woonplaats2']; ?></td>
        </tr>
        
		<?php 
        }
		}
    ?>
 
Laatst bewerkt:
Jij heb ook in elk onderdeel een topic hé:-)

Deze vraag heeft niets met sql te maken omdat het zoek gedeeltje met javascript werkt.
Je moet dus ergens in dat gedeelte iets invoeren dat de rijen in de tabel telt.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan