Display data from the database means php smarty mysql - php

help me understand: There is a table in the database:
id f_gdpec w_gdpec url_gdpec p_gdspec
1 Москва Красноярск www.test.ru 450
2 Москва Красноярск www.test.ru 900
3 Москва Красноярск www.test.ru 600
Need to pull data from a database and display means php means smarty. that's what I did: module.php:
<?php
function mod_gdspec($module_id){
$inCore = cmsCore::getInstance();
$inDB = cmsDatabase::getInstance();
$cfg = $inCore->loadModuleConfig($module_id);
$sql = "SELECT f_gdpec,
w_gdpec,
url_gdpec,
p_gdspec
FROM cms_gdspec";
$result = $inDB->query($sql) ;
if ($inDB->num_rows($result)){
$items = array();
while ($item=$inDB->fetch_assoc($result)){
$items[]=$item;
}
}
$smarty = $inCore->initSmarty('modules', 'mod_gdspec.tpl');
$smarty->assign('items', $items);
$smarty->display('mod_gdspec.tpl');
return true;
}
?>
The template mod_gdspec.tpl:
{foreach item=item from=$items}
<div class="mod_latest_entry">
<div class="mod_latest_f_gdpec">
{$item.f_gdpec}
</div>
<div class="mod_latest_w_gdpec" >
{$item.w_gdpec}
</div>
</div>
{/foreach}
data from tabditsy not appear, I can not understand how much.
ask your might. Thank you for your attention.
corrected script - module.php!!!!!!!!!!!!!

You are returning the value before assigning the value, remove that unnecessary return value
<?php
function mod_gdspec($module_id){
$inCore = cmsCore::getInstance();
$inDB = cmsDatabase::getInstance();
$cfg = $inCore->loadModuleConfig($module_id);
$sql = "SELECT f_gdpec,
w_gdpec,
url_gdpec,
p_gdspec
FROM cms_gdspec";
$result = $inDB->query($sql) ;
if ($inDB->num_rows($result)){
$items = array();
while ($item=$inDB->fetch_assoc($result)){
$items[]=$item;
}
}
// return true;------------>remove this
$smarty = $inCore->initSmarty('modules', 'mod_gdspec.tpl');
$smarty->assign('items', $items);
$smarty->display('mod_tags.tpl');
return true;
}
?>

Related

How can i create a new section with only 5 elements in PHP?

