JTapp Posted February 17, 2008 Share Posted February 17, 2008 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>"); } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 17, 2008 Share Posted February 17, 2008 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 Quote Link to comment Share on other sites More sharing options...
JTapp Posted February 17, 2008 Author Share Posted February 17, 2008 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> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 17, 2008 Share Posted February 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
JTapp Posted February 17, 2008 Author Share Posted February 17, 2008 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 ** Quote Link to comment Share on other sites More sharing options...
JTapp Posted February 17, 2008 Author Share Posted February 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 17, 2008 Share Posted February 17, 2008 Awesome, I'm glad it worked You can insert HTML right in with the PHP, here is a snippet that may help you. Although I'm not exactly sure what your looking for. http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment Share on other sites More sharing options...
JTapp Posted February 17, 2008 Author Share Posted February 17, 2008 Sweeet! I can finally go to bed! Have a great night! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.