Jump to content

[SOLVED] php/Query help please


grumpy

Recommended Posts

I have two tables, one stores categories (cat_relations) relative to the contractor's usr_id, and another (contractors) stores user info, including zip codes. The zip codes are in comma-separated strings in a row. The idea is that a site visitor selects a category and then they enter a zip. Every contractor's info should be displayed who's usr_id has a matching category_id in the cat_relations table which is selected by the site visitor and whose zip matches the zip entered by the site visitor. In other words:  I need the query to say: get the category ID that the site user selects and the zip code that the site user entered, and then find all the contractors who meet two criteria: has a relation_cat that matches the category ID and whose zip code matches the zip entered, and display their company name (co_name). But I can't figure out to do it. My latest attempt is below. I get results but they are not correct. They seem to only go by the zip. What am I doing wrong? Thanks.

 

$query = mysql_query("select * from cat_relations,contractors where contractors.co_workzip1 like '%$zip%' and cat_relations.relation_cat = '$ID'"); 

$result = $query or die ("Error:" . mysql_error());

if(mysql_num_rows($result) == 0)

{

echo ("<b>No Results Found</b>");

exit;

}

while ($row = mysql_fetch_array($result))

{

 

 

echo $row["co_name"];

}

Link to comment
Share on other sites

$query = mysql_query("SELECT contractors.co_workzip1,cat_relations.relation_cat from cat_relations INNER JOIN contractors ON WHERE contractors.co_workzip1 LIKE '%$zip%' AND cat_relations.relation_cat = '$ID'") or die ("Error:" . mysql_error()); 
if(!mysql_num_rows($query))//0 = false, any other number = true
{
echo ("No Results Found");
exit;
} else {//add your else statement, to prevent errors while reading this, just in case php doesn't "die"
echo "<ol>";
while ($row = mysql_fetch_array($query))
{
echo "<li>".$row["co_name"]."</li>";
}
echo "</ol>";
}

 

Try that, I removed the $result variable. I fixed your if/else statement a little bit. I moved the or die() part into the end of the mysql_query [where it should belong]. Added a bit of html to display the results in an ordered list, easier to read.

 

EDIT: Didn't realize that you were doing two diff tables, remodified, added the INNER JOIN statement which reads two tables at once really The modified code should work perfectly though ;-)

Link to comment
Share on other sites

Hello-

Thanks to everyone for their help. But I think I just figured it out. Only took me 6 hours :) I wasn't relating the usr_ids from the two tables.

Instead of:

$query = mysql_query("select * from cat_relations,contractors where contractors.co_workzip1 like '%$zip%' and cat_relations.relation_cat = '$ID'"); 

 

This seems to work:

$query = mysql_query("select * from cat_relations,contractors where contractors.co_workzip1 like '%$zip%' and cat_relations.relation_cat = '$ID' and cat_relations.relation_usr_id = contractors.usr_id"); 

 

 

 

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.