designer76 Posted August 3, 2009 Share Posted August 3, 2009 Hello guys. I am a web designer who is now trying to learn some php. I have this search form script that I put together from tutorials and asking a question or two. Now I am stuck and I can't seem to get this thing to work. The search itself works and returns the number of results just fine (e.g. 3 results found). My problem is that it isn't displaying the actual results in the table columns like I want it to. I have a specifically designed table (with a div around it) that I need to output with 1 result in 3 of the columns (the table has a total of 5 columns, but 2 are just visual spacers). after 3 results have been displayed then I want that same table output on the next "line" with three more results, etc... until I set the number to begin display paging (the paging isn't in the script yet). I have included an example of what I am trying to get my HTML code to display and the actual PHP script itself. Any help given would be awesome as I have been doing this for the past 3 days. <div id="search_number_found">3 Results found.</div> <div class="contain"> <div class='my_class'> <table width='639' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td width='197' height='270' align='left' valign='top'>search result</td> <td width='23'></td> <td width='197' align='center' valign='top'>search result</td> <td width='21'></td> <td width='199' align='right' valign='top'>search result</td> </tr> </table> </div> <div class='my_class'> <table width='639' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td width='197' height='270' align='left' valign='top'>search result</td> <td width='23'></td> <td width='197' align='center' valign='top'>search result</td> <td width='21'></td> <td width='199' align='right' valign='top'>search result</td> </tr> </table> </div> <div class='my_class'> <table width='639' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td width='197' height='270' align='left' valign='top'>search result</td> <td width='23'></td> <td width='197' align='center' valign='top'>search result</td> <td width='21'></td> <td width='199' align='right' valign='top'>search result</td> </tr> </table> </div> </div> <?php //get data $button = @$_GET['submit']; $search = @$_GET['search']; if (!$button) { $search_1 = "No button clicked: <span class='search_string_text'>$search</span>"; $search_2 = "<div id='message'>Please type keyword, click the search button.</div>"; } else { if (strlen($search)<=2) { $search_1 = "No button clicked: <span class='search_string_text'>$search</span>"; $search_2 = "<div id='message'>Please type keyword, click the search button.</div>"; } else { $search_1 = "SEARCH RESULTS FOR: <span class='search_string_text'>$search</span>"; //connect to database $con = mysql_connect("localhost", dfsfdsffd, sdfsdfdsf); if (!$con) { header('Location: http://localhost:/report.php'); } mysql_select_db($databasename); //explode search term $search_exploded = explode(" ",$search); foreach($search_exploded as $search_each) { //construct query $x++; if ($x==1) @$construct .= "keywords LIKE '%$search_each%'"; else @$construct .= "OR keywords LIKE '%$search_each%'"; } //echo out construct $construct = "SELECT * FROM searchengine WHERE $construct"; $run = mysql_query($construct); $foundnum = mysql_num_rows($run); if ($foundnum==0) $search_info = "<div id='message'>Sorry, no results.</div>"; else { $search_number_found = "<div id='search_number_found'>$foundnum Product(s) found.</div>"; // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); // find how many results there are $total_results = mysql_num_rows($results); // user specifies column count $num_cols = 3; // calculate row count $num_rows = ceil($total_results / $num_cols); // initialise variable $output_html = ''; // loop through rows for($row = 0; $row < $num_rows; $row++) { // output table (or just <tr>'s if you prefer) $output_html .= "<div class='my_class'>"; $output_html .= "<table>"; $output_html .= "<tr>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; // output table (or row) footer $output_html .= "</tr>"; $output_html .= "</table>"; $output_html .= "</div>"; } // parse an individual row function get_stuff($row) { // check if mysql_fetch_assoc failed (there's no results left) if($row != FALSE) { $info = $row['info']; $title = $row['title']; $desc = $row['description']; $url = $row['url']; $stuff = "<div class='container'><div class='container2'>$info, $title, $desc, $url</div></div>"; } else $stuff = " "; return $stuff; } } } mysql_close($con); } ?> Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/ Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 I see views but no one has replied. Can anyone help me out with this? Did I not phrase the question correctly? I could really use some help on this issue. Thanks for any help given. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889874 Share on other sites More sharing options...
_DarkLink_ Posted August 3, 2009 Share Posted August 3, 2009 Okay so the results are not displayed like you want it to? So you mean that it is not displayed at all? If so, then try to echo $stuff. Such as replacing this: $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; With: $stuff = get_stuff(mysql_query($results)); echo $stuff; $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); echo $stuff; $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); echo $stuff; $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; Or maybe I have it wrong? Anyway, hope I helped and did not make any mistakes. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889907 Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 Okay so the results are not displayed like you want it to? So you mean that it is not displayed at all? If so, then try to echo $stuff. Such as replacing this: $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; With: $stuff = get_stuff(mysql_query($results)); echo $stuff; $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); echo $stuff; $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); echo $stuff; $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; Or maybe I have it wrong? Anyway, hope I helped and did not make any mistakes. Thank you for your response. I forgot to add that I have a div in my HTML that is echoing $output_html like this: <div class="contain"><?php echo $output_html; ?></div> There is definitely something wrong with the part of the script where the: // fetch results portion begins because everything else in my script works fine and echoes like it should. It's just not displaying the search results at all when I try to place them in the formatted table that I created. Sorry for the confusion. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889934 Share on other sites More sharing options...
gevans Posted August 3, 2009 Share Posted August 3, 2009 I'd say you're query is failing; if ($x==1) @$construct .= "keywords LIKE '%$search_each%'"; else @$construct .= "OR keywords LIKE '%$search_each%'"; } should be <?php if ($x==1) @$construct .= "keywords LIKE '%$search_each%' "; else @$construct .= "OR keywords LIKE '%$search_each%' "; } echo $construct and post what it does, before and after change. You should also be checking if the query was succefull or not in your script Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889942 Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 I'd say you're query is failing; if ($x==1) @$construct .= "keywords LIKE '%$search_each%'"; else @$construct .= "OR keywords LIKE '%$search_each%'"; } should be <?php if ($x==1) @$construct .= "keywords LIKE '%$search_each%' "; else @$construct .= "OR keywords LIKE '%$search_each%' "; } echo $construct and post what it does, before and after change. You should also be checking if the query was succefull or not in your script Thanks for the reply but I don't think that's the problem, my script works fine until the code below the: // fetch results line begins, because everything else in my script works and echoes like it should. It's just not displaying the search results at all when I try to place them in the formatted table that I created. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889957 Share on other sites More sharing options...
_DarkLink_ Posted August 3, 2009 Share Posted August 3, 2009 Hello again, I don't think you should be querying a query. Unless I'm wrong, This part, $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; Should be: $stuff = get_stuff($results); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff($results); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff($results); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; Since you have already done a query at: // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); Hope it helps. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889969 Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 Hello again, I don't think you should be querying a query. Unless I'm wrong, This part, $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff(mysql_query($results)); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; Should be: $stuff = get_stuff($results); $output_html .= "<td width='197' height='270' align='left' valign='top'>$stuff</td>"; $output_html .= "<td width='23'></td>"; $stuff = get_stuff($results); $output_html .= "<td width='197' align='center' valign='top'>$stuff</td>"; $output_html .= "<td width='21'></td>"; $stuff = get_stuff($results); $output_html .= "<td width='199' align='right' valign='top'>$stuff</td>"; Since you have already done a query at: // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); Hope it helps. Ok I will look at this, also does this query:// fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); cause issues with construct query right above it? Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-889991 Share on other sites More sharing options...
_DarkLink_ Posted August 3, 2009 Share Posted August 3, 2009 Normally, no. Since a query is a demand for something. So in the piece of code that I attempted to correct had a demand for a demand for something, which doesn't make any sense. And simply the $construct variable is not the same as the $results variable. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890006 Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 Normally, no. Since a query is a demand for something. So in the piece of code that I attempted to correct had a demand for a demand for something, which doesn't make any sense. And simply the $construct variable is not the same as the $results variable. Thanks for the help. I tried what you said but it still didn't display the actual results. Again, everything above this line: // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); displays and works just as it should, but removing the mysql_query text didn't make my results display. Any other suggestions? Thanks again for your help. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890029 Share on other sites More sharing options...
_DarkLink_ Posted August 3, 2009 Share Posted August 3, 2009 Hmm, Try removing those single quotes... Replace: // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); With: $results = mysql_query("SELECT * FROM searchengine"); Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890056 Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 Hmm, Try removing those single quotes... Replace: // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); With: $results = mysql_query("SELECT * FROM searchengine"); When I do that my entire screen goes blank and none of my code gets echoed at all. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890093 Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 Have you tried echoing the queries before you run them and make sure that they containt what you want them to contain? Also you have not declared $construct anywhere before you start concencating it with the LIKE clauses. You should also remove all '@' marks in front of everything to make sure they don't hide errors and also make sure you have error reporting on. Because I think atleast it will throw a notice or warning about the $construct and this variable does not work. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890126 Share on other sites More sharing options...
designer76 Posted August 3, 2009 Author Share Posted August 3, 2009 Have you tried echoing the queries before you run them and make sure that they containt what you want them to contain? Also you have not declared $construct anywhere before you start concencating it with the LIKE clauses. You should also remove all '@' marks in front of everything to make sure they don't hide errors and also make sure you have error reporting on. Because I think atleast it will throw a notice or warning about the $construct and this variable does not work. I haven't tried echoing the queries for the search results, but the $construct query does work because it displays the information from that query just as it should, I have tested that many times. My issues only start when I introduce the code from the: // fetch results $results = mysql_query("SELECT * FROM 'searchengine'"); line down, the code underneath that line is what isn't displaying at all. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890132 Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 So do you have error reporting on and you removed the '@'s ? And you can add to every mysql operation to show mysql errorin case there is errors to see what possibly went wrong (it will give you reason also). mysql_query($someSql) or die (mysql_error()); See if anything pops up.. Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890141 Share on other sites More sharing options...
designer76 Posted August 4, 2009 Author Share Posted August 4, 2009 I'm getting nothing guys. I have tried everything I could and yet it still isn't working. Does anyone have any other suggestions or notice anything wrong with the code I put in the original post? Link to comment https://forums.phpfreaks.com/topic/168651-php-search-form-script-help/#findComment-890242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.