我在这里的问题(可能)是$ db-> fetch_array不会显示db的所有结果。我不知道会发生什么,但我只得到了3个结果中的一个,我尝试了很多东西,甚至我改变了一些查询。你知道为什么我不能在这里得到所有结果吗?
这是vBulletin 3.8 btw。
谢谢大家。
if ($_REQUEST['do'] == 'showthis') {
$rel = $db->query_first("
SELECT *
FROM " . TABLE_PREFIX . "anexampletable
WHERE fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "
AND confirmstatus =1
");
if ($rel) {
$queryrel = $db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "anexampletable
WHERE fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "
AND confirmstatus =1
");
while ($queryre = $db->fetch_array($queryrel)) {
if ($queryre['reltype'] == '1') {
$ty = " is something else ";
} else if ($queryre['reltype'] == '2') {
$ty = " is something ";
} else if ($queryre['reltype'] == '3') {
$ty = " is something else ";
} else if ($queryre['reltype'] == '4') {
$ty = " is something ";
} else if ($queryre['reltype'] == '5') {
$ty = " is something ";
} else if ($queryre['reltype'] == '6') {
$ty = " is something ";
} else if ($queryre['reltype'] == '7') {
$ty = " is something ";
} else if ($queryre['reltype'] == '8') {
$ty = " is something ";
} else if ($queryre['reltype'] == '9') {
$ty = " is something ";
} else if ($queryre['reltype'] == '10') {
$ty = " is something ";
} else if ($queryre['reltype'] == '11') {
$ty = " is something else ";
} else if ($queryre['reltype'] == '12') {
$ty = " is something else ";
} else if ($queryre['reltype'] == '13') {
$ty = " is something ";
} else if ($queryre['reltype'] == '14') {
$ty = " is something ";
} else {
$ty = " is default ";
}
$sender = $queryre['fromusername'];
$receiver = $queryre['tousername'];
$showit = $sender . $ty . $receiver;
}
eval('print_output("' . fetch_template('relationships') . '");');
}
}
答案 0 :(得分:0)
您的查询是:
WHERE `fromuserid` has any value
OR (touserid = $vbulletin->userinfo['userid']).
如果您希望fromuserid
与用户ID匹配的行或touserid
与用户ID匹配,请尝试以下操作:
$queryrel = $db->query_read("
SELECT * FROM " . TABLE_PREFIX . "anexampletable
WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . ")
OR (touserid = " . $vbulletin->userinfo['userid'] . ")
AND confirmstatus =1
");
<强>更新强>
在不知道您正在使用的数据的情况下很难确定问题。我创建了一个输出您的代码正在使用的数据的测试,您将能够看到查询的各个部分返回的内容,然后可以确定问题所在。
通过将此代码放在示例中的代码之前暂时修改文件(您需要使用正确的表名)。然后编辑您的问题并将输出粘贴到底部。
echo "vBulletin User ID = " . $vbulletin->userinfo['userid'];
$test_query1 = $db->query_read("
SELECT * FROM " . TABLE_PREFIX . "anexampletable
WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . ")
");
$t1_count = 0;
echo "Test From User ID Results<br />";
while ($test1_output = $db->fetch_array($test_query1)) {
$t1_count++;
echo "Test From User Result " . $t1_count . "<br />";
echo "From User ID = " . $test1_output['fromuserid'] . "<br />";
echo "To User ID = " . $test1_output['touserid'] . "<br />";
echo "Confirm Status = " . $test1_output['confirmstatus'] . "<br />";
echo "Relationship Status = " . $test1_output['reltype'] . "<br />";
}
$test_query2 = $db->query_read("
SELECT * FROM " . TABLE_PREFIX . "anexampletable
WHERE (touserid = " . $vbulletin->userinfo['userid'] . ")
");
$t2_count = 0;
echo "<br /><br />Test To User ID Results<br />";
while ($test2_output = $db->fetch_array($test_query2)) {
$t2_count++;
echo "Test To User Result " . $t2_count . "<br />";
echo "From User ID = " . $test2_output['fromuserid'] . "<br />";
echo "To User ID = " . $test2_output['touserid'] . "<br />";
echo "Confirm Status = " . $test2_output['confirmstatus'] . "<br />";
echo "Relationship Status = " . $test2_output['reltype'] . "<br />";
}
exit();
最终代码?
似乎存在两个问题:
1)需要修改查询:
原件:
fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "
更新:
(fromuserid = " . $vbulletin->userinfo['userid'] . "
OR
touserid = " . $vbulletin->userinfo['userid'] . ")
更新日期07/05/2012
2)您无法在vb3模板中循环遍历数组,因此我们将连接字符串。
要在模板中使用的$ showit变量是一个字符串。每次连续遍历while循环都会覆盖它,以便只将最后一个结果发送到模板。不要使用$showit = xxx;
,请将$showit .= xxx;
与.=
一起使用。
我已经更新了以下代码的最后15行。
您可以查看论坛页面的生成方式以了解详情。
打开upload \ _ forumdisplay.php文件。创建线程列表的while
循环从这里开始:
上传\ forumdisplay.php(962)
while ($thread = $db->fetch_array($threads))
使用“threadbit”模板生成每个线程的输出,并在此处添加到$threadbit
字符串:
上传\ forumdisplay.php(1000)
eval('$threadbit .= "' . fetch_template('threadbit') . '";');
最后输出“FORUMDISPLAY”模板:
上传\ forumdisplay.php(1056)
eval('print_output("' . fetch_template('FORUMDISPLAY') . '");');
如果查看FORUMDISPLAY模板,您会发现$threadbit
字符串从一开始就使用了大约1/5。
尝试下面的代码,看看它是如何工作的,我用else if
语句替换了switch()
语句系列。效率更高。
if ($_REQUEST['do'] == 'showthis') {
// Make standalone query, easy to output query string and run it directly for testing
$rel_sql = "SELECT * FROM " . TABLE_PREFIX . "anexampletable
WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . "
OR touserid = " . $vbulletin->userinfo['userid'] . ")
AND confirmstatus =1";
$queryrel = $db->query_read($rel_sql);
if ($db->num_rows($queryrel))
{
while ($queryre = $db->fetch_array($queryrel))
{
switch ($queryre['reltype'])
{
case 1:
$ty = " do something 1 ";
break;
case 2:
$ty = " do something 2 ";
break;
case 3:
$ty = " do something 3 ";
break;
// Add as many cases as needed
.......
case xxx:
$ty = " do something xxx ";
break;
.......
default:
$ty = " is default ";
}
$sender = $queryre['fromusername'];
$receiver = $queryre['tousername'];
// UPDATED FROM HERE DOWN on 07/05/2012
// Add to $showit with ".=" rather than overwriting it with "=".
// Method One
// If the output is simple, try this.
// I added a line break after each entry.
$showit .= $sender . $ty . $receiver . "<br />";
OR
// Method Two
// If the output is complex.
// Create a separate template and store the output in $showit
// Remember to add the new template to the $actiontemplates array.
eval('$showit .= "' . fetch_template('showit') . '";');
}
eval('print_output("' . fetch_template('relationships') . '");');
}
}