i have a PHP code that creates a team page with team member boxes on my website. The code for every team meber is fetched from the database, and it works fine, but time has passed and we are many now. So many, that I need to display only 5 team members per section. Like 5 team members in one line, the next 5 in the other line, .. - I tried a few things to do that, but I got stuck. Sometimes members were missing, sometimes he created many lines with the same members, and so on. That's why I'm asking here. I already searched a bit but couldn't find anything that helps me.
Currently, my code creates a new <section> for ALL team members. But that's not what I need. It should create a section for 5 members, the next section for the next 5 members, and so on. How can I do that?
(btw I'm a complete beginner in PHP and want to learn something from it so you can improve my code if you want.)
function team_helfer() {
$db = new mysqli("ip", $username, $password, $database);
$result = mysqli_query($db, "SELECT code FROM team_data WHERE rank = 'HELFER'");
$team_list = "";
$count = 0;
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $field => $value) {
$team_list .= "$value";
++$count;
}
}
if ($count == 2) {
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default" style="left: 17%;">
'.$team_list.'
</div>
</section>';
} elseif ($count == 1) {
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default" style="left: 33%;">
'.$team_list.'
</div>
</section>';
} else {
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default">
'.$team_list.'
</div>
</section>';
}
mysqli_free_result($result);
$db->close();
return $code;
}
add_shortcode( 'team_helfer', 'team_helfer' );
Code from HTMHell's answer:```php
function team_helfer() {
$db = new mysqli("ip", $username, $password, $database);
$result = mysqli_query($db, "SELECT code FROM team_data WHERE rank = 'HELFER'");
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sections = array_chunk($rows, 5);
foreach ($sections as $section) {
$teamList = implode(",", array_map(function($row) {
return $row['code'];
}));
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default">'.$teamList.'</div></section>';
}
mysqli_free_result($result);
$db->close();
return $code;```
Sadly that doesn't help, all elements are vanished now, i can't see them.
I think you are looking for array_chunk.
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sections = array_chunk($rows, 5);
foreach ($sections as $section) {
$teamList = implode(",", array_map(function($row) {
return $row['code'];
}, $section));
echo "<section>$teamList</section>";
}
Here's a live example:
https://3v4l.org/mXRtV

Smarty not displaying results in template file

I created file of code working perfect ... but problem is I added the file into my website header {include file="header_featured.html"} It is not displaying any thing ... But if write the url in browser with its .php file name like www.domain.com/header_featured.php it shows results what is the problem ...? even all of the other files of cms/script also included in header and displaying perfect results ..
header_featured.php
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$ads = array();
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($temp = mysql_fetch_assoc($featured_ads)) {
$record = array();
$record['ad_id'] = $temp['id'];
$record['ad_title'] = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no");
$img = mysql_fetch_assoc($featured_ads_images);
$record['img_id'] = $img['id'];
$record['ad_img'] = $img['picture'];
$record['img_folder'] = $img['folder'];
$ads[] = $record;
}
$smarty->assign('ads', $ads);
$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
$smarty->display('header_featured.html');
close();
?>
header_featured.html
{foreach item=item from=$ads}
{$item.ad_id}
{$item.ad_title}
{$item.img_id}
{$item.ad_img}
{$item.img_folder}
{/foreach}
My way of including the file into header
{include file="header_featured.html"}
It seems you should simple in your PHP code write:
<?php include ('header_featured.php'); ?>
to display your content.
However in fact you should move the smarty part to index.php, include header_featured.php file and in index.php display Smarty template.
For example I would do it it this way:
index.php
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$header_featured = '';
require('header_featured.php');
$db->close();
if($db->error!='') {
$db_error = $db->getError();
$smarty->assign('db_error',$db_error);
}
else {
$header_featured = $smarty->fetch('header_featured.tpl');
}
$smarty->assign('header_featured', $header_featured );
$smarty->display('index.tpl');
?>
header_featured.php
<?php
$ads = array();
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($temp = mysql_fetch_assoc($featured_ads)) {
$record = array();
$record['ad_id'] = $temp['id'];
$record['ad_title'] = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no");
$img = mysql_fetch_assoc($featured_ads_images);
$record['img_id'] = $img['id'];
$record['ad_img'] = $img['picture'];
$record['img_folder'] = $img['folder'];
$ads[] = $record;
}
$smarty->assign('ads', $ads);
index.tpl
<html>
<head>
....
<body>
{$header_featured}
...
</body>
</html>
header_featured.tpl
{foreach item=item from=$ads}
{$item.ad_id}
{$item.ad_title}
{$item.img_id}
{$item.ad_img}
{$item.img_folder}
{/foreach}

Some Assistance On Reply To Comments PHP

Could someone give me some assistance on adding reply to my commenting system.
I'm creating a project on Smarty 3 and so far i have done the comments part but i'm having a bit of trouble with the reply part because it won't display the replies could someone check my code to see what i am doing wrong or correct it for me please here is my code below.
PHP Functions from movie class
public function GetMovieComments($con, $movie_id) {
$c = array();
$q = mysqli_query($con,"SELECT * FROM `movie_comments`,`user` WHERE `movie_id` = '".$movie_id."' AND `uid` = `user_id`");
if (mysqli_num_rows($q) > 0) {
while ($r = mysqli_fetch_assoc($q)) {
$c[] = $r;
}
return $c;
}
}
public function GetMovieReplies($con, $comment_id) {
$comment_id = mysqli_real_escape_string($con, $comment_id);
$rp = array();
$q = mysqli_query($con,"SELECT * FROM `movie_replies`,`user` WHERE `comment_id` = '".$comment_id."' AND `uid` = `user_id`");
if (mysqli_num_rows($q) > 0) {
while ($r = mysqli_fetch_assoc($q)) {
$rp[] = $r;
}
return $rp;
}
}
Now calling the functions from movie.php
$comments = $movie->GetMovieComments($con, $movie_id);
if (isset($comments)) {
foreach($comments as $comment) {
$comment_id = $comment['cid'];
$replies = $movie->GetMovieReplies($con, $comment_id);
$smarty->assign('replies',$replies);
}
}
$smarty->assign('comments', $comments);
Now in movie.tpl
{if isset($comments)}
{if $comments neq ''}
{foreach from=$comments key=key item=comment}
<div class="media">
<div class="media-body">{$comment.comment}</div>
</div>
{foreach from=$replies key=key item=reply}
<h4>{$reply.reply}</h4>
{/foreach}
{/foreach}
{else}
<h5>No Comments</h5>
{/if}
{/if}
Could someone please help me i have been trying to solve this for hours lol thanks
For not overwriting the variable replies, I'd do something like this:
// Make GetMovieComments return an array with values or simply an array(), so no check needed
$comments = $movie->GetMovieComments($con, $movie_id);
foreach($comments as $key => $comment) {
$replies = $movie->GetMovieReplies($comment['cid']);
$comments[$key]['replies'] = $replies;
}
$smarty->assign('comments', $comments);
Then you can access the {foreach from=$comment.replies key=key item=reply} to use it within the inner foreach. However that was just one thing, there might be more mistakes in the code...
A couple of things I'd do extra with your code:
public function GetMovieComments($movie_id) {
$comments = array();
// Some data validation to avoid SQL Injections
// You might be better with binding parameters if it's not an int
$id = intval($movie_id);
// You use the connection already stored in this object. You should set it in the constructor
$query = mysqli_query($this->con,"SELECT * FROM `movie_comments`,`user` WHERE `movie_id` = '" . $id . "' AND `uid` = `user_id`");
// No need for the if. This will only enter if there's any result anyway
while ($result = mysqli_fetch_assoc($query)) {
$comments[] = $result;
}
return $comments;
}
// Apply some similar changes to GetMovieReplies() as the ones above.

Only Returning 1 Item

Can someone help me as i'm new to php?
I'm running a movie site that using smarty template engine and what i'm trying to do is return and display all categories foreach movie but what happens is it displays the same category 'Comedy' for all movies when i use this
The functions
public function getByPerma($perma, $lang=null){
$perma = mysql_real_escape_string($perma);
$movie = array();
$e = mysql_query("SELECT * FROM movies WHERE perma='$perma'") or die(mysql_error());
if (mysql_num_rows($e)>0){
$movie = $this->formatMovieData(mysql_fetch_assoc($e), $lang);
}
return $movie;
}
public function getMovieCategoryDetails($movieid,$lang=null){
$movieid = mysql_real_escape_string($movieid);
$e = mysql_query("SELECT * FROM movie_tags WHERE id IN (SELECT tag_id FROM movie_tags_join WHERE movie_id=$movieid)") or die(mysql_error());
$tags = array();
if (mysql_num_rows($e)){
while($s = mysql_fetch_array($e)){
$s['tag'] = json_decode($s['tag'],true);
if ($lang){
$s['tag'] = $s['tag'][$lang];
}
$tags[$s['id']] = $s;
}
}
return $tags;
}
Called the function
$movies = $movie->getByPerma($perma,$language);
if (empty($movies)) {
$movies = '';
} else {
$tags = $movie->getMovieCategoryDetails($movies['id'],$language);
if (!count($tags)){
$smarty->assign("tags","");
} else {
$smarty->assign("tags",$tags);
}
}
And displaying the categories
{foreach from=$tags key=id item=val name=tags}
{$val.tag}
{/foreach}
I hope someone can help?
I think your getByPerma() only return 1 movie info, when add item to array, [] is missing:
$movie = $this->formatMovieData(mysql_fetch_assoc($e), $lang);
-->
$movie[] = $this->formatMovieData(mysql_fetch_assoc($e), $lang);

Joomla's Who's Online Counter - How to add thousand separator?

One of my sites frequently has more than 1000 concurrent visitors and just for consistency I want to add a thousands separator to the display so it shows like 1,000.
My initial thought was just to add number_format before the variable holding the guest count but this stops the counter working for some reason.
The function in helper.php counting the guests looks like this:
// show online count
static function getOnlineCount() {
$db = JFactory::getDbo();
// calculate number of guests and users
$result = array();
$user_array = 0;
$guest_array = 0;
$query = $db->getQuery(true);
$query->select('guest, usertype, client_id');
$query->from('#__session');
$query->where('client_id = 0');
$db->setQuery($query);
$sessions = (array) $db->loadObjectList();
if (count($sessions)) {
foreach ($sessions as $session) {
// if guest increase guest count by 1
if ($session->guest == 1 && !$session->usertype) {
$guest_array ++;
}
// if member increase member count by 1
if ($session->guest == 0) {
$user_array ++;
}
}
}
$result['user'] = $user_array;
$result['guest'] = $guest_array;
return $result;
}
And in the template the data is displayed using the following:
<?php if ($showmode == 0 || $showmode == 2) : ?>
<?php $guest = JText::plural('MOD_WHOSONLINE_GUESTS', $count['guest']); ?>
<?php $member = JText::plural('MOD_WHOSONLINE_MEMBERS', $count['user']); ?>
<p><?php echo JText::sprintf('MOD_WHOSONLINE_WE_HAVE', $guest, $member); ?></p>
Where do I put the number_format so the separator is added please?
does this not work?
$guest = JText::plural('MOD_WHOSONLINE_GUESTS',number_format($count['guest'],0,'.',','));

Categories