Problem with user search[solved]

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Foofighter
Posts: 121
Joined: Sun Mar 04, 2012 1:52 pm

Problem with user search[solved]

Post by Foofighter »

Hi guys,
got some problems with my usersearch.

see here the script:
i tried a few things out so there a some lines commentet out..

Code: Select all


<?php
ob_start();
session_start();
include ('header.php');
if(!isset($_SESSION['uid'])){
    //include ('safe.php');
    echo "Sie müssen eingeloggt sein um diese Seite zu sehen!";
    header("Location:login.php");
}else{
?>

    <section id="middle">
        <div id="container">
            <div id="content">
<table border="2" cellpadding="5" cellspacing="1" width="80%">
        <tr>
            <td colspan="2"><b>Suche</b></td>
        </tr>
        <tr>
        <td>
        <form name="form2" method="post" action="#">
        <table border="1" cellpadding="5" cellspacing="1" width="100%">
        <tr>
            <td colspan="4" style="height:18px;">
            <div style="width:100%;height:100%;overflow:hidden;">
            Bitte gewünschten Suchbegriff eingeben.
            </div>
            </td>            
        </tr>
        <tr>
            <td><strong>Spielername</strong></td>
               <td colspan="2" valign="middle" align="center" style="height:20px; width:365px;">
            <div style="width:100%;height:100%;overflow:hidden;">
            <input name="suche" type="text" id="suche" size="30">
            </form>
            </div>
            </td>
            <td style="height:25px;">
            <div style="width:100%;height:100%;overflow:hidden;">
            <input type="submit" name="searchname" value="suchen" >
            </div>
            </td>
        </tr>
        </table>
        </table>
<p id="p_red">     
<?php

$tbl_name="users_valid";
$tbl1_name="planets";
$i = 0;
// values sent from form
if($_POST)
{
$suche=protect($_POST['suche']);        

//Kontrollstruktur für suche
if(isset($_POST['suche'])){
    
 if($suche == ""){
        echo "Bitte Suchbegriff einsetzen!";
    }else{

/*
$i = 0;    
$daten = mysql_query("SELECT * FROM `users_valid` WHERE `username` LIKE '%".$suche."%'");  
$row=mysql_fetch_array($daten);

if ($daten){
    */

$abfrage = "Select * FROM users_valid WHERE username LIKE '%".protect($suche)."%'";
$ergebnis = mysql_query($abfrage); 

/*
if (!$row[$i] = mysql_fetch_assoc($ergebnis)) 
{
//die ('Keine Ergebnisse zu dieser Suche');
echo "Spieler nicht gefunden.";

}
*/
/*elseif($row[$i] = mysql_fetch_assoc($ergebnis) < 1){ 

print "Spieler nicht gefunden.";

}else{*/

if($row[$i] = mysql_fetch_assoc($ergebnis)){
?>

<table border="1" cellpadding="5" cellspacing="1" width="80%">
<tr>
<td>Spielername</td>
<td>Rang</td>
<td>Gebäude Punkte</td>
<td>Forschungs Punkte</td>
<td>Gesamt Punkte</td>
</tr>
<tr>
<?php
//}elseif(!$row[$i] = mysql_fetch_assoc($ergebnis)){
//echo "Spieler nicht gefunden.";
}
$i = 0;
WHILE ($row[$i] = mysql_fetch_assoc($ergebnis))
{
   //echo $row[$i]['username'] . "<br />";
   //$i++;

?>  

<td>
<?php

/*$i = 0;
while($i < count($daten))
{    
*/
$stats_get = mysql_query("SELECT * FROM `planets` WHERE `id`='".$row[$i]['id']."'") or die(mysql_error());
$planets = mysql_fetch_assoc($stats_get);

/*
$search_get2 = mysql_query("SELECT * FROM `planets` WHERE `id` = '".$row[$i]['id']."'") or die(mysql_error());
$search2 = mysql_fetch_assoc($search_get2);

$row2[$i] = mysql_fetch_array($search2);
*/

echo "<a href=\"stats.php?id=" .$row[$i]['id']."\">" . $row[$i]['username'] . "</a>";    
//echo $row[$i]['username'];

?>

</td>
<td>    
    <?php echo $planets['rank'] ?>
</td>
<td>    
    <?php echo $planets['points_buildings'] ?>
</td>
<td>    
    <?php echo $planets['points_research'] ?>
</td>
<td>    
    <?php echo $planets['points_buildings']+$planets['points_research'] ?>
</td>
</tr>


<?php
$i++;
}}}
?>
</table>
<?php
//else{
//echo "Spieler nicht gefunden.";
}
?>
</p>       

<?php
}
?>       
<?php include ('footer.php') ?>
<?php ob_end_flush(); ?>
What it does is:
#if no input-> tells you to input something
#if input->find the user/users and shows some of their stats

Problem:
it doesnt show all users and i dunno why
for example if there are folowing 3 playernames:
+hallo
+hallo1
+hallotest

