By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

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

P: 22
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.

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3.     if (isset($_POST['search'])){ 
  4.     $getSearch = clean_text($_POST['search']);
  5.     $connectToDb = "select * from tutorials where tutorial_title like '%".$getSearch."%' and active=1"; 
  6.     $searchResults = $db->query($connectToDb) or die($db->error);
  7.     $numResultas = $searchResults ->num_rows;
  8.         echo "<p class='counter_search'>Found : " . $numResultas . "</p>";
  9.         switch ($numResultas) {
  10.  
  11.             case 0:
  12.             include "searchPageAdvanced.php";
  13.             break;
  14.  
  15.             case $numResultas >= 1:
  16.             while($row = mysqli_fetch_array($searchResults)) {
  17.  
  18.             echo "<a class='counter_linkHeader' href='index.php?cat=tutorials_view&learn_id=5&tutorial_id=".$row['tutorial_id']."'>".$row['tutorial_title']."</a>";
  19.             echo "<p class='counter_description'>";
  20.             $your_desired_width=200;
  21.             $description=$row['content'];
  22.             if (strlen($description) > $your_desired_width) {$description = wordwrap($description, $your_desired_width); $description = substr($description, 0, strpos($description, "\n"))." ..."; } echo $description;
  23.             echo"</p>";    
  24.         }
  25.             break;
  26.             }
  27.  
  28.     }else{ 
  29.     ?>
  30.     <p>&nbsp;</p>
  31. <table width="875" border="0">
  32.   <tr>
  33.     <td align="center" valign="middle"><img src="images/searchError.jpg" width="223" height="147" alt="search error" /></td>
  34.   </tr>
  35.   <tr>
  36.     <td align="center" valign="middle">Sorry you came here by mistake <br />
  37.       please use the search box to get results</td>
  38.   </tr>
  39. </table>
  40. <p>&nbsp;</p>
  41.     <?php
  42.     }
  43. ?>
how to adjust my code to meet my requirements.

Thanks
regards
Apr 8 '12 #1

✓ answered by Rabbit

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?

Share this Question
Share on Google+
9 Replies


Rabbit
Expert Mod 10K+
P: 12,347
Why send them to another page? Since you're running the query in that page anyways, why not just write out the results there?
Apr 8 '12 #2

P: 22
How are you dear Rabbit, thank you that you replayed to my question.
If you are asking about this
Expand|Select|Wrap|Line Numbers
  1. 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
Apr 8 '12 #3

Rabbit
Expert Mod 10K+
P: 12,347
If you have a new question, please post it in a new thread. We want to keep it one question per thread.
Apr 8 '12 #4

P: 22
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?
Apr 8 '12 #5

Rabbit
Expert Mod 10K+
P: 12,347
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.
Apr 9 '12 #6

P: 22
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.
Expand|Select|Wrap|Line Numbers
  1. <form action="index.php?cat=search_results&learn_id=1" method="post">
  2.       <div id="topSearchBodyStyle">
  3.         <input type="text" name="search" class="topSearchTextBackground" />
  4.       </div>
  5.       <div id="topSearchButtonStyle">
  6.         <input type="submit" name="submit" class="topSearchButtonBackground" value="" />
  7.       </div>
  8.     </form>
and this is the page called searchPage.php

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3.     if (isset($_POST['search'])){ 
  4.     $getSearch = clean_text($_POST['search']);
  5.     $connectToDb = "select * from tutorials where tutorial_title like '%".$getSearch."%' and active=1"; 
  6.     $searchResults = $db->query($connectToDb) or die($db->error);
  7.     $numResultas = $searchResults ->num_rows;
  8.         echo "<p class='counter_search'>Found : " . $numResultas . "</p>";
  9.         switch ($numResultas) {
  10.  
  11.             case 0:
  12.             include "searchPageAdvanced.php";
  13.             break;
  14.  
  15.             case $numResultas >= 1:
  16.             while($row = mysqli_fetch_array($searchResults)) {
  17.  
  18.             echo "<a class='counter_linkHeader' href='index.php?cat=tutorials_view&learn_id=5&tutorial_id=".$row['tutorial_id']."'>".$row['tutorial_title']."</a>";
  19.             echo "<p class='counter_description'>";
  20.             $your_desired_width=200;
  21.             $description=$row['content'];
  22.             if (strlen($description) > $your_desired_width) {$description = wordwrap($description, $your_desired_width); $description = substr($description, 0, strpos($description, "\n"))." ..."; } echo $description;
  23.             echo"</p>";    
  24.         }
  25.             break;
  26.             }
  27.  
  28.     }else{ 
  29.     ?>
  30.     <p>&nbsp;</p>
  31. <table width="875" border="0">
  32.   <tr>
  33.     <td align="center" valign="middle"><img src="http://bytes.com/images/searchError.jpg" width="223" height="147" alt="search error" /></td>
  34.   </tr>
  35.   <tr>
  36.     <td align="center" valign="middle">Sorry you came here by mistake <br />
  37.       please use the search box to get results</td>
  38.   </tr>
  39. </table>
  40. <p>&nbsp;</p>
  41.     <?php
  42.     }
  43. ?>
and the is the advanced search page.
which will search the content.

Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post">
  2.  
  3.         <input type='text' name='searchAdv' />
  4.  
  5.         <input type='submit' name='submit_adv' value='Search' id="submit_adv" />
  6.     </form>
  7.     </td>
  8.   </tr>
  9.   <tr>
  10.     <td align='center' valign='middle'><p>Sorry you came here because your search didn't </p>
  11.       <p>meet any of the content try advanced!!</p></td>
  12.   </tr>
  13. </table>
  14.  
  15. <?php
  16.     if (isset($_POST['submit_adv'])){
  17.     $getSearch = clean_text($_POST['searchAdv']);
  18.  
  19.     $connectToDb = "select * from tutorials where content like '%".$getSearch."%' and active=1"; 
  20.     $searchResultsAdv = $db->query($connectToDb) or die($db->error);
  21.  
  22.     while($rowAdv = mysqli_fetch_array($searchResultsAdv)) {
  23.  
  24.             echo "<a class='counter_linkHeader' href='index.php?cat=tutorials_view&learn_id=5&tutorial_id=".$rowAdv['tutorial_id']."'>".$rowAdv['tutorial_title']."</a>";
  25.             echo "<p class='counter_description'>";
  26.             $your_desired_width=200;
  27.             $description=$rowAdv['content'];
  28.             if (strlen($description) > $your_desired_width) {$description = wordwrap($description, $your_desired_width); $description = substr($description, 0, strpos($description, "\n"))." ..."; } echo $description;
  29.             echo"</p>";    
  30.         }
  31.     }
  32.   ?> 

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
Expand|Select|Wrap|Line Numbers
  1. 'Search'
please help me with this.
Apr 9 '12 #7

Rabbit
Expert Mod 10K+
P: 12,347
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.
Apr 9 '12 #8

P: 22
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.
Apr 9 '12 #9

Rabbit
Expert Mod 10K+
P: 12,347
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?
Apr 9 '12 #10

Post your reply

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