Database Search Dilemma

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Technoxic
Forum Newbie
Posts: 2
Joined: Thu Dec 17, 2009 10:19 am

Database Search Dilemma

Post by Technoxic »

Hello.
I am currently having some issues with my websites search feature.
So far the script below shows a page when just the search button is pressed where the user can do a more advanced search. The problem is when the user searches something into a search on my theme (code not supplied) but it only searches Tutorials at the moment. I want it to do both and display the results side by side, the problem is it is so confusing. Can any one help make this simpler? Thanks in advance.

Code for search.php below.

Code: Select all

<?php
if ($_GET['q'])
{
    $id = intval($_GET['id']);
    $var = $_GET['q'] ;
    $qt = $_GET['chois'] ;
    $trimmed = trim($var);
    $done = ereg_replace("[^A-Za-z0-9]", "", $trimmed );
    $cat = ''.$_GET['category'].'';
    if($qt==1)
    {
        echo "Questions";
        if($_GET['category'] == '0')
        {
            if($_GET['choix'] == '1')
            {
                $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE question_title like \"%$trimmed%\" "),0);
            }
            else if($_GET['choix'] == '2')
            {
                $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE question_desc like \"%$trimmed%\" "),0);
            }
            else if($_GET['choix'] == '3')
            {
                $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE question_desc like \"%$trimmed%\" OR question_title like \"%$trimmed%\" "),0);
            }
            //DO NOT TOUCH
            if ($trimmed == "")
            {
                echo '<br><strong>'.NOSEARCH.'</strong><br><br>';
            }
            elseif ($total_results_search >= 50)
            {
                echo '<br><strong>'.TOO_MANY.'</strong><br><br>';
            }
            elseif ($total_results_search == 0)
            {
                echo '<br><strong>'.NORESULTS.'</strong><br><br>';
            }
            elseif($_GET['choix'] == '1')
            {
                $sql = mysql_query("select * from questions WHERE question_title like \"%$trimmed%\" order by id DESC");
            }
            elseif($_GET['choix'] == '2')
            {
                $sql = mysql_query("select * from questions WHERE question_desc like \"%$trimmed%\" order by id DESC");
            }
            elseif($_GET['choix'] == '3')
            {
                $sql = mysql_query("select * from questions WHERE question_desc like \"%$trimmed%\" OR question_title like \"%$trimmed%\" order by id DESC");
            }
        }
        if($_GET['category'] != '0')
        {
            if($_GET['choix'] == '1')
            {
                $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE question_title like \"%$trimmed%\" AND catid = '".$_GET['category']."' "),0);
            }
            else if($_GET['choix'] == '2')
            {
                $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE question_desc like \"%$trimmed%\" AND catid = '".$_GET['category']."' "),0);
            }
            else if($_GET['choix'] == '3')
            {
                $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE question_desc like \"%$trimmed%\" OR question_title like \"%$trimmed%\" AND catid = '".$_GET['category']."'"),0);
            }
            //DO NOT TOUCH
            if ($trimmed == "")
            {
                echo '<br><strong>'.NOSEARCH.'</strong><br><br>';
            }
            elseif ($total_results_search >= 50)
            {
                echo '<br><strong>'.TOO_MANY.'</strong><br><br>';
            }
            elseif ($total_results_search == 0)
            {
                echo '<br><strong>'.NORESULTS.'</strong><br><br>';
            }
            elseif($_GET['choix'] == '1')
            {
                $sql = mysql_query("select * from questions WHERE question_title like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
            }
            elseif($_GET['choix'] == '2')
            {
                $sql = mysql_query("select * from questions WHERE question_desc like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
            }
            elseif($_GET['choix'] == '3')
            {
                $sql = mysql_query("select * from questions WHERE question_desc like \"%$trimmed%\" OR question_title like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
            }
        }
    }
    else
    if($qt==2)
    {
        {
            echo "Tutorials";
            if($_GET['category'] == '0')
            {
                if($_GET['choix'] == '1')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE title like \"%$trimmed%\" "),0);
                }
                else if($_GET['choix'] == '2')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" "),0);
                }
                else if($_GET['choix'] == '3')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" "),0);
                }
                //DO NOT TOUCH
                if ($trimmed == "")
                {
                    echo '<br><strong>'.NOSEARCH.'</strong><br><br>';
                }
                elseif ($total_results_search >= 50)
                {
                    echo '<br><strong>'.TOO_MANY.'</strong><br><br>';
                }
                elseif ($total_results_search == 0)
                {
                    echo '<br><strong>'.NORESULTS.'</strong><br><br>';
                }
                elseif($_GET['choix'] == '1')
                {
                    $sql = mysql_query("select * from tuts WHERE title like \"%$trimmed%\" order by id DESC");
                }
                elseif($_GET['choix'] == '2')
                {
                    $sql = mysql_query("select * from tuts WHERE ldesc like \"%$trimmed%\" order by id DESC");
                }
                elseif($_GET['choix'] == '3')
                {
                    $sql = mysql_query("select * from tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" order by id DESC");
                }
            }
            if($_GET['category'] != '0')
            {
                if($_GET['choix'] == '1')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE title like \"%$trimmed%\" AND catid = '".$_GET['category']."' "),0);
                }
                else if($_GET['choix'] == '2')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" AND catid = '".$_GET['category']."' "),0);
                }
                else if($_GET['choix'] == '3')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" AND catid = '".$_GET['category']."'"),0);
                }
                //DO NOT TOUCH
                if ($trimmed == "")
                {
                    echo '<br><strong>'.NOSEARCH.'</strong><br><br>';
                }
                elseif ($total_results_search >= 50)
                {
                    echo '<br><strong>'.TOO_MANY.'</strong><br><br>';
                }
                elseif ($total_results_search == 0)
                {
                    echo '<br><strong>'.NORESULTS.'</strong><br><br>';
                }
                elseif($_GET['choix'] == '1')
                {
                    $sql = mysql_query("select * from tuts WHERE title like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
                }
                elseif($_GET['choix'] == '2')
                {
                    $sql = mysql_query("select * from tuts WHERE ldesc like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
                }
                elseif($_GET['choix'] == '3')
                {
                    $sql = mysql_query("select * from tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
                }
            }
        }
        echo '<p>'.$total_results_search.' Results</p><div id="catwrapper">';
        while($row = mysql_fetch_array($sql))
        {
            echo '<div class="cat_game_container">';
            $therow = $therow + 1;
            if ($row['import'] == 1)
            {
                $image = '<img align="absmiddle" src="'.$site_url.'/games/images/'.$row['url'].'.png" width="'.$image_width.'" height="'.$image_height.'" />';
            }
            else if ($row['import'] == 3)
            {
                $image = '<img align="top" src="'.$site_url.'/games/images/'.$row['image'].'" width="'.$image_width.'" height="'.$image_height.'" />';
            }
            else
            {
                $image = '<img align="absmiddle" src="'.$row['image'].'" width="'.$image_width.'" height="'.$image_height.'" />';
            }
            if($qt==1)
            {
                if (strlen($row['question_title']) >20)
                {
                    $name = substr($row['question_title'], 0, 20)."...";
                }
                else
                {
                    $name = $row['question_title'];
                }
                if (strlen($row['question_desc']) >55)
                {
                    $description = substr($row['question_desc'], 0, 55)."...";
                }
                else
                {
                    $description = $row['question_desc'];
                }
                $game_url = 'index.php?task=view&id='.$row['id'].'';
            }
            else
            {
                if (strlen($row['title']) >20)
                {
                    $name = substr($row['title'], 0, 20)."...";
                }
                else
                {
                    $name = $row['title'];
                }
                if (strlen($row['ldesc']) >55)
                {
                    $description = substr($row['ldesc'], 0, 55)."...";
                }
                else
                {
                    $description = $row['ldesc'];
                }
                $game_url = 'index.php?task=viewtut&id='.$row['id'].'';
            }
            echo '<a title="View '.$name.'" href="'.$site_url.'/'.$game_url.'"><strong>'.$name.'</strong></a><br>'.$description.'';
            echo '</small>';
            if(isset($_COOKIE["qtut_username"]))
            {
                $sqlaa = mysql_query("SELECT * FROM users WHERE id=".$userid."");
                $rowaa = mysql_fetch_array($sqlaa);
                if ($rowaa['admin'] == 1)
                {
                    echo ' <br><strong>Admin: </strong><a href='.$site_url.'/admin/?action=delete_done&id='.$row['id'].'>Delete Question</a>';
                }
            }
            echo '</div>';
            if ($therow == 2)
            {
                echo '</div><div id="homewrapper">';
                $therow = 0;
            }
        }
        echo '</div>';
        $max_results = 20;
        $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE catid='".$id."'"),0);
        $total_pages = ceil($total_results / $max_results);
        $sq2 = mysql_query("SELECT * FROM cats WHERE id=".$id."");
        while($row2 = mysql_fetch_array($sq2))
        {
            $abcd2= $row2['name'];
            $abcd2 = str_replace (" ", "-", $abcd2);
        }
    }else{
      echo "Questions And Tutorials";
            if($_GET['category'] == '0')
            {
                if($_GET['choix'] == '1')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE title like \"%$trimmed%\" "),0);
                    
                }
                else if($_GET['choix'] == '2')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" "),0);
                }
                else if($_GET['choix'] == '3')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" "),0);
                }
                //DO NOT TOUCH
                if ($trimmed == "")
                {
                    echo '<br><strong>'.NOSEARCH.'</strong><br><br>';
                }
                elseif ($total_results_search >= 50)
                {
                    echo '<br><strong>'.TOO_MANY.'</strong><br><br>';
                }
                elseif ($total_results_search == 0)
                {
                    echo '<br><strong>'.NORESULTS.'</strong><br><br>';
                }
                elseif($_GET['choix'] == '1')
                {
                    $sql = mysql_query("select * from tuts   WHERE title like \"%$trimmed%\" order by id DESC");
                    $sql6 = mysql_query("select * from questions WHERE question_title like \"%$trimmed%\" order by id DESC");
                }
                elseif($_GET['choix'] == '2')
                {
                    $sql = mysql_query("select * from tuts WHERE  ldesc like \"%$trimmed%\" order by id DESC");
                     $sql6 = mysql_query("select * from questions WHERE question_desc like \"%$trimmed%\" order by id DESC");
                }
                elseif($_GET['choix'] == '3')
                {
                    $sql = mysql_query("select * from tuts WHERE   ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" order by id DESC");
                     $sql6 = mysql_query("select * from questions WHERE question_desc like \"%$trimmed%\" OR question_title like \"%$trimmed%\" order by id DESC");
                }
            }
            if($_GET['category'] != '0')
            {
                if($_GET['choix'] == '1')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE title like \"%$trimmed%\" AND catid = '".$_GET['category']."' "),0);
                }
                else if($_GET['choix'] == '2')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" AND catid = '".$_GET['category']."' "),0);
                }
                else if($_GET['choix'] == '3')
                {
                    $total_results_search = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" AND catid = '".$_GET['category']."'"),0);
                }
                //DO NOT TOUCH
                if ($trimmed == "")
                {
                    echo '<br><strong>'.NOSEARCH.'</strong><br><br>';
                }
                elseif ($total_results_search >= 50)
                {
                    echo '<br><strong>'.TOO_MANY.'</strong><br><br>';
                }
                elseif ($total_results_search == 0)
                {
                    echo '<br><strong>'.NORESULTS.'</strong><br><br>';
                }
                elseif($_GET['choix'] == '1')
                {
                    $sql = mysql_query("select * from tuts WHERE title like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
                }
                elseif($_GET['choix'] == '2')
                {
                    $sql = mysql_query("select * from tuts WHERE ldesc like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
                }
                elseif($_GET['choix'] == '3')
                {
                    $sql = mysql_query("select * from tuts WHERE ldesc like \"%$trimmed%\" OR title like \"%$trimmed%\" AND catid = '".$_GET['category']."' order by id DESC");
                }
            }
        }
        echo '<p>'.$total_results_search.' Results</p><div id="catwrapper">';
        while($row = mysql_fetch_array($sql))
        {
            echo '<div class="cat_game_container">';
            $therow = $therow + 1;
            if ($row['import'] == 1)
            {
                $image = '<img align="absmiddle" src="'.$site_url.'/games/images/'.$row['url'].'.png" width="'.$image_width.'" height="'.$image_height.'" />';
            }
            else if ($row['import'] == 3)
            {
                $image = '<img align="top" src="'.$site_url.'/games/images/'.$row['image'].'" width="'.$image_width.'" height="'.$image_height.'" />';
            }
            else
            {
                $image = '<img align="absmiddle" src="'.$row['image'].'" width="'.$image_width.'" height="'.$image_height.'" />';
            }
            if($qt==1)
            {
                if (strlen($row['question_title']) >20)
                {
                    $name = substr($row['question_title'], 0, 20)."...";
                }
                else
                {
                    $name = $row['question_title'];
                }
                if (strlen($row['question_desc']) >55)
                {
                    $description = substr($row['question_desc'], 0, 55)."...";
                }
                else
                {
                    $description = $row['question_desc'];
                }
                $game_url = 'index.php?task=view&id='.$row['id'].'';
            }
            else
            {
                if (strlen($row['title']) >20)
                {
                    $name = substr($row['title'], 0, 20)."...";
                }
                else
                {
                    $name = $row['title'];
                }
                if (strlen($row['ldesc']) >55)
                {
                    $description = substr($row['ldesc'], 0, 55)."...";
                }
                else
                {
                    $description = $row['ldesc'];
                }
                $game_url = 'index.php?task=viewtut&id='.$row['id'].'';
            }
            echo '<a title="View '.$name.'" href="'.$site_url.'/'.$game_url.'"><strong>'.$name.'</strong></a><br>'.$description.'';
            echo '</small>';
            if(isset($_COOKIE["qtut_username"]))
            {
                $sqlaa = mysql_query("SELECT * FROM users WHERE id=".$userid."");
                $rowaa = mysql_fetch_array($sqlaa);
                if ($rowaa['admin'] == 1)
                {
                    echo ' <br><strong>Admin: </strong><a href='.$site_url.'/admin/?action=delete_done&id='.$row['id'].'>Delete Question</a>';
                }
            }
            echo '</div>';
            if ($therow == 2)
            {
                echo '</div><div id="homewrapper">';
                $therow = 0;
            }
        }
        echo '</div>';
        $max_results = 20;
        $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM questions WHERE catid='".$id."'"),0);
        $total_pages = ceil($total_results / $max_results);
        $sq2 = mysql_query("SELECT * FROM cats WHERE id=".$id."");
        while($row2 = mysql_fetch_array($sq2))
        {
            $abcd2= $row2['name'];
            $abcd2 = str_replace (" ", "-", $abcd2);
        }
    
    
    }
 
