Getting value in smarty template - smarty

I have php file called testfun.php. Which is getting values from the databse
<?php
$conn=mysql_connect("localhost","root","") or die("unabke to connect");
$db=mysql_select_db("smartyform",$conn) or die("databse error");
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$sel=mysql_query("select * from form");
while($row=mysql_fetch_array($sel))
{
$id=$row[0];
$name=$row[1];
}
$smarty->assign('id',$id);
$smarty->assign('name',$name);
$smarty->display('testfunction.tpl');
?>
I have tpl file called testfunction.tpl. I am getting output in this file
<body>
<ul>
{$id}
:
{$name}
</ul>
</body>
</html>
When I run the testfun.php I got this output:
16 : dg
But I want output like:
1:d
d:g
What should I do ?

Store your data in an array. So you have to do a for loop in your tpl.
It would be like this in your tpl:
<ul>
{foreach from=$your_data item=item_value key=key}
<li> { $key } : {$item_value}</li>
{/foreach}
</ul>
In your php make it this way:
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$your_row= array();
$sel=mysql_query("select * from form");
while($row=mysql_fetch_array($sel))
{
$your_row[]= $row;
}
$smarty->assign('your_data',$your_row);
$smarty->display('testfunction.tpl');
?>
View this page for a more detailed information:
http://www.smarty.net/docsv2/en/language.function.foreach
Hope that helps you.

Related

Laravel 9 pass model object into view

I am trying to pass a model object into view and use it in JS, but I can't make it work. It looks broken and throws a syntax error Uncaught SyntaxError: Unexpected token '&' if I just use the variable.
I tried using compact() but this does not work aswell compact(): Argument #1 must be string or array of strings, App\Models\Quiz given
Here is how I return the view with the object:
$result = Quiz::where('id', $quiz_id)->first();
if (!$result['active'] || $result['hidden_by_admin']) {
return abort(404);
}
$result['classic_questions'] = ClassicQuestion::where('quiz_id', $quiz_id)->with('classic_answers')->get();
$result['quiz_tags'] = QuizHasTags::where('quizzes_id', $quiz_id)->with('quiz_tag')->get();
return view('play_quiz')->with('quiz', $result);
Here is how I want to access it in blade.php:
<script>
const quiz = {{ $quiz }};
console_log(quiz);
</script>
Assign your data into the window global object which will make it
available everywhere and you can access it from your JS file:
<ul>
#foreach ($quiz as $quiz_data)
<li> {{ $quiz_data }} </li>
#endforeach
</ul>
<script type="text/javascript">
window.data = {!! json_encode($quiz) !!};
</script>
<script src="...."></script>
// trying to access the model $data from the view.
$(function() {
var values = window.data;
alert(values);
}
You can also send data to view like this:
In controller:
return view('play_quiz', $result);
In view
foreach($quiz_tags as quiz){
//code
}

Get img src within html file in Laravel

How can i get the image if I was given a URL with lots of img src? I am using Laravel 5.2.
This takes too long to load given that I have 6,000 links. Thanks in advance.
$url="$products->product_url";
$html = file_get_contents($url);
$doc = new DOMDocument();
#$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
echo $tag->getAttribute('src');
}
Firstable you need to find some kind of class of id of parent element that contain image that you need for example:
<body>
// bla-bla-bla
<div class="content">
<img src="img.png" />
</div>
</body>
You can do follow with ZendDomQuery:
$finder = new Zend_Dom_Query($html);
$node = $finder->query("div[class~='content'] img");

AJAX example in magento

Hello everyone,
I am a newbie to Magento. I want to learn **ajax process in Magento.** Can anyone help me to understand ajax in Magento with one simple example?
Your help will be highly appreciated.
I give you a simple example for you. To work with basic jQuery Ajax in Magento you have work in phtml page and Controller.
Just add the script in phtml page:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".like-result").click(function() {
//alert(this.id);
var id = this.id;
//alert(custid);
jQuery(".notify-status").hide();
jQuery(".notify-loader").show();
jQuery.ajax({
type: "POST",
data: 'pid=' + id,
url:'http://192.168.2.3/subhranil-demo/blog/index/likecount',
success:function(response){
if (response) {
jQuery(".notify-loader").hide();
jQuery(".notify-status").show();
jQuery("#un"+id).html(response);
}
}
});
});
});
</script>
In the above script under jQuery.ajax you can also see type, data, url. type is used for sending process like POST or GET; in data, you will send information to the controller; in URL, you can declare the controller path. Here I have a 'blog' module and I write the public function under 'index' controller and I give the function name 'likecount'. Also here my base path is http://192.168.2.3/subhranil-demo/. So I add the link to URL as following structure: http://192.168.2.3/subhranil-demo/blog/index/likecount.
Now I go to 'IndexController.php' in my controller's folder of blog module and open it. Under the class I add the following function:
public function likecountAction()
{
$blogload = Mage::getModel('blog/blog')->load($_POST['pid']);
$newid = $blogload['like']+1;
$data = array('like'=> $newid);
$blogload->addData($data);
try {
$blogload->setId($_POST['pid'])->save();
echo $newid;
} catch (Exception $e){
echo $e->getMessage();
}
}
Here in the Blog Database, I have the fields like pid (as a primary key) and like. the function works like that when you click on 'like-result' class the like increase +1.
My div structure also like that:
<?php
$allCollection=Mage::getModel("blog/blog")->getCollection();
$allCollection->addFieldToFilter('status',1);
if ($allCollection->count() >= 1)
{
$news = array();
?>
<div class="blog clearfix">
<?php
foreach ($allCollection as $news)
{?>
<p class="like-result" id="<?php echo $news->getId(); ?>"> <?php echo $news->getLike(); ?> </p>
<a style="display: none;" class="notify-loader"><img src="http://www.sendhersomething.com/skin/frontend/megatron/default/images/ajax/notify-loader.gif"></a>
<a style="display: none;" class="notify-status"><img src="http://www.sendhersomething.com/skin/frontend/megatron/default/images/ajax/ststus.png"></a>
<?php } ?>
</div>
<?php } ?>
Try this!