and i search for "hal"
the output is only user "hallo1" + user "hallotest" but not user "hallo"...
dont understand this
and btw it has nothing to do with which player iam logged in and do the search..

Next problem is that iam not always get an output:
Example if i search for player "schatz1"
and my input is "sc" i dont find anything
but if i type in as serach term "ha" i find player "schatz1" and also find players "hallo1" and "hallotest"...(also not working with any other 2letter terms... - makes no sense to me...)

Any suggestions or someone can see my mistake?
Last edited by Foofighter on Thu Jun 07, 2012 9:20 am, edited 1 time in total.
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Problem with user search

Post by Chris »

What does

Code: Select all

echo protect($suche); 
spit out when you search "sc"?
Fighting for peace is declaring war on war. If you want peace be peaceful.
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Problem with user search

Post by Winawer »

Try

Code: Select all

if( mysql_num_rows( $ergebnis ) > 0 ) { 
Instead of

Code: Select all

if($row[$i] = mysql_fetch_assoc($ergebnis)){ 
User avatar
Foofighter
Posts: 121
Joined: Sun Mar 04, 2012 1:52 pm

Re: Problem with user search

Post by Foofighter »

Winawer wrote:Try

Code: Select all

if( mysql_num_rows( $ergebnis ) > 0 ) { 
Instead of

Code: Select all

if($row[$i] = mysql_fetch_assoc($ergebnis)){ 
Thx a lot guys, this code solved it! :)

Here is the fully functional code for those who need a search script, feel free to use it.

Code: Select all

<?php
ob_start();
session_start();
include ('header.php');
if(!isset($_SESSION['uid'])){
    //include ('safe.php');
    echo "Sie müssen eingeloggt sein um diese Seite zu sehen!";
    header("Location:login.php");
}else{
?>

    <section id="middle">
        <div id="container">
            <div id="content">
<table border="2" cellpadding="5" cellspacing="1" width="80%">
        <tr>
            <td colspan="2"><b>Suche</b></td>
        </tr>
        <tr>
        <td>
        <form name="form2" method="post" action="#">
        <table border="1" cellpadding="5" cellspacing="1" width="100%">
        <tr>
            <td colspan="4" style="height:18px;">
            <div style="width:100%;height:100%;overflow:hidden;">
            Bitte gewünschten Suchbegriff eingeben.
            </div>
            </td>            
        </tr>
        <tr>
            <td><strong>Spielername</strong></td>
               <td colspan="2" valign="middle" align="center" style="height:20px; width:365px;">
            <div style="width:100%;height:100%;overflow:hidden;">
            <input name="suche" type="text" id="suche" size="30">
            </form>
            </div>
            </td>
            <td style="height:25px;">
            <div style="width:100%;height:100%;overflow:hidden;">
            <input type="submit" name="searchname" value="suchen" >
            </div>
            </td>
        </tr>
        </table>
        </table>
<p id="p_red">     
<?php

$tbl_name="users_valid";
$tbl1_name="planets";
$i = 0;
// values sent from form
if($_POST)
{
$suche=protect($_POST['suche']);        

//Kontrollstruktur für suche
if(isset($_POST['suche'])){
    
 if($suche == ""){
        echo "Bitte Suchbegriff einsetzen!";
    }else{

//Vergleich Suchparameter mit user table
$abfrage = "Select * FROM users_valid WHERE username LIKE '%".protect($suche)."%'";
$ergebnis = mysql_query($abfrage); 


if(mysql_num_rows( $ergebnis ) > 0 ) { 
?>

<table border="1" cellpadding="5" cellspacing="1" width="80%">
<tr>
<td>Spielername</td>
<td>Rang</td>
<td>Gebäude Punkte</td>
<td>Forschungs Punkte</td>
<td>Gesamt Punkte</td>
</tr>
<tr>

<?php
}elseif(mysql_num_rows( $ergebnis ) <= 0 ){
echo "Spieler nicht gefunden.";
}
$i = 0;
WHILE ($row[$i] = mysql_fetch_assoc($ergebnis))
{
?> 
<td>
<?php

$stats_get = mysql_query("SELECT * FROM `planets` WHERE `id`='".$row[$i]['id']."'") or die(mysql_error());
$planets = mysql_fetch_assoc($stats_get);

echo "<a href=\"stats.php?id=" .$row[$i]['id']."\">" . $row[$i]['username'] . "</a>";    

?>

</td>
<td>    
    <?php echo $planets['rank'] ?>
</td>
<td>    
    <?php echo $planets['points_buildings'] ?>
</td>
<td>    
    <?php echo $planets['points_research'] ?>
</td>
<td>    
    <?php echo $planets['points_buildings']+$planets['points_research'] ?>
</td>
</tr>


<?php
$i++;
}}}
?>
</table>
<?php
}
?>
</p>       

<?php
}
?>       
<?php include ('footer.php') ?>
<?php ob_end_flush(); ?>
regards Foo
Post Reply

Return to “Advanced Help and Support”