else
{echo '<br /><br />'.ENTER_SEARCH.':<br /><br />';
 
$options = '<optgroup label="Choice">'; 
$options .= '<option value="0">Select</option>';    
$options .= '<option value="0">|- &nbsp;&nbsp;All the categories</option>'; 
$options .= '<option value="0">&nbsp;</option>';    
$sql = mysql_query("SELECT id, name FROM cats ORDER BY name ASC");
while($row = mysql_fetch_array($sql))
{
$options .= '<option value="'.$row['id'].'">|-  &nbsp;&nbsp;'.$row['name'].'</option>';
}
$options .= '<option value="0">&nbsp;</option>';    
$options .= '</optgroup>';  
?>
 
<div class="game_info_head"></div>
<div class="game_info" style="text-align:center">
  <form name="form" action="<? echo $site_url?>/index.php?task=search" method="get">
    <input name="q" type="text" size="45"/>
    <br />
    <br />
    Search For
    <input type="radio" name="chois" checked="checked" id="chois1" value="1" />
    Questions
    <input type="radio" name="chois"  id="chois2" value="2" />
    Tutorials <br />
    <br />
    Search in
    <input type="radio" name="choix" id="choix1" value="1" />
    Title
    <input type="radio" name="choix" checked="checked" id="choix2" value="2" />
    Description
    <input type="radio" name="choix" id="choix3" value="3" />
    Title & Description <br />
    <br />
    Category
    <select name="category">
      <?php echo $options ?>
    </select>
    <br  />
    <br />
    <input type="submit" name="Submit" value="<?php echo SEARCH; ?>" class="btn" />
    <input name="task" type="hidden" value="search" />
  </form>
</div>
<div class="game_info_bottom"></div>
<?php } ?>
 
Technoxic
Forum Newbie
Posts: 2
Joined: Thu Dec 17, 2009 10:19 am

Re: Database Search Dilemma

Post by Technoxic »

Just had a thought if you need more script just say. Thank you again.
Post Reply