Getting database value in smarty template - smarty

I have php file called testfun.php
<?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
<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 ?

You have to pass your data as an array to smarty. So in PHP you should do something like this:
while($row=mysql_fetch_array($sel))
{
$data[] = array(
"id" => $row[0],
"name" => $row[1]
);
}
$smarty->assign("data", $data);
Then, in your Smarty template you have to use foreach to list your items:
{foreach $data as $item}
{$item.id}:{$item.name}
{/foreach}

Related

Breadcrumbs links not showing correct url

I am creating some bread crumbs for my folders. When I click on the 2nd bread crumb link it does not go to the correct URL
The previous URL shows "test"
http://localhost/project/admin/common/test?directory=test/sub
It should show
http://localhost/project/admin/common/test?directory=test
Inspector Output
<ul class="breadcrumb">
<li>Catalog</li>
<li>test</li>
<li>sub</li>
</ul>
On view
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
Question: How can I make sure the previous URL to shows the before folder correct URL.
I generate my breadcrumbs on my controller
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => 'Catalog',
'href' => base_url('admin/common/test')
);
$directory_names = explode('/', $this->input->get('directory'));
foreach ($directory_names as $directory_name) {
$data['breadcrumbs'][] = array(
'text' => $directory_name,
'href' => base_url('admin/common/test?directory=' . $this->input->get('directory'))
);
}
Thanks to a solution now I found on another forum I will post on here to share
https://forum.codeigniter.com/thread-68653-post-346110.html#pid346110
$directory_names = explode('/', $this->input->get('directory'));
$directory_done = '';
foreach ($directory_names as $directory_name) {
$directory_done .= ($directory_done <> ''? '/':'').$directory_name;
$data['breadcrumbs'][] = array(
'text' => $directory_name,
'href' => base_url('admin/common/test?directory=' . $directory_done)
);
}

Cakephp3 pass (custom) validation to flash message

It is simple to pass a message to flash via:
$this->Flash->error(__('The user could not be saved. Please, try again.'));
But when there are more errors from:
$package->errors();
I use just a simple foreach loop:
foreach ($package->errors() as $error=>$value)
{
foreach ($value as $single_error)
{
$error_array[] = ($single_error);
}
}
Then I pass it to a flash element:
$this->Flash->custom($error_array, [
'key' => 'custom']);
And in the flash message:
if ($message > 0) {
foreach ($message as $m) {
echo h($m).'<br />';
}
} else {
echo h($message);
}
I wonder it here is a better way of handling an array of validation errors.
I am using the following method if there are errors:
Controller:
$errors = $action->errors();
$errorMessages = [];
array_walk_recursive($errors, function($a) use (&$errorMessages) { $errorMessages[] = $a; });
$this->Flash->error(__('Your action cannot be saved!'), ['params' => ['errors' => $errorMessages]]);
Template/Element/Flash/error.tcp:
<?php if (isset($params) AND isset($params['errors'])) : ?>
<ul class="collection with-header">
<li class="collection-header"><h5><?= __('The following errors occurred:') ?></h5></li>
<?php foreach ($params['errors'] as $error) : ?>
<li class="collection-item"><i class="material-icons">error</i><?= h($error) ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Result:
Just for anyone interested, I am using MaterializeCSS.

Clear or reset the wordpress posts pagination while changing filters

I think it is simple, but I don't get it.
This is my filter:
<form class='post-filters'>
<select name="filter">
<?php
$filter_options = array(
'houses' => 'Houses',
'hotels' => 'Hotels',
);
foreach( $filter_options as $value => $label ) {
echo "<option ".selected( $_GET['filter'], $value )." value='$value'>$label</option>";
}
?>
</select>
<input type='submit' value='Filter!'>
</form>
Related PHP to apply the filter to the wordpress query:
<?php
global $destinations;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$destinations = new WP_Query([
'paged' => $paged,
'location' => $location,
'category_name' => urldecode(get_query_var('filter')),
'posts_per_page' => 6
]);
?>
If I do select my "filter" and the result has more than six entries, I use next_posts_link() to see the next six results. The problem is now, if I'm on page 2 or 3 and the other filter has less than e.g. 6 entries, I will see no results while changing my filter.
How do I clear the get variable (/page/2/) while changing my filter?
Example:
category/subcategory/subsubcategory/page/3/?filter=houses
Now I select "filter" hotels
category/subcategory/subsubcategory/page/3/?filter=hotels
and the "/page/3" will not be cleared. So I can not see some posts.
It has been anwsered here:
https://wordpress.stackexchange.com/a/264266
function get_nopaging_url() {
$current_url = $_SERVER[REQUEST_URI];
$pattern = '/page\\/[0-9]+\\//i';
$nopaging_url = preg_replace($pattern, '', $current_url);
return $nopaging_url;
}
You could use this function to remove the pagination in your urls with filters.
See example:
<a href="<?php echo get_nopaging_url(); ?>?filter=houses">

Yii2 Session, Flash messages

