Need help with functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DreamOn
    New Member
    • Jun 2015
    • 6

    Need help with functions

    Hi guys. Need some help with some functions. I'm using a table that lists all the records in my db table but i did a search form and i have been told that for me to do what i want i would have to call one function while i search and one while i list all records. I will show the full code of the page where the two functions are:

    Code:
    <?php
    include_once("dbconfig.php");
    ?>
    <?php
    $output = '';
    
    if(isset($_POST['search'])) {
    		$searchq = $_POST['search'];
    		$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    		
    		$query = mysql_query("SELECT * FROM tbl_ficheiros WHERE id_ficheiro LIKE '%$searchq%' OR ficheiro LIKE '%$searchq%'") or die("Erro na procura...");
    		$count = mysql_num_rows($query);
    		if($count == 0){
    			$output = 'Sem resultados na procura...';
    		}else{
    			while($row = mysql_fetch_array($query)) {
    				$idficheiro = $row['id_ficheiro'];
    				$nficheiro = $row['ficheiro'];
    				$id = $row['id_ficheiro'];
    				
    				$output .= '<div>ID = '.$idficheiro.' // nome = '.$nficheiro.'</div>';
    			}
    		}
    	}
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    	<title>Lista de Relatórios</title>
    <link rel="stylesheet" type="text/css" media="screen" href="style.css" />
    <style type="text/css">
    #container #intro #pageHeader h1 {
    	color: #CC0000;
    	font-size: 2em;
    }
    #container #intro #pageHeader h1 {
    	font-size: 4em;
    	color: #666666;
    }
    #body form p label {
    	color: #666666;
    }
    body table tr th {
    	color: #FFFFFF;
    }
    body table tr td {
    	color: #FFFFFF;
    }
    </style>
    </head>
    <body>
    <br/><h1>Lista de Relatórios</h1><br/>
      <form id="frm_lista" method="post">
      <div align="center">
      <table width="40%" border="1">
        <tr>
          <td width="15%">Ficheiro</td>
          <td width="12%">Tipo</td>
          <td width="19%">Tamanho(KB)</td>
          <td width="20%">Ver</td>
    <?php 
    	$sql="SELECT * FROM tbl_ficheiros";
    	$result_set=mysql_query($sql);
    	while($row=mysql_fetch_array($result_set))
    	{
    		?>
      </tr>
        
        <tr>
          <td><?php echo $row['ficheiro'] ?></td>
          <td><?php echo $row['tipo'] ?></td>
          <td><?php echo $row['tamanho'] ?></td>
          <td><a href="uploads/<?php echo $row['ficheiro'] ?>" target="_blank">Ver Ficheiros</a></td>
      </tr>
        <?php
    	}
    	?>
       }
    </table>
      
      
    
      </div>
      
      <form action="search_teste.php" method="post">
      	<input type="text" name="search" placeholder="Nome do Relatório..." />
        <input type="submit" class="btn" value=">>"  />
      </form>
      
      <?php print("$output");?>
      
      <p>&nbsp;</p>
      <p><a href="novo_relatorio.php">
      <input align="middle" name="btn_novo" type="submit" class="btn" id="button" value="Novo Relatório" />
      </a></p>
    <p><a href="relatorios.php"><input align="middle" name="btn_voltar" type="submit" class="btn" id="button" value="Voltar" /></a></p>
    </body>
    </html>
    This is the full code. Now as you see on top i have a search by filter, in this case by file name (ficheiro) and ID (id_ficheiro). And a bit below i have one that show all records. I need to wrap this into two different functions. One that runs while no search has been done and one that is called if some search has been made. Anyone can help? Any more info or detail needed just ask. Thanks in advance ;)
  • Bharat383
    New Member
    • Aug 2011
    • 93

    #2
    You can change your code like as below :
    Code:
    if(isset($_POST['search'])) {
            $searchq = $_POST['search'];
            $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
            $query = mysql_query("SELECT * FROM tbl_ficheiros WHERE id_ficheiro LIKE '%$searchq%' OR ficheiro LIKE '%$searchq%'") or die("Erro na procura...");
        }else{
          $query = mysql_query("SELECT * FROM tbl_ficheiros WHERE id_ficheiro") or die("Erro na procura...");
        }
    
          $count = mysql_num_rows($query);
          if($count == 0){
              $output = 'Sem resultados na procura...';
          }else{
              while($row = mysql_fetch_array($query)) {
                  $idficheiro = $row['id_ficheiro'];
                  $nficheiro = $row['ficheiro'];
                  $id = $row['id_ficheiro'];
    
                  $output .= '<div>ID = '.$idficheiro.' // nome = '.$nficheiro.'</div>';
              }
          }

    Comment

    Working...