Jump to content

PHP Search Form Script Help!?!?


designer76

Recommended Posts

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 :facewall: 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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.