I need help, I am total Newbie in php. I buy code and it is not generating the report which I need. I have a peace of code with missing user Id in report. I could have wrote something but it will due to lack of my knowledge last forever... I really appreciate your help.
There are few kind of ID in code... USER ID, PACKET PALNS ID, ADS ID, PAYMENT ID... in final report depending on what user choose, system generate bugged report with a) POINTS PLAN ID b) ADS ID and send to mail C) PAYMENT ID....I need to change the code that in all of these 3 kinds of reports have one more id - USER ID
<?php
/**
* You should have received a copy of the GNU General Public License
* along with DJ Classifieds. If not, see <http://www.gnu.org/licenses/>.
*
*/
defined('_JEXEC') or die('Restricted access');
jimport('joomla.event.plugin');
if(!defined("DS")){ define('DS',DIRECTORY_SEPARATOR);}
$lang = JFactory::getLanguage();
$lang->load('plg_djclassifiedspayment_djcfBankTransfer',JPATH_ADMINISTRATOR);
require_once(JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_djclassifieds'.DS.'lib'.DS.'djnotify.php');
class plgdjclassifiedspaymentdjcfBankTransfer extends JPlugin
{
function plgdjclassifiedspaymentdjcfBankTransfer( &$subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage('plg_djcfBankTransfer');
$params["plugin_name"] = "djcfBankTransfer";
$params["icon"] = "banktransfer_icon.jpg";
$params["logo"] = "banktransfer_overview.jpg";
$params["description"] = JText::_("PLG_DJCFBANKTRANSFER_PAYMENT_METHOD_DESC");
$params["payment_method"] = JText::_("PLG_DJCFBANKTRANSFER_PAYMENT_METHOD_NAME");
$params["pay_info"] = $this->params->get("pay_info");
$this->params = $params;
}
function onPaymentMethodList($val)
{
if($val["direct_payment"]){
return null;
}
$type='';
if($val['type']){
$type='&type='.$val['type'];
}
$html ='';
$user = JFactory::getUser();
if($this->params["pay_info"]!=''){
$paymentLogoPath = JURI::root()."plugins/djclassifiedspayment/".$this->params["plugin_name"]."/".$this->params["plugin_name"]."/images/".$this->params["logo"];
//$form_action = JRoute :: _("index.php?option=com_djclassifieds&task=processPayment&ptype=".$this->params["plugin_name"]."&pactiontype=process&id=".$val["id"].$type, false);
$form_action = JURI::root()."index.php?option=com_djclassifieds&task=processPayment&ptype=".$this->params["plugin_name"]."&pactiontype=process&id=".$val["id"].$type;
$html ='<table cellpadding="5" cellspacing="0" width="100%" border="0">
<tr>';
if($this->params["logo"] != ""){
$html .='<td class="td1" width="160" align="center">
<img src="'.$paymentLogoPath.'" title="'.$this->params["payment_method"].'"/>
</td>';
}
$html .='<td class="td2">
<h2>'. $this->params["payment_method"].'</h2>
<p style="text-align:justify;">'.$this->params["description"].'</p>
</td>
<td class="td3" width="130" align="center">
<a class="button" style="text-decoration:none;" href="'.$form_action.'">'.JText::_('COM_DJCLASSIFIEDS_BUY_NOW').'</a>
</td>
</tr>
</table>';
}
return $html;
}
function onProcessPayment()
{
$ptype = JRequest::getVar('ptype','');
$id = JRequest::getInt('id','0');
$html="";
if($ptype == $this->params["plugin_name"])
{
$action = JRequest::getVar('pactiontype','');
switch ($action)
{
case "process" :
$html = $this->process($id);
break;
case "notify" :
$html = $this->_notify_url();
break;
case "paymentmessage" :
$html = $this->_paymentsuccess();
break;
default :
$html = $this->process($id);
break;
}
}
return $html;
}
function process($id)
{
$db = JFactory::getDBO();
$app = JFactory::getApplication();
$Itemid = JRequest::getInt("Itemid",'0');
$par = JComponentHelper::getParams( 'com_djclassifieds' );
$user = JFactory::getUser();
$config = JFactory::getConfig();
$ptype = JRequest::getVar('ptype');
$type = JRequest::getVar('type','');
$row = JTable::getInstance('Payments', 'DJClassifiedsTable');
if($type=='prom_top'){
$query ="SELECT i.* FROM #__djcf_items i "
."WHERE i.id=".$id." LIMIT 1";
$db->setQuery($query);
$item = $db->loadObject();
if(!isset($item)){
$message = JText::_('COM_DJCLASSIFIEDS_WRONG_AD');
$redirect="index.php?option=com_djclassifieds&view=items&cid=0";
}
$row->item_id = $id;
$row->user_id = $user->id;
$row->method = $ptype;
$row->status = 'Start';
$row->ip_address = $_SERVER['REMOTE_ADDR'];
$row->price = $par->get('promotion_move_top_price',0);
$row->type=2;
$row->store();
$amount = $par->get('promotion_move_top_price',0);
$itemname = $item->name;
$item_id = $row->id;
$item_cid = '&cid='.$item->cat_id;
}else if($type=='points'){
$query ="SELECT p.* FROM #__djcf_points p "
."WHERE p.id=".$id." LIMIT 1";
$db->setQuery($query);
$points = $db->loadObject();
if(!isset($item)){
$message = JText::_('COM_DJCLASSIFIEDS_WRONG_POINTS_PACKAGE');
$redirect="index.php?option=com_djclassifieds&view=items&cid=0";
}
$row->item_id = $id;
$row->user_id = $user->id;
$row->method = $ptype;
$row->status = 'Start';
$row->ip_address = $_SERVER['REMOTE_ADDR'];
$row->price = $points->price;
$row->type=1;
$row->store();
$amount = $points->price;
$itemname = $points->name;
$item_id = $row->id;
$item_cid = '';
}else if($type=='plan'){
$query ="SELECT p.* FROM #__djcf_plans p "
."WHERE p.id=".$id." LIMIT 1";
$db->setQuery($query);
$plan = $db->loadObject();
if(!isset($plan)){
$message = JText::_('COM_DJCLASSIFIEDS_SUBSCRIPTION_PLAN');
$redirect="index.php?option=com_djclassifieds&view=plans";
}
$row->item_id = $id;
$row->user_id = $user->id;
$row->method = $ptype;
$row->status = 'Start';
$row->ip_address = $_SERVER['REMOTE_ADDR'];
$row->price = $plan->price;
$row->type=3;
$row->store();
$amount = $plan->price;
$itemname = $plan->name;
$item_id = $row->id;
$item_cid = '';
}else{
$query ="SELECT i.*, c.price as c_price, c.alias as c_alias FROM #__djcf_items i "
."LEFT JOIN #__djcf_categories c ON c.id=i.cat_id "
."WHERE i.id=".$id." LIMIT 1";
$db->setQuery($query);
$item = $db->loadObject();
if(!isset($item)){
$message = JText::_('COM_DJCLASSIFIEDS_WRONG_AD');
$redirect="index.php?option=com_djclassifieds&view=items&cid=0";
}
$amount = 0;
if(strstr($item->pay_type, 'cat')){
$amount += $item->c_price/100;
}
if(strstr($item->pay_type, 'type,')){
$itype = DJClassifiedsPayment::getTypePrice($item->user_id,$item->type_id);
$amount += $itype->price;
}
$query = "SELECT * FROM #__djcf_days d "
."WHERE d.days=".$item->exp_days." LIMIT 1";
$db->setQuery($query);
$day = $db->loadObject();
if(strstr($item->pay_type, 'duration_renew')){
$amount += $day->price_renew;
}else if(strstr($item->pay_type, 'duration')){
$amount += $day->price;
}
if(strstr($item->pay_type, 'extra_img_renew')){
if($day->img_price_default){
$amount += $par->get('img_price_renew','0')*$item->extra_images_to_pay;
}else{
$amount += $day->img_price_renew*$item->extra_images_to_pay;
}
}else if(strstr($item->pay_type, 'extra_img')){
if($day->img_price_default){
$amount += $par->get('img_price','0')*$item->extra_images_to_pay;
}else{
$amount += $day->img_price*$item->extra_images_to_pay;
}
}
if(strstr($item->pay_type, 'extra_chars_renew')){
if($day->char_price_default){
$amount += $par->get('desc_char_price_renew','0')*$item->extra_chars_to_pay;
}else{
$amount += $day->char_price_renew*$item->extra_chars_to_pay;
}
}else if(strstr($item->pay_type, 'extra_chars')){
if($day->char_price_default){
$amount += $par->get('desc_char_price','0')*$item->extra_chars_to_pay;
}else{
$amount += $day->char_price*$item->extra_chars_to_pay;
}
}
$query = "SELECT p.* FROM #__djcf_promotions p "
."WHERE p.published=1 ORDER BY p.id ";
$db->setQuery($query);
$promotions=$db->loadObjectList();
foreach($promotions as $prom){
if(strstr($item->pay_type, $prom->name)){
$amount += $prom->price;
}
}
/*$query = 'DELETE FROM #__djcf_payments WHERE item_id= "'.$id.'" ';
$db->setQuery($query);
$db->query();
$query = 'INSERT INTO #__djcf_payments ( item_id,user_id,method, status)' .
' VALUES ( "'.$id.'" ,"'.$user->id.'","'.$ptype.'" ,"Start" )'
;
$db->setQuery($query);
$db->query();*/
$row->item_id = $id;
$row->user_id = $user->id;
$row->method = $ptype;
$row->status = 'Start';
$row->ip_address = $_SERVER['REMOTE_ADDR'];
$row->price = $amount;
$row->type=0;
$row->store();
$itemname = $item->name;
$item_id = $row->id;
$item_cid = '&cid='.$item->cat_id;
$item_slug = $item->id.':'.$item->alias;
$cat_slug = $item->cat_id.':'.$item->c_alias;
}
/*
$query ="SELECT i.*, c.price as c_price, c.alias as c_alias FROM #__djcf_items i "
."LEFT JOIN #__djcf_categories c ON c.id=i.cat_id "
."WHERE i.id=".$id." LIMIT 1";
$db->setQuery($query);
$item = $db->loadObject();
if(!isset($item)){
$message = JText::_('COM_DJCLASSIFIEDS_WRONG_AD');
$redirect="index.php?option=com_djclassifieds&view=items&cid=0";
}
$query = 'DELETE FROM #__djcf_payments WHERE item_id= "'.$id.'" ';
$db->setQuery($query);
$db->query();
$query = 'INSERT INTO #__djcf_payments ( item_id,user_id,method, status)' .
' VALUES ( "'.$id.'" ,"'.$user->id.'","'.$ptype.'" ,"Start" )'
;
$db->setQuery($query);
$db->query();
$amount = 0;
if(strstr($item->pay_type, 'cat')){
$amount += $item->c_price/100;
}
if(strstr($item->pay_type, 'duration_renew')){
$query = "SELECT d.price_renew FROM #__djcf_days d "
."WHERE d.days=".$item->exp_days;
$db->setQuery($query);
$amount += $db->loadResult();
}else if(strstr($item->pay_type, 'duration')){
$query = "SELECT d.price FROM #__djcf_days d "
."WHERE d.days=".$item->exp_days;
$db->setQuery($query);
$amount += $db->loadResult();
}
$query = "SELECT p.* FROM #__djcf_promotions p "
."WHERE p.published=1 ORDER BY p.id ";
$db->setQuery($query);
$promotions=$db->loadObjectList();
foreach($promotions as $prom){
if(strstr($item->pay_type, $prom->name)){
$amount += $prom->price;
}
}
*/
if($user->id){
/*$mailto = $user->email;
$mailfrom = $app->getCfg( 'mailfrom' );
$fromname=$config->get('config.sitename').' - '.str_ireplace('administrator/', '', JURI::base());
$subject = JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_INFRORMATIONS').' '.$config->get('config.sitename');
$m_message = JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_INFRORMATIONS').' '.$config->get('config.sitename')."<br /><br />";
if($type=='points'){
$m_message .= JText::_('PLG_DJCFBANKTRANSFER_POINTS_PACKAGE').': '.$itemname."<br /><br />";
}else{
$m_message .= JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_AD_NAME').': '.$itemname."<br /><br />";
}
$m_message .= JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_PRICE_TO_PAY').': '.$amount.' '.$par->get('unit_price','')."<br /><br />";
$m_message .= JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_PAY_INFORMATION').': <br /><br />'.JHTML::_('content.prepare',nl2br($this->params["pay_info"]))."<br /><br />";
if($type==''){
$u = JURI::getInstance( JURI::base() );
$link= $u->getHost().JRoute::_(DJClassifiedsSEO::getItemRoute($item_slug,$cat_slug));
$m_message .=JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_AD_LINK').': '.$link.'<br /><br />';
$m_message .=JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_AD_ID').': '.$id.'<br /><br />';
}
$m_message .=JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_ID').': '.$item_id;
$mailer = JFactory::getMailer();
$mailer->sendMail($mailfrom, $fromname, $mailto, $subject, $m_message,$mode=1);*/
$payment_info = array();
$payment_info['id'] = $item_id;
$payment_info['itemname'] = $itemname;
$payment_info['amount'] = DJClassifiedsTheme::priceFormat($amount,$par->get('unit_price',''));
$payment_info['info'] = $this->params["pay_info"];
DJClassifiedsNotify::notifyUserPayment($type,$id,$payment_info);
}
echo '<div id="dj-classifieds" class="clearfix">';
echo '<table width="98%" cellspacing="0" cellpadding="0" border="0" class="paymentdetails first">';
echo '<tr><td class="td_title"><h2>'.$this->params["payment_method"].'</h2></td></tr>';
echo '<tr><td class="td_pdetails">';
echo '<div class="pd_row">';
if($type=='points'){
echo '<span class="djcfpay_label">'.JText::_('PLG_DJCFBANKTRANSFER_POINTS_PACKAGE').':</span>';
}else{
echo '<span class="djcfpay_label">'.JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_AD_NAME').':</span>';
}
echo '<span class="djcfpay_value">'.$itemname.'</span>';
echo '</div>';
echo '<div class="pd_row">';
echo '<span class="djcfpay_label">'.JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_PRICE_TO_PAY').':</span>';
echo '<span class="djcfpay_value">'.$amount.' '.$par->get('unit_price','').'</span>';
echo '</div>';
echo '<div class="pd_row">';
if($type=='points'){
echo '<span class="djcfpay_label">'.JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_POINTS_ID').':</span>';
}else{
echo '<span class="djcfpay_label">'.JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_AD_ID').':</span>';
}
echo '<span class="djcfpay_value">'.$id.'</span>';
echo '</div>';
echo '<div class="pd_row">';
echo '<span class="djcfpay_label">'.JText::_('PLG_DJCFBANKTRANSFER_PAYMENT_PAY_INFORMATION').': </span><br /><br />';
echo '<span class="djcfpay_value">'.JHTML::_('content.prepare',nl2br($this->params["pay_info"])).'</span>';
echo '</div>';
echo '</td></tr>';
echo '</table>';
echo '</div>';
}
}
?>
Related
How to show the total bonus in red color at the screenshot below ?
Demo link : http://client.bfm.expert/test_unilevel1.php
Code :
<?php
//Use the following function to get the data of downlines
function getChildren($parent) {
//list out members by rank
$query = "SELECT user_id, first_name, sponsor_id, rank FROM tbl_user_master WHERE sponsor_id = $parent";
$result = $conn->query($query);
$children = array();
$i = 0;
$result = $conn->query($query) or die($conn->error);
while($row = $result->fetch_assoc())
{
$children[$i] = array();
$children[$i]['userid'] = $row['user_id'];
$children[$i]['name'] = $row['first_name'];
$children[$i]['rank'] = $row['rank'];
$children[$i]['children'] = getChildren($row['user_id']);
$i++;
}
return $children;
}
//modify rank = 4 to 2 to calculate unilevel bonus for rank IAM
$query2 = "SELECT user_id, first_name FROM tbl_user_master WHERE rank = 3";
$result2 = $conn2->query($query2);
$result2 = $conn2->query($query2) or die($conn2->error);
while($row2 = $result2->fetch_assoc())
{
$member_id = $row2['user_id'];
$member_name = $row2['first_name'];
$finalResult = getChildren($member_id); //enter sponsor_id here
echo "Unilevel Bonus for user id : " . $member_id . " " . $member_name . " - Total Unilevel Bonus for lvl1 : Usd??? , lvl2 : Usd???, lvl3 : Usd??? <br /> ";
printList($finalResult);
}
//display all downlines of the sponsor
function printList($array = null, $level = 1) {
$m_profit = 168000;
$Total_investment = 1500000;
$unibonus_percent = 0.00;
if (count($array)) {
echo "<ul>";
foreach ($array as $item) {
echo "<li>";
echo $item['name'];
echo " - Rank : " . $item['rank'];
echo " - level : " . $level;
//show unilevel bonus
$unibonus_percent = 0.06;
//show how many package_lot
$userid = $item['userid'];
$query3 = "SELECT user_id, package_id, package_amount FROM tbl_product_order WHERE user_id = $userid && status=3";
$result3 = $conn3->query($query3);
$result3 = $conn3->query($query3) or die($conn3->error);
while($row3 = $result3->fetch_assoc()) {
$package_siz = $row3['package_amount'] / 1500;
echo " - LOT : " . $package_siz;
$unibonus = (($m_profit / ($Total_investment / 1500)) * $unibonus_percent) * $package_siz;
echo " - unibonus : USD" . $unibonus;
}
if (count($item['children'])) {
printList($item['children'], $level+1);
}
echo "</li>";
}
echo "</ul>";
}
}
?>
Use call by refrence for total bonus calculation
here is an example
$Bonus = 0;
printList($array = null, 1, $Bonus);
echo $Bonus;
function printList($array = null, $level = 1, &$Bonus = 0) {<---call by referance for bonus
if ($level == 10) {
return; //only for break recursive
}
$Bonus = $Bonus + $level;
printList($array = null, $level + 1, $Bonus);
}
How do i add PHP pagination to results coming from search query. I have tried here andhere but didn't seem to get how these works or should work with my code. I have written the search query before i thought of pagination cause i was using the load on scroll before. How do i make the results show paginated.
Below is my code to display searchresults.php.
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
}
}
else
{
echo "No results found";
}
}
I was using this along side javascript to load more result on scroll
if (isset($_POST['limit']) && isset($_POST['start'])) {
$start = $_POST["start"];
$limit = $_POST["limit"];
$query =<<<EOF
SELECT * FROM questions ORDER BY quiz_id DESC LIMIT '$start', '$limit';
EOF;
// echo $query;
$result = $db->query($query);
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo '<div class="quesbox_2">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$row["answer"].'</div>
<div class="quesdatetime"><img src="images/questime.png" alt="export question">'.$row["date"].'</div>
</div>';
}
}
How do i add pagination to my searchresults.php?
Thanks.
try this code without using jquery
<?php
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
$perpageview=10;
if($_GET["pageno"]){
$page=$_GET["pageno"];
}else{
$page=1;
}
$frompage = $page*$perpageview-$perpageview;
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order.' LIMIT '.$frompage.','.$perpageview;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
for ($i=1; $i <= $pagecount; $i++) {
echo ''.$i.'';
}
}
}
else
{
echo "No results found";
}
}
?>
I have a php search script that search and echos out result from database. The issue is that i am trying to paginate the search results but when i click on a different page, it shows the same result. That is the search results are not shared across the pages. Please how do i fix?
Below is my code
if (isset($_GET["mainSearch"]))
{
$condition = '';
$mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
$perpageview = 10;
$page = $_GET["pageno"];
$frompage = $page*$perpageview+1-$perpageview;
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
for ($i=1; $i <= $pagecount; $i++)
{
echo ''.$i.'';
}
}
}
else
{
echo "No results found";
}
}
Thanks very much.
I have mentioned in your previous request check
if (isset($_GET["mainSearch"]))
{
$condition = '';
// $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
$keyword = $_GET['mainSearch'];
$query = explode(" ", $keyword);
$perpageview=10;
if($_GET["pageno"]){
$page=$_GET["pageno"];
}else{
$page=1;
}
$frompage = $page*$perpageview-$perpageview;
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = " ORDER BY quiz_id DESC ";
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order.' LIMIT '.$frompage.','.$perpageview;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
$pagecount = ceil($resultCount/$perpageview);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$wording.'</div>
</div>';
}
for ($i=1; $i <= $pagecount; $i++) {
echo ''.$i.'';
}
}
}
else
{
echo "No results found";
}
}
Currently I am using following code to get data sorted by starting letter of name, if you run this code you will get what i am trying to create
<?php
$dirs = array('Aname1','Aname2','Aname3','A Nmae','Bname ','Cname','Cardiff','Dname','Dname',);
$cur_let = null;
foreach ($dirs as $dir) {
if ($cur_let !== strtoupper(substr($dir,0,1))){
$cur_let = strtoupper(substr($dir,0,1));
echo "<li class=\"title\">".$cur_let."</li>";
}
echo "<li class=\"clear\">
<div class=\"name\">".$dir."</div>
<div class=\"mobile\"></div>
<div class=\"telephone\"></div>
<div class=\"email\"></div>
<div class=\"action\">edit | delete</div>
<div class=\"clear\"></div>
</li>";
}
but how to use above loop inside following to get vales from database and it should be display like (I want highlight first letter) http://i.stack.imgur.com/bLHVD.jpg
$query = "SELECT * FROM phone_number";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$names = $row['name'].",";
}
?>
With MySQLi:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = $mysqli->query($query);
while($row = $sql->fetch_assoc()) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
With mysql_* deprecated functions:
$last_letter = null;
$query = "SELECT name FROM phone_number ORDER BY name";
$sql = mysql_query($query);
while($row = mysql_fetch_array($sql)) {
$first_letter = substr(ucfirst($row['name']), 0, 1);
if($last_letter != $first_letter) {
$last_letter = $first_letter;
echo '<div class="letter">', $first_letter, '</div>';
}
echo ucwords($row['name']), '<br />';
}
Try something like this:
$query = "SELECT * FROM phone_number ORDER BY name DESC";
$result = mysql_query($query) or die(mysql_error());
$lastLetter = '';
$html = '<ul>';
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
if (strtoupper($name[0]) !== $lastLetter) {
if ($lastLetter !== '')
$html .= '</ul></li>';
$lastLetter = strtoupper($name[0]);
$html .= '<li class="title">' . $lastLetter;
$html .= '<ul>';
}
$html .= '<li>' . $name . '</li>';
}
$html .= '</ul></li></ul>';
I decided to give PHP a try, and then bought lynda.com's essential training tutorial.
The problem is, that I get this error:
( ! ) Notice: Uninitialized string offset: 0 in
C:\wamp\www\widget_corp\includes\functions.php on line 147.
when trying to compare two values.
Can anyone help me ? :)
error in navigation function:
if($page["id"] == $selectedPage['id']){
Class content.php below:
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php");?>
<?php findSelectedPage(); ?>
<?php include("includes/header.php");?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($selSubject, $selectedPage);?>
<br/>
+ Add a new subject
</td>
<td id="page">
<?php echo checkSubjOrPage();?>
<br/>
<div id="footer">Copyright 2007, Widget Corp</div>
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>
class function.php below:
<?php
//This file is the place to store all basic functions.
//NB!
//function to prevent problems with submitting values, that contains
//chars such as: "", '' etc., into the database.
function mysql_prep($value){
$magic_quotes_active = get_magic_quotes_gpc;
//i.e. php>= v4.3.0
$new_enough_php = function_exists("mysql_real_escape_string");
if($new_enough_php){
//undo any magic quotes effects so mysql_real_escape_string can do the work
if($magic_quotes_active){
$value = stripslashes($value);
}
$value = mysql_real_escape_string($value);
} else { // before PHP v4.3.0
//if magic quotes aren't already on then add slashed manually
if(!$magic_quotes_active){
$value = addslashes($value);
}
}
return $value;
}
function confirm_query($result_set){
if(!$result_set){
die("Database connection failed: " . mysql_error());
}
}
function getAllSubjects(){
global $connection;
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}
function getPagesForSubject($subject_id){
global $connection;
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}
function get_subject_by_id($subject_id){
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
//REMEMBER:
//if no rows are returned, fetch_array will return false.
if($subject = mysql_fetch_array($result_set)){
return $subject;
} else {
return NULL;
}
}
function get_page_by_id($page_id){
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE id=" . $page_id . " ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
//REMEMBER:
//if no rows are returned, fetch_array will return false.
if($page = mysql_fetch_array($result_set)){
return $page;
} else {
return NULL;
}
}
function checkSubjOrPage(){
global $selSubject;
global $selectedPage;
if(!is_null($selSubject)){
return "<h2>" . $selSubject['menu_name'] . "</h2>";
} else if(!is_null($selectedPage)){
return "<h2>" . $selectedPage['menu_name'] . "</h2>" . "<div>" . $selectedPage['content'] . "</div>";
} else {
return "<h2>" . "Select a subject or page to edit!" . "</h2>";
}
}
function findSelectedPage(){
global $selSubject;
global $selectedPage;
if(isset($_GET['subj'])){
$selSubject = get_subject_by_id($_GET['subj']);
$selectedPage = "";
} else if(isset($_GET['page'])){
$selSubject = NULL;
$selectedPage = get_page_by_id($_GET['page']);
} else {
$selectedPage = NULL;
$selSubject = NULL;
}
}
function navigation($selSubject, $selectedPage){
$output = "<ul class=\"subjects\">";
//3. Perform our database query
$subject_set = getAllSubjects();
while($subject = mysql_fetch_array($subject_set)){
$output .= "<li";
if($subject["id"] == $selSubject['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = getPagesForSubject($subject["id"]);
$output .= "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)){
$output .= "<li";
if($page["id"] == $selectedPage['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?page=" . urlencode($page{"id"}) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
function getPositions(){
$subject_set = getAllSubjects();
$subject_counts = mysql_num_rows($subject_set);
$output = "<select name=\"position\">";
//$subject_counts + 1 b/c we are adding a subject.
for($count = 1; $count <= $subject_counts +1; $count++){
$output .= "<option value=\"{$count}\">{$count}</option>";
}
return $output . " </select>";
}
?>
here you override $selectedPage to a string or set it to null
function findSelectedPage(){
global $selSubject;
global $selectedPage;
if(isset($_GET['subj'])){
$selSubject = get_subject_by_id($_GET['subj']);
$selectedPage = "";
} else if(isset($_GET['page'])){
$selSubject = NULL;
$selectedPage = get_page_by_id($_GET['page']);
} else {
$selectedPage = NULL;
$selSubject = NULL;
}
}
And here it should be an array:
if($page["id"] == $selectedPage['id']){
function findSelectedPage(){
global $selSubject;
global $selectedPage;
if(isset($_GET['subj'])){
$selSubject = get_subject_by_id($_GET['subj']);
$selectedPage = NULL;
} else if(isset($_GET['page'])){
$selSubject = NULL;
$selectedPage = get_page_by_id($_GET['page']);
} else {
$selectedPage = NULL;
$selSubject = NULL;
}
}
function navigation($selSubject, $selectedPage){
$output = "<ul class=\"subjects\">";
//3. Perform our database query
$subject_set = getAllSubjects();
while($subject = mysql_fetch_array($subject_set)){
$output .= "<li";
if(isset($selSubject['id']) && $subject["id"] == $selSubject['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = getPagesForSubject($subject["id"]);
$output .= "<ul class=\"pages\">";
while($page = mysql_fetch_array($page_set)){
$output .= "<li";
if(isset($selectedPage['id']) && $page["id"] == $selectedPage['id']){
$output .= " class=\"selected\"";
}
$output .= "><a href=\"content.php?page=" . urlencode($page{"id"}) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}