show specific image for field content - html

I am creating the following table
CREATE TABLE IF NOT EXISTS `videothek9`.`movies` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`release_year` VARCHAR(45) NOT NULL,
`available` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`))
and baking it with the cakephp bake console.
Here is an example for the standard movies/index page:
The "available" field can be 0 or 1. Now i want to show specific images instead of 0 or 1, like a checkmark tick or a red x-mark. The cakephp cookbook (html helper section) didnt help me much...
And here a piece of the view code
<?php foreach ($movies as $movie): ?>
<tr>
<td><?php echo h($movie['Movie']['id']); ?> </td>
<td><?php echo h($movie['Movie']['title']); ?> </td>
<td><?php echo h($movie['Movie']['release_year']); ?> </td>
<td><?php echo h($movie['Movie']['available']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $movie['Movie']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $movie['Movie']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $movie['Movie']['id']), array(), __('Are you sure you want to delete # %s?', $movie['Movie']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
Can I just place a normal php if/else statement in the Movies Controller ?

It's pretty simple. Instead of echoing the value of "available" field use HtmlHelper::image() to echo img tag with image of your choice.

Super simple.. just do it with the if else statement.and display image or anything you want
If (0)
echo $this->Html->image('tick.png');
Else
echo $this->Html->image('cross.png');

Related

Generate pedigree tree YII2

I am trying to generate pedigree
my DB schema:
CREATE TABLE `Pedigrees` (
`pedigrees_id` int(10) NOT NULL AUTO_INCREMENT,
`horse_name` varchar(32) NOT NULL,
`sire_id` int(10) DEFAULT NULL,
`dam_id` int(10) DEFAULT NULL,
PRIMARY KEY (`pedigrees_id`),
and other info data
Generete GII CRUD for controller model and index.php and viev.php
get info for SIRE and DAM to viev.php
Model Pedigrees:
public function getParentsir()
{
return $this->hasOne(Pedigrees::className(), ['pedigrees_id' => 'sire_id']);
}
public function getParentdam()
{
return $this->hasOne(Pedigrees::className(), ['pedigrees_id' => 'dam_id']);
}
and for viev.php use html + php ()
<?php if ($model->parentdam->parentdam->parentdam->parentdam) {
echo Html::a(Html::encode($model->parentdam->parentdam->parentdam->parentdam->name));
}else{
echo "<tr><td><li>none</li></td></tr>";
}?>
and table example:
<table>
<TR><TD ROWSPAN=8 >
<div>
<h3> <?php
echo Html::a( Html::encode($model->name, $model->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->pedigrees_id]));
echo Html::img('/web/images/photo_p/'.$model->photo_path, [ 'class'=>'img-responsive img-rounded']);;
echo "</h3>";
echo "<ul>
<li>$model->color</li>
<li>$model->land_stand</li>
<li>$model->date_birth</li>
<li>$model->titles</li>
</ul> </div> </div> </TD>";
?>
<?php if ($model->parentsir) {
echo " <TD ROWSPAN=4 >
<div class=header><h3>sir1_1 ";
echo Html::a( Html::encode($model->parentsir->name, $model->parentsir->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->parentsir->pedigrees_id]));
echo Html::img('/web/images/photo_p/'.$model->parentsir->photo_path, [ 'class'=>'img-responsive img-rounded']);
echo "<br>";
echo Html::label( $model->parentsir->color);
echo Html::label( $model->parentsir->date_birth);
echo Html::label( $model->parentsir->titles);
echo "</h3></div>
</TD>
<TD ROWSPAN=2 >
<div class=header><h3>sir1_3";
if ($model->parentsir->parentsir) {
echo Html::a( Html::encode($model->parentsir->parentsir->name, $model->parentsir->parentsir->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->parentsir->parentsir->pedigrees_id]));
echo Html::img('/web/images/'.$model->parentsir->parentsir->photo_path, ['class'=>'img-responsive img-rounded']);
echo Html::label( $model->parentsir->parentsir->color);
echo Html::label( $model->parentsir->parentsir->date_birth);
echo Html::label( $model->parentsir->parentsir->titles);
} else {
echo "no info";
}
ETC...
</table>
i have lov skill for YII2 and php
may be some have better solution for this?
or can help with this example for this solution for YII1 Remake for yii2?

cakephp inside form how to get input value

I'm using cakephp 1.3,I want to create a link with to params ( id and nbr) to call function in controller but the problem is I can't get the input value inside my form..
here my code:
<tr>
<td width="25%"> <?php echo $value['StocksCour']['module_formation']; ?> </td>
<td width="25%"> <?php echo $value['StocksCour']['nb_stock']; ?> </td>
<td width="25%">
<?php
echo $form->create('StocksCour');
echo $bouton->add('-', array('id'=>'subtsract', 'div'=>false));
echo $v->input('nbr_edit',array(
'id'=>'nbr_edit',
'label'=>false,
'default'=> $value['StocksCour']['nb_stock'],
'div'=>false,
'style'=>'width:30px;'
));
echo $bouton->add('+', array('id'=>'add', 'div'=>false));
echo $v->input('id_formation',array(
'type'=>'hidden',
'name'=>'id_formation',
'id'=>'id_formation',
'value'=>$value['StocksCour']['nom_formation']
));
?>
<?php
echo $v->link(
'Valider',
array(
'controller'=> 'Sessions',
'action'=>'saveChange',
$value['StocksCour']['id_formation'] ,
$this->data['StocksCour']['nbr_edit']
),
array(
'image'=>'tick.gif',
'class'=>'button small',
'post'
)
);
?>
</tr>
the problem is she have to get the new 'nb_edit' after change ..

Only single return value($data) will display codeigniter View page

I have a result.php page in view/result.php
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
</body>
html table is currently working. It retrives data from my table testing and display it. The problem is echo part in below code, it doesn't work.
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
From the same table testing. It shows an error in the error page it shows Undefined variable name1,name2,name3
controller/example.php
<?php
class Example extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
function index() {
$this->load->view('example_view');
$this->getvalue();
$this->edit_content();
}
function getvalue()
{
if ($this->input->post('submit')==true) {
$data1['name1']=$this->input->post('name1');
$data1['name2']=$this->input->post('name2');
$data1['name3']=$this->input->post('name3');
$this->load->view('result',$data1);
$this->load->model('Insert_model');
$this->Insert_model->uploaddata($data);
}
}
function edit_content()
{
$data = array();
$this->load->model('Selected_model');
$this->load->helper('url');
$data['result'] = $this->Selected_model->get_contents();
$this->load->view('result',$data);
}
}
?>
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php if(is_array($result)){ foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; } ?>
</table>
<?php echo 'Your new value is '.$row->name1;?>
<?php echo 'Your new value is '.$row->name2;?>
<?php echo 'Your new value is '.$row->name3;?>
If your array is like in the following format.
$info = array
(
[0] => Array
(
[name1] => a
[name2] => b
[name3] => c
)
[1] => Array
(
[name1] => aa
[name2] => bb
[name3] => cc
)
[2] => Array
(
[name1] => aaa
[name2] => bbb
[name3] => ccc
)
[3] => Array
(
[name1] => aaaa
[name2] => bbbb
[name3] => cccc
)
);
Then
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($info as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
The above loop will give you accurate result like.
name1 name2 name3
a b c
aa bb cc
aaa bbb ccc
aaaa bbbb cccc
Now the following code will generate an error because it does not know the following
$row->name1
$row->name1
$row->name1
The reason is that these are defined in the scope of foreach loop thats why its generate error.

Nested queries using wpdb

Im trying to display a few rows of data for each <h3><?php echo $name; ?></h3> item that appears in the loop.
With the current code it only shows one row for each item <h3><?php echo $name; ?></h3>
I have this in my functions.php file
$newdb = new wpdb('login', 'pass', 'db', 'host');
<?php $result=$newdb->get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name');
$names=array();
foreach($result as $row): ?>
<?php $names[$row->name][]=$row;
endforeach; ?>
<?php foreach($names as $name=>$info): ?>
<h3><?php echo $name; ?></h3>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
<?php foreach($info as $n):?>
<tr>
<td><?php echo $n->col1; ?></td>
<td><?php echo $n->col2; ?></td>
<td><?php echo $n->col3; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
So the loop displays the heading followed by a few rows of records, not just one.
<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
col1-value1 col2-value1 col3-value1
col1-value2 col1-value2 col1-value2
etc.
</table>
<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
col1-value1 col2-value1 col3-value1
col1-value2 col1-value2 col1-value2
etc.
</table>
<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
col1-value1 col2-value1 col3-value1
col1-value2 col2-value2 col3-value2
etc.
</table>
.....`
It's possible using your idea but you can do it using one query like
global $wpdb;
$result=$wpdb->get_results('select a.col, b.col1, b.col2, b.col3 from a,b where a.col=b.col5');
Assume that you have tbl1 table and it has a name field and tbl2 table and it has a field for example tbl1_name which has the name value of tbl1 name field and other fields, (with no duplicates) so you can join both tables like
$result=$wpdb->get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name');
$names=array();
foreach($result as $row): ?>
$names[$row->name][]=$row;
<?php endforeach; ?>
foreach($names as $name=>$info):
echo $name;
?><table><?php
foreach($info as $n):
?>
<tr>
<td><?php echo $n->col1; ?></td>
<td><?php echo $n->col2; ?></td>
<td><?php echo $n->col3; ?></td>
</tr>
<?php
<?php endforeach; ?>
?></table><?php
<?php endforeach; ?>
Also remember $wpdb is a global object and available through the template files so you dont need to use new to make an instanse, just use global keyword as I wrote in my answer.
Class Reference/wpdb and sql join and this one.

