vBulletin 3.8: How to display array values in templates - vbulletin

My problem in here (probably) is that $db->fetch_array won't show all the results from the db. I don't know what happens but I just get 1 of the 3 results, I tried many things even I changed the query a bit. Do you have any ideas why I can't get all the results in here?
It's for vBulletin 3.8 btw.
Thanks people.
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') . '");');
}
}

Your query is saying:
WHERE `fromuserid` has any value
OR (touserid = $vbulletin->userinfo['userid']).
If you want the rows where either fromuserid matches the userid or touserid matches the userid, try this:
$queryrel = $db->query_read("
SELECT * FROM " . TABLE_PREFIX . "anexampletable
WHERE (fromuserid = " . $vbulletin->userinfo['userid'] . ")
OR (touserid = " . $vbulletin->userinfo['userid'] . ")
AND confirmstatus =1
");
Update:
It's hard to determine the problem without knowing the data you're working with. I've created a test that outputs the data your code is working with, you'll be able to see what the individual parts of your query are returning and then can determine where the problem lies.
Modify your file temporarily by placing this code just before the code in your example (you'll need to use the correct table name). Then edit your question and paste the output at the bottom.
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();
Final Code?
It appears that there were two problems:
1) The query needed to be modified:
Original:
fromuserid OR touserid = " . $vbulletin->userinfo['userid'] . "
Updated:
(fromuserid = " . $vbulletin->userinfo['userid'] . "
OR
touserid = " . $vbulletin->userinfo['userid'] . ")
Updated 07/05/2012
2) You can't loop through arrays in vb3 templates, so we'll concatenate strings.
The $showit variable being output for use in the template is a string. It's being overwritten by each successive pass through the while loop so that only the last result is sent to the template. Instead of using $showit = xxx;, use $showit .= xxx; with .=.
I've updated the last 15 lines or so of the code below.
You can look at how the forum page is generated to learn more.
Open the upload\forumdisplay.php file. The while loop that creates the list of threads starts here:
upload\forumdisplay.php(962)
while ($thread = $db->fetch_array($threads))
The output for each thread is generated using the "threadbit" template and added to the $threadbit string here:
upload\forumdisplay.php(1000)
eval('$threadbit .= "' . fetch_template('threadbit') . '";');
The "FORUMDISPLAY" template is output at the end:
upload\forumdisplay.php(1056)
eval('print_output("' . fetch_template('FORUMDISPLAY') . '");');
If you look at the FORUMDISPLAY template, you'll find that the $threadbit string is used about 1/5 from the beginning.
Try the code below and see how it works, I replaced the series of else if statements with a switch() statement. It's more efficient.
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') . '");');
}
}

Related

page has Too many redirects from mail.php file?

