473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once("dbconfig.php");
  3. ?>
  4. <?php
  5. $output = '';
  6.  
  7. if(isset($_POST['search'])) {
  8.         $searchq = $_POST['search'];
  9.         $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
  10.  
  11.         $query = mysql_query("SELECT * FROM tbl_ficheiros WHERE id_ficheiro LIKE '%$searchq%' OR ficheiro LIKE '%$searchq%'") or die("Erro na procura...");
  12.         $count = mysql_num_rows($query);
  13.         if($count == 0){
  14.             $output = 'Sem resultados na procura...';
  15.         }else{
  16.             while($row = mysql_fetch_array($query)) {
  17.                 $idficheiro = $row['id_ficheiro'];
  18.                 $nficheiro = $row['ficheiro'];
  19.                 $id = $row['id_ficheiro'];
  20.  
  21.                 $output .= '<div>ID = '.$idficheiro.' // nome = '.$nficheiro.'</div>';
  22.             }
  23.         }
  24.     }
  25. ?>
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  27.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  28. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
  29. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  30. <head>
  31.     <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  32.     <title>Lista de Relatórios</title>
  33. <link rel="stylesheet" type="text/css" media="screen" href="style.css" />
  34. <style type="text/css">
  35. #container #intro #pageHeader h1 {
  36.     color: #CC0000;
  37.     font-size: 2em;
  38. }
  39. #container #intro #pageHeader h1 {
  40.     font-size: 4em;
  41.     color: #666666;
  42. }
  43. #body form p label {
  44.     color: #666666;
  45. }
  46. body table tr th {
  47.     color: #FFFFFF;
  48. }
  49. body table tr td {
  50.     color: #FFFFFF;
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <br/><h1>Lista de Relatórios</h1><br/>
  56.   <form id="frm_lista" method="post">
  57.   <div align="center">
  58.   <table width="40%" border="1">
  59.     <tr>
  60.       <td width="15%">Ficheiro</td>
  61.       <td width="12%">Tipo</td>
  62.       <td width="19%">Tamanho(KB)</td>
  63.       <td width="20%">Ver</td>
  64. <?php 
  65.     $sql="SELECT * FROM tbl_ficheiros";
  66.     $result_set=mysql_query($sql);
  67.     while($row=mysql_fetch_array($result_set))
  68.     {
  69.         ?>
  70.   </tr>
  71.  
  72.     <tr>
  73.       <td><?php echo $row['ficheiro'] ?></td>
  74.       <td><?php echo $row['tipo'] ?></td>
  75.       <td><?php echo $row['tamanho'] ?></td>
  76.       <td><a href="uploads/<?php echo $row['ficheiro'] ?>" target="_blank">Ver Ficheiros</a></td>
  77.   </tr>
  78.     <?php
  79.     }
  80.     ?>
  81.    }
  82. </table>
  83.  
  84.  
  85.  
  86.   </div>
  87.  
  88.   <form action="search_teste.php" method="post">
  89.       <input type="text" name="search" placeholder="Nome do Relatório..." />
  90.     <input type="submit" class="btn" value=">>"  />
  91.   </form>
  92.  
  93.   <?php print("$output");?>
  94.  
  95.   <p>&nbsp;</p>
  96.   <p><a href="novo_relatorio.php">
  97.   <input align="middle" name="btn_novo" type="submit" class="btn" id="button" value="Novo Relatório" />
  98.   </a></p>
  99. <p><a href="relatorios.php"><input align="middle" name="btn_voltar" type="submit" class="btn" id="button" value="Voltar" /></a></p>
  100. </body>
  101. </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 ;)
Jun 29 '15 #1
1 1345
You can change your code like as below :
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['search'])) {
  2.         $searchq = $_POST['search'];
  3.         $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
  4.         $query = mysql_query("SELECT * FROM tbl_ficheiros WHERE id_ficheiro LIKE '%$searchq%' OR ficheiro LIKE '%$searchq%'") or die("Erro na procura...");
  5.     }else{
  6.       $query = mysql_query("SELECT * FROM tbl_ficheiros WHERE id_ficheiro") or die("Erro na procura...");
  7.     }
  8.  
  9.       $count = mysql_num_rows($query);
  10.       if($count == 0){
  11.           $output = 'Sem resultados na procura...';
  12.       }else{
  13.           while($row = mysql_fetch_array($query)) {
  14.               $idficheiro = $row['id_ficheiro'];
  15.               $nficheiro = $row['ficheiro'];
  16.               $id = $row['id_ficheiro'];
  17.  
  18.               $output .= '<div>ID = '.$idficheiro.' // nome = '.$nficheiro.'</div>';
  19.           }
  20.       }
  21.  
Jul 7 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: dx27s | last post by:
I want a function to take another function as a parameter, and execute it as a line of code. I put a simplified example below, just to illustrate the idea. Does anyone know the syntax to do this,...
5
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
46
by: Your Uncle | last post by:
About a month ago, Heathfield posted the peudosource for random permuting from TAOCP. It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a...
0
by: swtstrawberry | last post by:
here are the directions and below the direction is what I have so far. I need help with (B) the bisect function. I've only posted part of my program, if more information is needed please let me...
3
by: John Shell | last post by:
Hello, all. The following code results in a C2666 error (2 overloads have similar conversions). class FSVec2D { public: FSVec2D() { // code omitted }
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
2
by: =?Utf-8?B?LnBhdWwu?= | last post by:
i'm using this method with subs + it works well, but with functions, they don't return a value. what am i doing wrong? Private Delegate Function listview_items_countCallback() As Integer ...
2
by: Anish G | last post by:
Hi, I am new to working on threading. I need to implement multithreading for calling four functions (each functions are having parameters) in different threads so that i can improve the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.