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}
Related
Please apologyze my English.
I'm developing a kind of sitemap.xml system for my website to include products to my database from external websites, like Google did with his "Spiders", I just developed this whole script to verify if an specific sitemap.xml contains every required tags, and It works: If any tag is missing, the script returns an error, but if all "products" has the required tags, the script returns a success message.
But I only have one problem: if the xml external file has any error like a missing "close" tags, the script stop working and returns an error.
I need to verify the if the external xml file contais any error, and then proceed with the rest of the code that I wrote, if the result is "NO".
this is my developed script:
<?
$sitemap = htmlspecialchars(addslashes(stripslashes(strip_tags(trim($_POST['sitemap'])))));
$ref = htmlspecialchars(addslashes(stripslashes(strip_tags(trim($_POST['ver_id'])))));
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
include dirname(dirname(__FILE__))."/connectdb.php";
$query = "SELECT * FROM website WHERE `ver_id` = '$ref'";
$result = $mysqli->query($query);
/* array asociativo */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$url = $row['url'];
$long_url = $url.'/'.$sitemap;
?>
<?php
$total = 0;
$xml=simplexml_load_file("$long_url") or die("Error: Cannot create object");
$count = $xml->count();
foreach($xml->children() as $producto) {
$id = $producto->id;
$titulo = $producto->titulo;
$descripcion = $producto->descripcion;
$fotografia = $producto->fotografia;
$precio = $producto->precio;
$costo = $producto->costo;
$horario = $producto->horario;
$tiempo = $producto->tiempo;
$cobertura = $producto->cobertura;
$keywords = $producto->keywords;
$destacar = $producto->destacar;
$delivery = $producto->delivery;
if (!empty($id) && !empty($titulo) && !empty($descripcion) && !empty($fotografia) && !empty($precio) && !empty($costo) && !empty($horario) && !empty($tiempo) && !empty($cobertura) && !empty($keywords) && !empty($destacar) && !empty($delivery)) {
$total = $total + 1; } else { $total = $total - 1; }
}
if($total < $count){ header("Location:/user/sitio.php?id=$ref&mensaje=1"); } else
if($total == $count){
$query = "UPDATE website SET sitemap='$sitemap',sitemap_status='active' WHERE ver_id='$ref'";
$result = $mysqli->query($query);
header("Location:/user/sitio.php?id=$ref&mensaje=2");
}
?>
Solved with this simple script!
<?
$dom = new DOMDocument;
$dom->Load($long_url);
if ($dom->validate()) {
echo "Valid File";
} else { echo "Invalid File"; }
?>
thanks to everyone.
I'm trying to create multiple .php files using php itself.
I want to put some code into a file; most of code is the same but only one or two variables that I wanted to be dynamic. I mean every file that I make are exactly like each other the only difference between theme is one variable.
My function is this:
function generate_corn_files()
{
$C = $GLOBALS['C'];
$db = $GLOBALS['db'];
//delete all contents of folder
RemoveDir($C->INCPATH.'cron/feed/', false);
$res = $db->query('SELECT id FROM category ');
while($cat = $db->fetch_object($res)) {
$id = $cat->id;
$open_output = <<<'PHP'
<?php
$outter_id = $id;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
$fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
fwrite($fp, $open_output);
fclose($fp);
}
}
I tried to put content of file using heredoc but I want to $id in $outter_id = $id; be equal to $id = $cat->id;
it's a variable outside of heredoc I can't make it work inside of it !
Are there any other solutions to make it work ?
You aren't using HEREDOC syntax but rather NOWDOC syntax. If you use HEREDOC, all variables inside will be evaluated, so you will have to escape with \$ the variables you don't want evaluated.
$open_output = <<<PHP
<?php
\$outter_id = $id;
if(\$example = true){
echo 'test';
echo \$C->INCPATH;
}
?>
PHP;
Or, you can stick with NOWDOC, use a placeholder, and replace it afterwards.
$open_output = <<<'PHP'
<?php
$outter_id = %%%id%%%;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
str_replace("%%%id%%%", $id, $open_output);
Maybe this could inspire you
function generate_corn_files()
{
$C = $GLOBALS['C'];
$db = $GLOBALS['db'];
//delete all contents of folder
RemoveDir($C->INCPATH.'cron/feed/', false);
$res = $db->query('SELECT id FROM category ');
while($cat = $db->fetch_object($res)) {
$id = $cat->id;
$open_output = <<<'PHP'
<?php
$outter_id = $id;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
$php_var_name_pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
$open_output = preg_replace_callback(
$php_var_name_pattern,
function($matches) {
if(isset($GLOBALS[$matches[1]])) {
if(is_string($GLOBALS[$matches[1]])) {
return '\''.$GLOBALS[$matches[1]].'\'';
} else {
return $GLOBALS[$matches[1]];
}
} else {
return $matches[0];
}
},
$open_output);
$fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
fwrite($fp, $open_output);
fclose($fp);
}
}
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.
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);
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;
}
?>