Solved

mysql query on explode string to get names

Posted on 2008-10-29
25
1,865 Views
Last Modified: 2013-12-13
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
Comment
Question by:newbe101
  • 10
  • 8
  • 4
  • +1
25 Comments
 

Author Comment

by:newbe101
Comment Utility
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
 
LVL 8

Assisted Solution

by:CoyotesIT
CoyotesIT earned 100 total points
Comment Utility
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
 
LVL 10

Accepted Solution

by:
Tyler Laczko earned 400 total points
Comment Utility
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
 

Author Comment

by:newbe101
Comment Utility
My problem is that I need to do a query against my exploded string.  Please give me an example.
0
 
LVL 8

Expert Comment

by:CoyotesIT
Comment Utility
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
 
LVL 8

Expert Comment

by:CoyotesIT
Comment Utility
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
 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility
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
 
LVL 8

Expert Comment

by:CoyotesIT
Comment Utility
"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
 

Author Comment

by:newbe101
Comment Utility
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
 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility
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
 

Author Comment

by:newbe101
Comment Utility
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
 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility
no do not loop inside a loop

0
 

Author Comment

by:newbe101
Comment Utility
can you give me an example of your array idea?
0
 

Author Comment

by:newbe101
Comment Utility
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
 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility
$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
 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility

while($row = mysql_fetch_array($result))
0
 

Author Comment

by:newbe101
Comment Utility
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
 
LVL 19

Expert Comment

by:NerdsOfTech
Comment Utility
0
 
LVL 19

Expert Comment

by:NerdsOfTech
Comment Utility
onClick='doChange(src, val);'
Get new results from selection

?
0
 
LVL 10

Expert Comment

by:Tyler Laczko
Comment Utility
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
 

Author Comment

by:newbe101
Comment Utility
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
 

Author Comment

by:newbe101
Comment Utility
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
 

Author Comment

by:newbe101
Comment Utility
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
 
LVL 19

Expert Comment

by:NerdsOfTech
Comment Utility
Create a follow up question so that we can solve this last issue.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now