<?php
foreach ($myassignment->result() as $e){
$courseId=$e->courseId;
$getassign=$this->Usermodel->getmysubassignment($courseId);
if($getassign->num_rows()==''){
print_r('no rows');
} else {
print_r('some rows');
}
}
?>
在上面的代码中,$ myassignment包含2个courseId&#39; s。在第一个课程中,我不会使用$getassign=$this->Usermodel->getmysubassignment($courseId);
从DB返回任何内容,而第二个返回内容。但它打印no rows
和some rows
,但我只需要some rows
。怎么办?
答案 0 :(得分:1)
您无法对循环的未来迭代作出反应。只需将输出移出循环:
#attempt at Guess Who!
import random
class Character:
def __init__(self, g, h, e, s, n):
self.hasGlasses = g
self.hairColor = h
self.eyeColor = e
self.gender = s
self.name = n
def __repr__(self):
return "{}, {}, {} eyes, {} glasses, {} hair".format(self.name, self.gender, self.eyeColor, self.hasGlasses, self.hairColor)
def checkEC(self, x):
result = False
if (self.eyeColor.lower() == str(x).lower()):
result = False
return result
def checkGlasses(self, x):
result = False
if (self.hasGlasses.lower() == str(x).lower()):
result = True
return result
def checkHC(self, x):
result = False
if (self.hairColor.lower() == str(x).lower()):
result = True
return result
def checkG(self, x):
result = False
if (self.gender.lower() == str(x).lower()):
result = True
return result
possibleChars = []
rob = Character("yes", "blonde", "green", "boy", "Rob")
possibleChars.append(rob)
susan = Character("no", "red", "blue", "girl", "Susan")
possibleChars.append(susan)
pat = Character("no", "brown", "brown", "boy", "Pat")
possibleChars.append(pat)
shane = Character("no", "brown", "blue", "boy", "Shane")
possibleChars.append(shane)
ben = Character("no", "blonde", "green", "boy", "Ben")
possibleChars.append(ben)
angello = Character("yes", "blonde", "hazel", "girl", "Angelica")
possibleChars.append(angello)
questions = ["What color is your hair? ", "Do you wear glasses? ", "Are you a boy or a girl? ",
"What color are your eyes? "
];
counter = 0
guessed = False
qsAsked = []
charsLeft = []
for i in possibleChars:
print(i)
while not guessed:
y = random.randint(0, len(questions) - 1)
print(int(y))
if (counter == 0):
if (y in qsAsked):
guessed = False
elif(y == 0):
x = input(questions[int(y)])
for character in possibleChars:
if (character.checkHC(str(x))):
charsLeft.append(character)
qsAsked.append(0)
for i in possibleChars:
print(i)
counter = counter + 1
elif(y == 1):
x = input(questions[int(y)])
for character in possibleChars:
if (character.checkGlasses(str(x))):
charsLeft.append(character)
qsAsked.append(1)
for i in possibleChars:
print(i)
counter = counter + 1
elif(y == 2):
x = input(questions[int(y)])
for character in possibleChars:
if (character.checkG(str(x))):
charsLeft.append(character)
qsAsked.append(2)
for i in possibleChars:
print(i)
counter = counter + 1
elif(y == 3):
x = input(questions[int(y)])
for character in possibleChars:
if (character.checkEC(str(x))):
charsLeft.append(character)
qsAsked.append(3)
for i in possibleChars:
print(i)
counter = counter + 1
elif (counter > 0) :
if (y in qsAsked):
guessed = False
elif(y == 0):
x = input(questions[int(y)])
for character in charsLeft:
if (not character.checkHC(str(x))):
charsLeft.remove(character)
if (len(charsLeft) == 1):
print("Your character must be {}!".format(charsLeft[0]))
guessed = True
qsAsked.append(0)
for i in charsLeft:
print(i)
elif(y == 1):
x = input(questions[int(y)])
for character in charsLeft:
if (not character.checkGlasses(str(x))):
charsLeft.remove(character)
if (len(charsLeft) == 1):
print("Your character must be {}!".format(charsLeft[0]))
guessed = True
qsAsked.append(1)
for i in charsLeft:
print(i)
elif(y == 2):
x = input(questions[int(y)])
for character in charsLeft:
if (not character.checkG(str(x))):
charsLeft.remove(character)
if (len(charsLeft) == 1):
print("Your character must be {}!".format(charsLeft[0]))
guessed = True
qsAsked.append(2)
for i in charsLeft:
print(i)
elif(y == 3):
x = input(questions[int(y)])
for character in charsLeft:
if (not character.checkEC(str(x))):
charsLeft.remove(character)
if (len(charsLeft) == 1):
print("Your character must be {}!".format(charsLeft[0]))
guessed = True
qsAsked.append(3)
for i in charsLeft:
print(i)
答案 1 :(得分:1)
使用flag
代替
$flag = FALSE;// set if false
foreach ($myassignment->result() as $e) {
$courseId = $e->courseId;
$getassign = $this->Usermodel->getmysubassignment($courseId);
if ($getassign->num_rows() > 0) {
$flag = TRUE;// if row then set it true
}
}// end of foreach loop
if ($flag) {
print_r('some rows');
} else {
print_r('no rows');
}