I have a problem with setting flash messages.
So, i have an action which in some cases should redirect with flash. It looks like this:
if(!$this->_isSameOrg($reports)){
\Yii::$app->session->setFlash('consol_v_error',\Yii::t('app/consol', 'some_text'));
$this->redirect(\Yii::$app->request->getReferrer());
return;
}
After redirect in view i have this
<div class="col-lg-12">
<?php if(Yii::$app->session->hasFlash('consol_v_error')): ?>
<div class="alert alert-danger" role="alert">
<?= Yii::$app->session->getFlash('consol_v_error') ?>
</div>
<?php endif; ?>
</div>
The problem is i don't see any message here. In Debug panel i see SESSION var populated with good flash, but it doesn't display with this if-statement.
Maybe i need to configure session component or something?...
To set flash,try like
\Yii::$app->getSession()->setFlash('error', 'Your Text Here..');
return $this->redirect('Your Action');
And to display it..
<?= Yii::$app->session->getFlash('error'); ?>
you can try like this
<?php
foreach (Yii::$app->session->getAllFlashes() as $key => $message) {
echo '<div class="alert alert-' . $key . '">' . $message . '</div>';
}
?>
Simply do:
Add two strings to: /views/layout/main.php
in block use:
use frontend\widgets\Alert;
before <?= $content ?>:
<?= Alert::widget() ?>
Now all messages automatically will be on screen. Lets try it! Add in any controller's method:
Yii::$app->session->setFlash('warning', 'bla bla bla bla 1');
Yii::$app->session->setFlash('success', 'bla bla 2');
Yii::$app->session->setFlash('error', 'bla bla 3');
Instead of this:
$this->redirect(\Yii::$app->request->getReferrer());
return;
try this:
return $this->redirect(\Yii::$app->request->getReferrer());
It's working fine for me.
in yii2 flash can be set like this
Yii::$app->session->setFlash('success', 'Thank you ');
Here is my solution:
overwrite standart Session class:
namespace app\components;
use Yii;
class Session extends \yii\web\Session {
public function getAllFlashesNormalized() {
$flashes = [];
foreach (Yii::$app->session->getAllFlashes() as $key => $flash) {
if (is_array($flash))
foreach ($flash AS $message)
$flashes[] = ['key' => $key, 'message' => $message];
else
$flashes[] = ['key' => $key, 'message' => $flash];
}
return $flashes;
}
}
So you can:
Yii::$app->session->addFlash('success', 'Text.');
Yii::$app->session->addFlash('success', 'Another text.');
And output this messages:
<?php foreach (Yii::$app->session->getAllFlashesNormalized() as $flash) { ?>
<div class="alert alert-<?=$flash['key']?>" role="alert"><?=$flash['message']?></div>
<?php } ?>
in my case flash message deleted after redirect, when i use hasFlash before redirect.
if (!Yii::$app->getSession()->hasFlash('success')) {
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'your text'));
}
So i added this and it helped
if (!Yii::$app->getSession()->hasFlash('success')) {
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'your text'));
} else {
Yii::$app->getSession()->set('__flash', array('success' => -1));
}
Did not work for me.
I'd rather use:
In the controler:
$session = new Session;
$session->addFlash("warning","Your text here");
In the view :
<?php
$session = new Session;
foreach ($session->getAllFlashesNormalized() as $flash) {
?>
<div class="alert alert-<?=$flash['key']?>" role="alert">
<?=$flash['message']?>
</div>
<?php
}
?>

How to load database into helper and display in view with specific column

footer_helper
<?php
function get_gadget_footer() {
//the database functions can not be called from within the helper
//so we have to explicitly load the functions we need in to an object
//that I will call ci. then we use that to access the regular stuff.
$ci=& get_instance();
$ci->load->database();
//select the required fields from the database
//$ci->db->select('name, type, setting');
//tell the db class the criteria
$ci->db->where('display', 'Footer');
//supply the table name and get the data
$query = $ci->db->get('gadgets');
foreach($query->result() as $row):
$type['name'] = $row->name;
$type['type'] = $row->type;
var_dump($type);
endforeach;
return $type;
}
hi this is my helper page. var_dump($type) is returning me required result in form of array as:
var_dump($type)
array (size=2)
'name' => string 'Quick Links' (length=11)
'type' => string '<a href='www.facebook.com'>Facebook</a><br>
<a href='#'>Twitter</a><br>
<a href='#'>Salyani</a><br>
<a href='#'>B&W</a><br>' (length=123)
array (size=2)
'name' => string 'Social Network' (length=14)
'type' => string '<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>' (length=115)
Now in my view page. I want display specific record in div. Like record of name
record of type .
Since now i had done following:
view
<?php
$this->load->helper('footer_helper');
$name = get_gadget_footer();
?>
<div id="footer">
<div id="footer_gadget_collection">
<?php
foreach ($name as $dat){
?>
<?php
?>
<div id="footer_subgadget">
<div id='title'><?php echo $dat['name']; ?></div>
<div id='description'><?php $dat['type']; ?> </div>
</div>
<?php
}
?>
But, its giving me error. Please help me.
In your helper you are restting the name/type of the array, not building it.
It should be something like;
<?php
//supply the table name and get the data
$query = $ci->db->get('gadgets');
$type = array();
foreach ($query->result() as $row) {
$type[] = array('name' => $row->name, 'type' => $row->type);
}
return $type;

Resources