How to display Serial Numbers with mysql query result?

I have a script that displays information of these fields- batchname, class, batchinstructor from the table "batch". But I want to display dynamically generated serial number on the left side when I show the data. For example:
Serial Number BatchName Class Batch Instructor
1. Solar Class Five John
2. Lunar Class six Bon Jovi
I have tried a lot but its not working. Would you please kindly help me to solve this? Please note that these serial number are not from database.
Here's my Controller:
<?php
class Batchlist extends CI_Controller{
function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page'],$this->uri->segment(3));
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
}
?>
Here's my Model:
function batch_list($perPage,$uri) {
$this->db->select('*');
$this->db->from('batch');
$this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
$this->db->order_by('batchid','DESC');
$getData = $this->db->get('', $perPage, $uri);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
Here's my View
<h1>Batch List </h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $row){ ?>
<tr>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
Maybe I misunderstood, but...what about a simple counter?? (after you ordered your records in SQL).
You need to pass to the view the number of items per page (in this snippet: $per_page), then you retrieve the current page from the URI ($this->uri->segment(n)).
At page 1, counter starts from (20*0)+1, i.e. 1. At page 2, starts from (1*20)+1 ie 21, at page 3 from (2*20)+1 ie 41 and so on...
<?php
$cur_page = $this->uri->segment(n) ? intval($this->uri->segment(n)) : 1;
$i = (($cur_page-1) * $per_page) +1;
foreach ($records as $row) :
?>
<tr>
<td><?php echo $i;?>.</td>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" /><img src="<?php echo base_url();?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php
++$i;
endforeach;
?>
You can place the counter wherever you want, I added a column for simplicity but if you want it somewhere else just place the counter there.
You said the serial numbers are not from the database. Where are they from then? If you want the database to generate them you can create a column named id and set it as the primary key and have it auto increment.