newbie problems with codeigniter

i'm trying to learn codeigniter (following a book) but don't understand why the web page comes out empty.
my controller is
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$data['title'] = "Welcome to Claudia's Kids";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
$data['sidef'] = $this->MProducts->getRandomProducts(3, $skip);
$data['main'] = "home";
$this->load->vars($data);
$this->load->view('template');
}
the view is:
<--doctype declaration etc etc.. -->
</head>
<body>
<div id="wrapper">
<div id="header">
<?php $this->load->view('header');?>
</div>
<div id='nav'>
<?php $this->load->view('navigation');?>
</div>
<div id="main">
<?php $this->load->view($main);?>
</div>
<div id="footer">
<?php $this->load->view('footer');?>
</div>
</div>
</body>
</html>
Now I know the model is passing back the right variables, but the page appears completely blank. I would expect at least to see an error, or the basic html structure, but the page is just empty. Moreover, the controller doesn't work even if I modify it as follows:
function index()
{
echo "hello.";
}
What am I doing wrong?
Everything was working until I made some changes to the model - but even if I delete all those new changes, the page is still blank.. i'm really confused!
thanks,
P.
I've isolated the function that gives me problems.
here it is:
function getMainFeature()
{
$data = array();
$this->db->select("id, name, shortdesc, image");
$this->db->where("featured", "true");
$this->db->where("status", "active");
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get("products");
if ($Q->num_rows() > 0)
{
foreach($Q->result_arry() as $row)
{
$data = array(
"id" => $row['id'],
"name" => $row['name'],
"shortdesc" => $row['shortdesc'],
"image" => $row['image']
);
}
}
$Q->free_result();
return $data;
}
I'm quite convinced there must be a syntax error somewhere - but still don't understand why it doesn't show any error, even if I've set up error_reporting E_ALL in the index function..
First port of call is to run php -l on the command line against your controller and all the models you changed and then reverted.
% php -l somefile.php
It's likely that there is a parse error in one of the files, and you have Display Errors set to Off in your php.ini. You should set Display Errors on for development and off for production, in case you haven't already.
(Edit: in the example above you have missed off the closing } of the class. It might be that.)
Make sure error_reporting in index.php is set to E_ALL and post your code for the model in question.
After looking through your function I suspect it's caused by $this->db->orderby("rand()");
For active record this should be $this->db->order_by('id', 'random');
Note that orderby is deprecated, you can still use it for now but the new function name is order_by
Not sure, but it can be also caused by php's "display_errors" is set to false.
You can change it in your php.ini file.

codeigniter simplepie

I wanted to mention, i have simplepie working in my development environment but as soon as I uploaded the site I cannot get feeds into my homepage. Any ideas? here is the code that works on localhost:
function Homepage()
{
parent::Controller();
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
$this->images = $this->config->item('images');
$this->load->library('simplepie');
$this->simplepie->set_feed_url('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml');
$this->simplepie->set_cache_location(APPPATH.'cache/rss');
$this->simplepie->init();
$this->simplepie->handle_content_type();
}
function index()
{
$data['rssdata'] = array(
"title" => $this->simplepie->get_title(),
"description" => $this->simplepie->get_description(),
"items" => $this->simplepie->get_items(0,5)
);
$this->load->view($data)
}
this is the code that is in the view:
<h3 class="ui-widget-header"><?= $rssdata['title']?></h3>
<div id="accordion" >
<div>
<h5><?= $rssdata['description']?></h5>
<p><?php foreach($rssdata['items'] as $item) :?>
<ul>
<li><?php anchor($item->get_link(),$item->get_title());?></li>
<li class="rssfeed"><?php echo $item->get_description();?></li>
</ul>
<p><small>Posted on <?php echo $item->get_date('j F Y g:i a');?></small></p>
<?php endforeach;?>
</div>
Use the newest "bleeding edge" from their GitHub profile, it sorts out several problems with PHP 5.3 which were making Apache explode from too many errors.
At time of writing, the bleeding edge is marked as v1.2.1-dev.
I've had this problem once, when my hosting setup did not have cURL installed, and error_reporting was off....
try setting var $force_fsockopen = false; to var $force_fsockopen = true; in the SimplePie config file to see if it makes a difference

Resources