• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1904
  • Last Modified:

mysql query on explode string to get names

I have a php mysql query and on the results, I end up with an array which I explode into a drop-down list. The list is of ID numbers.  The ID numbers relate to another table.  What I need to do is replace the ID numbers with the name... ie. ID 1 becomes apple and ID 2 becomes orange.
My tables (the parts that matter):
dealers.dealers_type
department.department_id
department.department_name
My dealers.dealers_type stores ~ separated values of  department.department_id.  I need to change the department.department_id to department.department_name.
Here is what I have which is working, just need to replace the id with the name from the department table:
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
echo '<option>'.$v.'</option>';
}

Open in new window

0
newbe101
Asked:
newbe101
  • 10
  • 8
  • 4
  • +1
2 Solutions
 
newbe101Author Commented:
So I need a query inside of a query.  Here is my main mysql query with has some unrelated stuff:
$query="SELECT * FROM dealers, dealers_location where dealers.dealers_location=dealers_location.dealers_location_id$orderby";
$result=mysql_query($query);
$num=mysql_numrows($result);
...
$i=0;
while ($i < $num) {$dealers_id=mysql_result($result,$i,"dealers_id");
$dealers_id=mysql_result($result,$i,"dealers_id");
...
++$i;
} 

Open in new window

0
 
CoyotesITCommented:
this is a basic join that you are trying to accomplish.

something such as

select a.id, b.name from id_table [a], name_table [b] where a.id = b.id

this would produce something like

id       name
1        apple
2        orange
1        apple
etc...

Then you can use the name column to get your values you need, however you may want to leave the value of the drop down as the id, since this is the value you store in your data table.

<select name="options">
<option value="1">Apple</option>
<option value="2">Orange</option>
</select>

Not sure if that example is relevant to what you are trying to do, but a simple join is all you need.

Good luck


0
 
Tyler LaczkoCommented:
your question is worded very difficult...

if you are looking to have the id as the value for the drop down and the name display

see attached
foreach($data as $k => $v)
{
echo '<option value="' . $v . '">'.$k.'</option>';
}

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
newbe101Author Commented:
My problem is that I need to do a query against my exploded string.  Please give me an example.
0
 
CoyotesITCommented:
I agree it is a little confusing. However I dont think he is wanting to store the counter in the value, but rather the actual id field from the select, the value should be the corresponding value which is stored in the separate table.

Your query:

$query="SELECT * FROM dealers, dealers_location where dealers.dealers_location=dealers_location.dealers_location_id$orderby";

is pulling back more data than you need by using (*) since you are wanting what looks to be the location you may try something like

SELECT dealers.location_id, dealers_location.id FROM dealers, dealers_location WHERE dealers.location_id = dealers_location.id

Then you can run your foreach assigning the values to the proper variables and create your <select>


0
 
CoyotesITCommented:
SELECT dealers.location_id, dealers_location.location FROM dealers, dealers_location WHERE dealers.location_id = dealers_location.id

changed dealers_location.id to dealers_location.location

sorry for the confusion.


0
 
Tyler LaczkoCommented:
coyetesit is not doing the join correctly.

can you please show us your tables that you are trying to get data from. also which tables you want to display and finally how the tables are connected. (which ids)
0
 
CoyotesITCommented:
"My problem is that I need to do a query against my exploded string.  Please give me an example."

I am not understanding why you need to query your results from your query. You should be able to get the results you want from a single query and manipulate the output of the result to whatever you need. You are building a standard drop down list.


0
 
newbe101Author Commented:
This is for display purpose only.  I am not going to do anything with the drop-down except show it... so I don't care about the values for it.  My original query shows all of the dealers.  The original query already is joining 2 tables to show the dealer location.  I think my query is fine and is working.  Part of my dealers query displays dealers.dealers_type which is ~ separated values of IDs for a completely separate table (departments).  I explode the dealers.dealers_type to create a dropdown for display purposes only.  What I need is to take the results of my string and query it to replace the string of IDs with names.
Maybe create an array with the string values then query the array to replace the IDs with the corresponding names.
To be clear, I have 3 tables that I am dealing with: dealers, dealers_location and departments.  I need to turn my dealers.dealers_type string (which is a string of departments.departments_id) into departments.departments_name
I hope this is more clear.
0
 
Tyler LaczkoCommented:
does this display the correct ids?

$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
echo '<option>'.$v.'</option>';
}
0
 
newbe101Author Commented:
Yes it does.  I was thinking something like this... But I don't know if loops inside of loops is a good idea or not.
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
 
$query="SELECT * FROM department WHERE department_id=$v ORDER BY department_name";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
$i=0;
while ($i < $num) {$department_id=mysql_result($result,$i,"department_id");
$department_id=mysql_result($result,$i,"department_id");
$department_name=mysql_result($result,$i,"department_name");
echo echo '<option>'.$department_name.'</option>';
++$i;
}
 
//echo '<option>'.$v.'</option>';
}

Open in new window

0
 
Tyler LaczkoCommented:
I would query the departments table for the id and name

using the id and name create an associative array

arrayDept = { 1=>"IT", 2=>"HR" }

Then you can address the associative array to get your values.


(Main reason for doin it this way is to only query the dept table once)
0
 
Tyler LaczkoCommented:
no do not loop inside a loop

0
 
newbe101Author Commented:
can you give me an example of your array idea?
0
 
newbe101Author Commented:
I can do a query to build the department array, but I wouldn't know how to compair the string to the array to get the names.
0
 
Tyler LaczkoCommented:
$query="SELECT dept_id, dept_name FROM department;"
$result=mysql_query($query);

$deptArray = array();
while($row = mysql_fetch_array())
{
$a1 = array($row[0] => $row[1]);
$deptArray = array_merge($a1, $deptArray);
}
0
 
Tyler LaczkoCommented:

while($row = mysql_fetch_array($result))
0
 
newbe101Author Commented:
I still don't see how I can compare the array against the string to get the names...  it looks like you just made the array for department.
0
 
NerdsOfTechTechnology ScientistCommented:
0
 
NerdsOfTechTechnology ScientistCommented:
onClick='doChange(src, val);'
Get new results from selection

?
0
 
Tyler LaczkoCommented:
now that you have the dept array you can index it using your ids

$deptArray[     yourID      ]

you can access all of the values in the associative array.
0
 
newbe101Author Commented:
I don't follow...  Something like this...
$query="SELECT * FROM department;"
$result=mysql_query($query);
 
$deptArray = array();
while($row = mysql_fetch_array($result))
{
$a1 = array($row[0] => $row[1]); // or $a1 = array($row[department_id] => $row[department_name]);
$deptArray = array_merge($a1, $deptArray);
}
 
//then
 
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
$deptArray[     $v      ]
}

Open in new window

0
 
newbe101Author Commented:
OK, since we now have the array for table departments, we now need to change the value of my string to the value of department_name if the value of the string == department_id of the array... here is what I have so far (which doesn't work)
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
if ( $v == $deptArray[$v] ) {
	echo '<option>'.$deptArray($row[department_name].'</option>';
}
//echo '<option>'.$v.'</option>';
}

Open in new window

0
 
newbe101Author Commented:
getting close... now it shows the first letter of the department_name... why is the rest of the word not showing?
$str = $dealers_type;
$str2 = substr($str, 0, -1);
$data = explode("~",$str2);
foreach($data as $k => $v)
{
echo '<option>'.$deptArray[$v][department_name].' '.$v.'</option>';
//echo '<option>'.$v.'</option>';
}

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
Create a follow up question so that we can solve this last issue.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now