Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my working search code

What do i do to organise results?

What I have tried:

PHP
<?php
    $query = $_GET['query']; 
     
    $min_length = 3;
     
    if(strlen($query) >= $min_length){
         
        $query = htmlspecialchars($query); 
         
        $query = mysql_real_escape_string($query);
         
        $raw_results = mysql_query("SELECT * FROM avenuedata
            WHERE (`idno` LIKE '%".$query."%') OR (`username` LIKE '%".$query."%')") or die(mysql_error());
         
        if(mysql_num_rows($raw_results) > 0){
             
            while($results = mysql_fetch_array($raw_results)){
				echo "<h3>".$results['idno']."</h3>".$results['username']."<br>".$results['mmc']."<br>";
			}	
		}
		else{ 
			echo "No results";
		}
		
	}
	else{ 
		echo "Minimum length is ".$min_length;
	}
?>
Posted
Updated 31-Dec-16 21:07pm
v2

Apart from solution 1, to organize data into table, you have to create it programmatically in your read row while loop, see one of my previous answer[^].
 
Share this answer
 
v2
Comments
Thomas Daniels 1-Jan-17 3:15am    
+5; I had overlooked that part :-)
Peter Leow 1-Jan-17 3:23am    
Thank you, and Happy New Year to you!
Thomas Daniels 1-Jan-17 3:24am    
Same to you!
Member 12929725 2-Jan-17 4:12am    
Hello.. The code did not work. Syntax Error.. Unexpected Else
Peter Leow 2-Jan-17 4:25am    
What code? My example does not have "Else". If you encounter a new problem after trying, post it as a new question.
Firstly, you appear to use string concatenation to form your queries. Don't do this. You are vulnerable to SQL Injection[^]. Use prepared statements and parameterized queries.[^]. (Edit: ah, you are using mysql_real_escape_string. That should be fine then... nevertheless prepared statements and parameterized queries would still be my recommendation. Aside from safety you also have easier-to-read queries.)

About the organizing of the search results... you probably want to use to sort the rows using "ORDER BY"[^]. If you want to sort the rows based on the username, add this to your query:
ORDER BY username
... or if you want to sort them by "idno":
ORDER BY idno
 
Share this answer
 
v3
Comments
Member 12929725 1-Jan-17 3:08am    
I would like to tabulate search results. Currently they simply display as list. i would like to tabulate. And thank you for the advice. i will put it into action.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900