- 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.
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)
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)
does anyone know what im doing wrong, or know a better way?
Thank you very much for your help.
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.
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.I want to assign codes should the names change, all existings students will still be ok, because the code won't change.
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."";
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);
Thank you very much for your help.
Last edited: