adjusting my code to take the user to the search results if there is any

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yousef Altaf
    New Member
    • Jun 2011
    • 22

    adjusting my code to take the user to the search results if there is any

    I have made this page to search my site but here in my MySQLi I am looking in the column title so if the user didn't find what he looking for I will include a advanced search which will look into the content of the tutorials so the user can get any results.

    this the complete code of my page.

    Code:
    <?php 
    	
    	if (isset($_POST['search'])){ 
    	$getSearch = clean_text($_POST['search']);
    	$connectToDb = "select * from tutorials where tutorial_title like '%".$getSearch."%' and active=1"; 
    	$searchResults = $db->query($connectToDb) or die($db->error);
    	$numResultas = $searchResults ->num_rows;
    		echo "<p class='counter_search'>Found : " . $numResultas . "</p>";
    		switch ($numResultas) {
    			
    			case 0:
    			include "searchPageAdvanced.php";
    			break;
    			
    			case $numResultas >= 1:
    			while($row = mysqli_fetch_array($searchResults)) {
    			
    			echo "<a class='counter_linkHeader' href='index.php?cat=tutorials_view&learn_id=5&tutorial_id=".$row['tutorial_id']."'>".$row['tutorial_title']."</a>";
    			echo "<p class='counter_description'>";
    			$your_desired_width=200;
    			$description=$row['content'];
    			if (strlen($description) > $your_desired_width) {$description = wordwrap($description, $your_desired_width); $description = substr($description, 0, strpos($description, "\n"))." ..."; } echo $description;
    			echo"</p>";	
    		}
    			break;
    			}
    		
    	}else{ 
    	?>
        <p>&nbsp;</p>
    <table width="875" border="0">
      <tr>
        <td align="center" valign="middle"><img src="images/searchError.jpg" width="223" height="147" alt="search error" /></td>
      </tr>
      <tr>
        <td align="center" valign="middle">Sorry you came here by mistake <br />
          please use the search box to get results</td>
      </tr>
    </table>
    <p>&nbsp;</p>
        <?php
    	}
    ?>
    how to adjust my code to meet my requirements.

    Thanks
    regards
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Why send them to another page? Since you're running the query in that page anyways, why not just write out the results there?

    Comment

    • Yousef Altaf
      New Member
      • Jun 2011
      • 22

      #3
      How are you dear Rabbit, thank you that you replayed to my question.
      If you are asking about this
      Code:
      include "searchPageAdvanced.php";
      by your question "Why send them to another page?" so I have tried many times to let the all code in one page.

      OK look again to my updated post I have made a changes to it now it works and I attached 2 files.php see them too.

      I have now new problem that when I make search on the second page which is the advanced search page it takes me to empty page.

      sorry for my long replay,
      Regards
      Yousef Altaf Wasti

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        If you have a new question, please post it in a new thread. We want to keep it one question per thread.

        Comment

        • Yousef Altaf
          New Member
          • Jun 2011
          • 22

          #5
          Rabbit it's not it's the same question but I just updated the code for better idea anyway it's the same. did you see my attachments?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Yes, I see the attachment. But I don't open attachments for security reasons. If you have code, post it in the thread with code tags.

            Comment

            • Yousef Altaf
              New Member
              • Jun 2011
              • 22

              #7
              OK, if I am in your place I would say the same.

              this is the first form that in the header of the page which search my titles.
              Code:
              <form action="index.php?cat=search_results&learn_id=1" method="post">
                    <div id="topSearchBodyStyle">
                      <input type="text" name="search" class="topSearchTextBackground" />
                    </div>
                    <div id="topSearchButtonStyle">
                      <input type="submit" name="submit" class="topSearchButtonBackground" value="" />
                    </div>
                  </form>
              and this is the page called searchPage.php

              Code:
              <?php 
              	
              	if (isset($_POST['search'])){ 
              	$getSearch = clean_text($_POST['search']);
              	$connectToDb = "select * from tutorials where tutorial_title like '%".$getSearch."%' and active=1"; 
              	$searchResults = $db->query($connectToDb) or die($db->error);
              	$numResultas = $searchResults ->num_rows;
              		echo "<p class='counter_search'>Found : " . $numResultas . "</p>";
              		switch ($numResultas) {
              			
              			case 0:
              			include "searchPageAdvanced.php";
              			break;
              			
              			case $numResultas >= 1:
              			while($row = mysqli_fetch_array($searchResults)) {
              			
              			echo "<a class='counter_linkHeader' href='index.php?cat=tutorials_view&learn_id=5&tutorial_id=".$row['tutorial_id']."'>".$row['tutorial_title']."</a>";
              			echo "<p class='counter_description'>";
              			$your_desired_width=200;
              			$description=$row['content'];
              			if (strlen($description) > $your_desired_width) {$description = wordwrap($description, $your_desired_width); $description = substr($description, 0, strpos($description, "\n"))." ..."; } echo $description;
              			echo"</p>";	
              		}
              			break;
              			}
              		
              	}else{ 
              	?>
                  <p>&nbsp;</p>
              <table width="875" border="0">
                <tr>
                  <td align="center" valign="middle"><img src="http://bytes.com/images/searchError.jpg" width="223" height="147" alt="search error" /></td>
                </tr>
                <tr>
                  <td align="center" valign="middle">Sorry you came here by mistake <br />
                    please use the search box to get results</td>
                </tr>
              </table>
              <p>&nbsp;</p>
                  <?php
              	}
              ?>
              and the is the advanced search page.
              which will search the content.

              Code:
              <form action="" method="post">
                    
                      <input type='text' name='searchAdv' />
                    	
                      <input type='submit' name='submit_adv' value='Search' id="submit_adv" />
                  </form>
                  </td>
                </tr>
                <tr>
                  <td align='center' valign='middle'><p>Sorry you came here because your search didn't </p>
                    <p>meet any of the content try advanced!!</p></td>
                </tr>
              </table>
              
              <?php
              	if (isset($_POST['submit_adv'])){
              	$getSearch = clean_text($_POST['searchAdv']);
              	
              	$connectToDb = "select * from tutorials where content like '%".$getSearch."%' and active=1"; 
              	$searchResultsAdv = $db->query($connectToDb) or die($db->error);
              	
              	while($rowAdv = mysqli_fetch_array($searchResultsAdv)) {
              			
              			echo "<a class='counter_linkHeader' href='index.php?cat=tutorials_view&learn_id=5&tutorial_id=".$rowAdv['tutorial_id']."'>".$rowAdv['tutorial_title']."</a>";
              			echo "<p class='counter_description'>";
              			$your_desired_width=200;
              			$description=$rowAdv['content'];
              			if (strlen($description) > $your_desired_width) {$description = wordwrap($description, $your_desired_width); $description = substr($description, 0, strpos($description, "\n"))." ..."; } echo $description;
              			echo"</p>";	
              		}
              	}
                ?>

              now again the problem begin in the advanced search page, the search page is working fine and every thing else hope so!

              but when I search in the advanced page it return the page empty like he didn't take any thing for the input area
              Code:
              'Search'
              please help me with this.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                You only include the search page. That doesn't mean you set any of the values that it needs to do its search. You should either set those variables needed or redirect to the advanced search page and pass the values.

                Comment

                • Yousef Altaf
                  New Member
                  • Jun 2011
                  • 22

                  #9
                  Rabbit sorry I didn't get your point can you please show me an example.

                  1- when the user search for a word and the found is 0 it takes him to includes and includes has a new form that has the value of "SearchAdv" on the input text.

                  2- what do you mean with pass the values it is already on the same page.

                  please need an example on this is you can.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    1) Yes I know what you're doing. I can see the code.

                    2) No, the variables may be on the same page but your code does not use the same variables.

                    In your regular search, you're using $_POST['search']. In your advanced search, you're using $_POST['submit_adv']. If you didn't notice, they are different. If the user first uses the regular search, only $_POST['search'] is populated. $_POST['submit_adv'] is never populated. So tell me why you expect the advanced search to work if $_POST['submit_adv'] has no value?

                    Comment

                    Working...