I hope someone can help me. I have a page with a form and then the information
is emailed to me. the code below seems to be redirecting to the same page over and over again. It used to work for years now its not always working.
any help appreciated. Thanks Steve
<?php
//****************************************
//edit here
$senderName = 'WEB';
$senderEmail = 'site#example.com';
$targetEmail = 'contact#sellmydiabeticteststrips.com';
$messageSubject = 'Message from web-site';
$redirectToReferer = true;
$redirectURL = 'contact.html';
//****************************************
// mail content
$ufname = $_POST['ufname'];
$uaddress = $_POST['uaddress'];
$uemail = $_POST['uemail'];
$utel = $_POST['utel'];
$ucity = $_POST['ucity'];
$ustate = $_POST['ustate'];
$uzip = $_POST['uzip'];
$ubrand = $_POST['ubrand'];
$unumber = $_POST['unumber'];
$ucheck = $_POST['ucheck'];
$agree = $_POST['agree'];
// collect interests data
$interestsString = '';
for($i = 0; $i < count($interests); $i++) {
$interestsString .= $interests[$i].($i < count($interests) - 1 ? ', ' : '');
}
// prepare message text
$messageText = 'First Name: '.$ufname."\n".
'Address: '.$uaddress."\n".
'Email: '.$uemail."\n".
'Telephone: '.$utel."\n".
'City: '.$ucity."\n".
'State: '.$ustate."\n".
'Zip: '.$uzip."\n".
'Brand of test strips: '.$ubrand."\n".
'Number of boxes: '.$unumber."\n".
'Check: '.$ucheck."\n".
'Terms and Conditions: '.$agree."\n";
if($interestsString) {
$messageText .= 'Kinderen wonen bij mij: '.$interestsString."\n";
}
// send email
$senderName = "=?UTF-8?B?" . base64_encode($senderName) . "?=";
$messageSubject = "=?UTF-8?B?" . base64_encode($messageSubject) . "?=";
$messageHeaders = "From: " . $senderName . " <" . $senderEmail . ">\r\n"
. "MIME-Version: 1.0" . "\r\n"
. "Content-type: text/plain; charset=UTF-8" . "\r\n";
if (preg_match('/^[_.0-9a-z-]+#([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$targetEmail,$matches))
mail($targetEmail, $messageSubject, $messageText, $messageHeaders);
// redirect
if($redirectToReferer) {
header("Location: ".#$_SERVER['HTTP_REFERER'].'#sent');
} else {
header("Location: ".$redirectURL);
}
?>

Moodle Filters - GROUP BY

How can we get to work the moodle filters when GROUP BY - select form is used as filter mform field. I wasn't getting the desired output when I select the course. GROUP BY is not consider when sql_like function is called:
require_once($CFG->dirroot.'/filter_form.php');
$mform = new filter_form();
$coursefilter = '';
if ($formdata = $mform->get_data()) {
$coursefilter = $formdata->course;
}
$mform->set_data($formdata);
$mform->display();
$reporttable = new html_table();
$reporttable->head = array('course', 'users');
$reporttable->attributes['class'] = 'table';
$sql = "SELECT c.fullname, count(cc.userid) AS 'completed'
FROM {course_completions} cc JOIN {course} ON c.id = cc.course WHERE cc.timestarted > 0
GROUP BY c.fullname ";
$params = array();
if (!empty($coursefilter)) {
$params['fullname'] = '%' . $DB->sql_like_escape($coursefilter) . '%';
$sql .= " AND " . $DB->sql_like('c.fullname', ':fullname', false);
}
$mds = $DB->get_records_sql($sql, $params);
foreach ($mds as $m) {
$reporttable->data[] = new html_table_row(array(implode(array($m->fullname. $m->lastname)),
$m->completed ));
}
echo html_writer::table($reporttable);
Add the GROUP BY after the filter.
if (!empty($coursefilter)) {
$params['fullname'] = '%' . $DB->sql_like_escape($coursefilter) . '%';
$sql .= " AND " . $DB->sql_like('c.fullname', ':fullname', false);
}
$sql .= " GROUP BY c.fullname ";

pg_fetch_assoc returning 1 after values

I am beginner in PHP, i am working with session and pg functions. So i want to return user's first name and last name from database(basically user's details) and display it to different page using session. and it is returning using pg_fetch_assoc() but the problem is like it is showing 1 after every value like (firstname 1), (lastname 1). How can i fix it. is there any other way to return values from database and display it and also use these values as conditions.
Thanks
Below is my code:
$login = trim($_POST['login']);
$pass = trim($_POST['pass']);
$result = pg_execute($conn, "login_query", array($login, hash(HASH_ALGO, $pass)));
$records = pg_num_rows($result);
if($records == 1){
$_SESSION['user_id'] = $login;
$row = pg_fetch_assoc($result);
$_SESSION['first_name'] = $row['first_name'];
$_SESSION['last_name'] = $row['last_name'];
$_SESSION['last_access'] = $row['last_access'];
$_SESSION['user_type'] = $row['user_type'];
//$_SESSION['details'] = $row;
pg_execute($conn, "update_query", array($login));
/* $_SESSION['output'] = $output();*/
$_SESSION['output'] = "Welcome back " . pg_fetch_result($result,0,"first_name") . pg_fetch_result($result,0,"last_name").'</br>';
$_SESSION['output'] .= "Our records show that your </br>" . "email address is " . pg_fetch_result($result,0,"email_address") . "</br>";
$_SESSION['output'] .= "and you last accessed our system: " . pg_fetch_result($result,0,"last_access") . "</br>";
header('location: ./user-dashboard.php');
}
here is var_dump($row); value:
array(9) { ["user_id"]=> string(20) "jdoe " ["password"]=> string(32) "179ad45c6ce2cb97cf1029e212046e81" ["user_type"]=> string(2) "c " ["email_address"]=> string(256) "jdoe#gmail.com " ["first_name"]=> string(128) "John " ["last_name"]=> string(128) "Doe " ["birth_date"]=> string(10) "1998-02-05" ["enrol_date"]=> string(10) "2017-01-01" ["last_access"]=> string(10) "2017-10-18" }
database
pg_fetch_assoc returns values OK. you can see it in result of var_dump($row); - see the ["last_name"]=> string(128) "Doe " - it has extra space - yes, but not 1. So another bit of code adds 1 after $row["last_name"] value.
<h1 class="floating-box" ><?php echo print_r($_SESSION['first_name'],True); ?></h1>
<h1 class="floating-box" ><?php echo print_r($_SESSION['last_name']); ?></h1>
<h1 class="floating-box" ><?php echo print_r($_SESSION['last_access']); ?></h1>
Sorry, i forgot to add true in print_r() function. Thanks for your help. #Vao Tsun

PHP preg_replace supplement link URL pattern

I have the following URLs structure:
http://example.com/item/example-descriptions/6454986
http://example.com/item/example-bla-bla-bla/6545455
http://example.com/item/example-other-url/5454555
I need to add text to the numbers to get so (add "/demo/" and "?id=test")
http://example.com/item/example-descriptions/demo/6454986?id=test
http://example.com/item/example-bla-bla-bla/demo/6545455?id=test
http://example.com/item/example-other-url/demo/5454555?id=test
Here are a couple of ways imperfect:
$myurl = 'http://example.com/item/example-descriptions/6454986';
if (substr_count($myurl, 'example.com')){
$url = "$myurl.html";
$url = preg_replace('/^(.*)\/([^.]+)\.html$/','$1/demo/$2?id=test', $url);
echo "$url";
} else {
echo "$myurl";
}
and
$myurl = 'http://example.com/item/example-descriptions/6454986';
if (substr_count($myurl, 'example.com')){
$url = explode('/', $myurl);
echo "" . $url[0] . "/" . $url[1] . "/" . $url[2] . "/" . $url[3] . "/" . $url[4] . "/demo/" . $url[5] . "/?id=test";
} else {
echo "$myurl";
}
Help me improve the code.
You can use parse_url:
if (parse_url($myurl, PHP_URL_HOST) == 'example.com') {
$arr = explode('/', $myurl);
$arr[] = 'demo/' . array_pop($arr) . '?id=test';
$myurl = implode('/', $arr);
}

zend search lucence return hit id of search insted id of field

i have poblem zend search lucence :
zend search lucence return hit id of search insted id of field.for example:
i have this codde in Yii controller for create index of news data:
public function createNewsIndex()
{
setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
$index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles.'.news'), true);
$news= News::model()->findAll();
foreach ($news as $newsItem) {
$news_doc=new Zend_Search_Lucene_Document();
$news_doc->addField(Zend_Search_Lucene_Field::Text('id',CHtml::encode($newsItem->id),'utf-8'));
$news_doc->addField(Zend_Search_Lucene_Field::Text('title',CHtml::encode($newsItem->title),'utf-8'));
$news_doc->addField(Zend_Search_Lucene_Field::Text('keywords',CHtml::encode($newsItem->keywords),'utf-8'));
$index->addDocument($news_doc);
}
$index->commit();
$index->optimize();
}
i have this code for search news :
public function searchNews($term) {
setlocale(LC_CTYPE, 'de_DE.iso-8859-1');
Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
try
{
$index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles . '.news'));
}
catch(Zend_Search_Lucene_Exception $e)
{
$this->createNewsIndex();
$index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles . '.news'));
}
$query = Zend_Search_Lucene_Search_QueryParser::parse($term);
$results = $index->find($term . '*');
$this->render('search', array(
'results' => $results,
)
}
and this code in view:
<?php foreach ($results as $result)
{
print "ID: " . $result->id . "\n";
print "Score: " . $result->title . "\n<br>";
print CHtml::link(CHtml::encode($result->title), array($controller.'/view', 'id'=>$result->id));
}
?>
i want $result->id be id of this news title but I think this is a hit ID of I think this is a test Aht ID. hit in find function according this link:(see find function)
http://phpcrossref.com/zendframework/library/Zend/Search/Lucene.php.html
.
Sorry for weak English.
i fix problem by rename id field name:
$news_doc->addField(Zend_Search_Lucene_Field::Text('news_id',CHtml::encode($newsItem->id),'utf-8'));
and in view:
<?php foreach ($results as $result)
{
print "ID: " . $result->news_id . "\n";
print "Score: " . $result->title . "\n<br>";
print CHtml::link(CHtml::encode($result->title), array($controller.'/view', 'id'=>$result->news_id));
}
?>