I need to Replace portions of query results (mysql, php)

Joined
Jun 13, 2005
Messages
148
Reaction score
0
Hello and thank you!

I have been working with php for a few months on and off during school and I have gotten a decent profificency. In the past I have relied on your support, and I call on it once again.

I know how to query a mysql database and display and maniuplate any part of the results.

I can also use arrays to replace portions of strings to add or eliminate code,html etc.

For the life of me, I cannot figure how to build an array based on mysql results, and use those to parse further queries. (combine query with a str_replace)

EXAMPLE
each major has a code

Accounting 100001
Businesss 200001

etc.

Each student has a major code as one of their attributes.
John 200001 (Business Major)

I want to assign codes should the names change, all existings students will still be ok, because the code won't change.


So now I want all queries of students to change the variable assigned to major code, and switch it to a major name before outputing to the user.
The following code is inside a loop, it displays each students anme and major bfore moving on to the next student(in a while loop not shown)
PHP:
    $matching_students = mysql_query($sql_query) or die ("Error!: Can't access data.")
   $row  = mysql_fetch_array($matching_students);
    
    $student_name = $row[0];
     $student_major = $row[1];
    															
    
    
    echo "Name: ".$name."";
    																
    echo "Major: ".$major."";

Problem
What i want to do now, is take $major and send it through a function to trade its name and value.

I DONT want to run an sql query by the code because it would some 300 queries every page (1 for eeach student).

I would rather query the major table once, and build two arrrays I can use to 'clean' the student strings.

like this(only this dont work)
PHP:
    // grab avaialable majors and code from majors table
    $condition_values = mysql_query($sql_conditions);
    
    // 
    //  switch major code out for major name
// $result comes from a query in the MJRS table
    function translate($result,$string){
    	while ($row=mysql_fetch_row($result)){
    			$codes .= ", ".$row[0];
    			$majors .= ", "$row[1];
    			$readable = str_replace($codes, $majors, $string);
    
    		echo $code." and ".$category." and ".$readable."";
    	}
    }
    
    translate($major_values,$student_major);


does anyone know what im doing wrong, or know a better way?

Thank you very much for your help.
 
Last edited:
Joined
Jun 13, 2005
Messages
148
Reaction score
0
I figured it out!
This is great for replacing banned words in a forum, codes with titles, raw code with html friendly code, etc.

PHP:
function translate($result,$string){
	while ($row=mysql_fetch_row($result)){
		$code = array($row[0]);
		$text = array($row[1]);	
		
	}
		$readable = str_replace($code, $text, $string);
		echo $code." and ".$text." and ".$readable."<br />";
}
// first value is the results of any sql query to a table containg indexes and titles
// second value is the code to be replaced
translate($condition_values,$student_major);
 

Ian

Administrator
Joined
Feb 23, 2002
Messages
19,873
Reaction score
1,499
Glad you figured it out :) str_replace is a very useful too for sanitising variables, it's probably one of the few PHP commands I use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top