I have a tutor database (MySQL) which store tutor available subjects, example:- tutor1 available subject = A,B,C tutor2 available subject = A,B tutor3 available subject = A
in SearchTutor.php;
<select id="select3" class="medium" name="upsr">
<option selected="selected" value="%">-</option>
<option value="A">A only</option>
<option value="A,B">A and B only</option>
<option value="A,B,C">All Subjects</option>
</select>
in DisplayTutor.php;
$subjectin = '';
$subdescription = '';
if(strlen($upsr) > 1)
{
$subjectin .= "'106',";
if($upsr == "A")
$subdescription .= "'A',";
else if($upsr == "A,B")
$subdescription .= "'A','B',";
else if($upsr == "A,B,C")
$subdescription .= "'A','B','C',";
else
$subdescription .= "'".$upsr."',";
}
so.. what i want is, when i choose "A only", the result should be tutor3, because i want "A only", those tutor with A,B,C is not qualify, same goes to when i choose "All Subjects", the result should be tutor1, because only tutor1 have all subjects,
but in my cod开发者_运维技巧ing, i know it was wrong, is there anyone can help me solve this ?
Thank you all programmer, ^^
You can change this Part, you get the result as you need i think so
$subjectin = '';
$subdescription = '';
if(strlen($upsr) > 0)
{
$subjectin .= "'106',";
if($upsr == "A")
$subdescription .= "'A',";
else if($upsr == "A,B")
$subdescription .= "'A','B',";
else if($upsr == "A,B,C")
$subdescription .= "'A','B','C',";
else
$subdescription .= "'".$upsr."',";
}
Because in first result of A you get string length as 1, so you can't get anything. Try now.
$arr=array('A'=>'tutor3','A,B'=>'tutor2','A,B,C'=>'tutor1') echo(arra[$upsr]);
You can't achieve this too easily (only way I know is exists sub-query, but I claim that what I propose is more readable and easier to dynamically construct according to the selection) with value in ('A', 'B', 'C') assuming you have these in a different table with some column referencing the tutor and subject tables.
What I propose you do is to join the second table more than once with left join. I'm just throwing the table names, substitute with the correct ones. Also fix the referencing column names
select othercolumns,
s1.description as DESCA,
s2.description as DESCB,
s3.description as DESCC
from tutor t
left join subject s1 on s1.description = 'A' and s1.idref = t.id
left join subject s2 on s2.description = 'B' and s2.idref = t.id
left join subject s3 on s3.description = 'C' and s3.idref = t.id
In the where -clause you check that the ones you want are null and the others aren't. For A only:
where DESCA is not null and DESCB is null and DESCC is null
for A and B:
where DESCA is not null and DESCB is not null and DESCC is null
I think you get the idea. This shouldn't be too difficult to "convert" to PHP if's and elses.
精彩评论