Jump to content

Searching Multiple Tables AND $metode Like'%$search%


JTapp

Recommended Posts

I'm trying to search through two tables with a common ID field.  I'm trying to return results from a search.html page using

$metode LIKE '%$search%

but I can't seem to get the code right.  It seems like I'm really close.. can somebody please take a look? 

Here is my latest code - I've changed my tablenames and fieldnames to make it easier for you to read:

 

$query = mysql_query("SELECT table1.field1, table1.field2, table2.field1, table2.field2 FROM table1, table2 WHERE $metode LIKE '%$search%' LIMIT 0, 50");

while ($row = @mysql_fetch_array($query))

 

{

$variable1=$row["field1"];

$variable2=$row["field2"];

$variable3=$row["field3"];

$variable4=$row["field4"];

 

//table layout for results

 

print ("<tr>");

echo "<td class=\"td_id\"><strong>$variable1</strong></td>\n";

print ("<td>$variable1</td>");

print ("<td>$variable2</td>");

print ("<td>$variable3</td>");

print ("<td>$variable4</td>");

print ("</tr>");

}

Link to comment
Share on other sites

First off, what does the variable $metode contain? I'm assuming it's a field name in the database, but from which table?

 

Your query should look similar to this:

 

SELECT 
t1.field1, t1.field2, t2.field1, t2.field2 FROM table1 t1
LEFT JOIN table2 t2
ON t1.ID_field = t2.ID_field
WHERE $metode LIKE '%$search%' 
LIMIT 0, 50

Link to comment
Share on other sites

Thanks soooo much for taking the time.

I'm such a beginner.. so my apologies for ignorance.

I think this is what you are asking me for:

 

My search.html file code is:

 

 

<p align="center">

<select name="metode" size="1">

<option value="table1field1">table1field1</option>

<option value="table1field2">table1field2</option>

<option value="table2field1">table2field2</option>

<option value="table2field2">table2field2</option>

</select> <input type="text" name="search" size="25">  <br>

Search database: <input type="submit" value="Go!!" name="Go"></p>

</td>

Link to comment
Share on other sites

Lets take one of your HTML options for example

<option value="table1field1">table1field1</option>

 

For the value of a table1 field, you are going to want to put this:

value="t1.field_name"

and just replace the "field_name" with whatever the fields name is. For a field in table2, you just want to put t2 instead of t1.

 

I'm not positive this is going to work, but it's worth a shot. So do what I told you, and use the query I supplied in my last post. Just make sure you define the variable $metode and $search before using them in the query.

Link to comment
Share on other sites

pocobueno1388- I'm testing your suggestion but don't understand the information I've highlighted in blue.  This seems redundant.. can you clarify for me?  Thanks again.

 

t1.field1, t1.field2, t2.field1, t2.field2 FROM table1 t1

LEFT JOIN table2 t2

ON t1.ID_field = t2.ID_field

WHERE $metode LIKE '%$search%'

LIMIT 0, 50

 

 

*** GOT YOUR LAST POST WHILE I WAS TYPING.. WILL TRY IT, THANKS **

Link to comment
Share on other sites

Ok, I removed the t1 and the t2 in the blue font and your solution worked.

Thank you very much.  You fixed something in 5 minutes that I struggled with for the past 48 hours.

 

On another note - my guru - can you direct me to a tutorial or a site or a posting that can help me to format my database results?

I want to make some fields results in a larger font, insert a paragraph break here and there.. etc.

I found how to insert a table.. but I require more than that.

I keep trying to exit PHP and go into HTML but it seems so incorrect...

 

If you can't help or don't have time, no worries.

I thank you so much for the assistance you've already provided.

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.