how to display data from database using yii framework - php

I'm new to yii..I created one form that contains name,password and role.
I want to display all three data in view page.I'm using encode method but it display only one field.can anyone helpme..
<?php echo CHtml::encode($data->fieldname); ?>
it display one field name but I want to display all three..
Help me...

You need to write above code for each field separately:
<?php echo CHtml::encode($data->fieldname); ?>
<?php echo CHtml::encode($data->fieldpassword); ?>
<?php echo CHtml::encode($data->fieldrole); ?>
Note that, I suppose your fields are fieldpassword and fieldrole.
Update:
This is not possible to write only one command. All these 3 commands is required. But if you mean number of lines, you can do this:
<?php echo CHtml::encode($data->fieldname)."-". CHtml::encode($data->fieldpassword)."-".CHtml::encode($data->fieldrole);

Related

How to show a link if user_id =1 in HTML with PHP

I am new and learning PHP and HTML. I have one user table where I am displaying data. I want to show Delete button if user_id = 1. else I want to hide it. My current link code is like below
delete
I have done PHP code for doing achieve my above description
<?php
if ($row['user_id']==1){
echo //// I want a link here
}
?>
But since my link have also used some PHP codes, I am confused and not able to properly echo button. I am trying from an hour and not able to fix it. Let me know if someone can help me for do it.
Thanks!
I think you want to add variables in string:
<?php
if ($row['user_id']==1){
echo 'delete';
}
?>
Use . to combine strings & variables:
'string'.$var
Read about string concatenation in PHP.
Here is a possible solution for your problem:
<?php
if ($row['user_id'] === 1) {
echo ''.$row['username'].'';
}
here an an example...
<?php if ($row['user_id']==1){ ?>
delete
<?php } ?>
you can put html code in echo like this
echo "<tag name></tag name>"

How do I echo PHP code from database and make the syntax highlighted?

Is it good for users to be allowed to insert PHP code as an input and then store to database? If yes how can we echo out the inputs and display them in a page safely without losing syntax highlighting?
I tried to echo it out this way but it tends to execute on the webpage
echo''.$row['input'].'';
Any answer will be appreciated. Thanks
You need to store your code samples in the database using htmlentities etc. So
<?php
//comment
?>
It must look like this in your MySQL table:
<?php // comment ?>
Create a PHP page that can SELECT the entries from the database table. It must include:
<head>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
</head>
Echo your code sample from your query something like:
echo '<pre class="prettyprint">'.$row['code_sample'].'</pre>';

Adding echo row inside of include statement

Can I pull data from mySQL into include statement so it will open whatever URL I place into field "theme"
<?php include("<? echo $rows['theme']; ?>"); ?>
I want to add this to index.php and I have a editable form where I can add any URL to field theme. Can someone help me get this to work?
<?php
// here you are already inside the php tags so you don't need any others
include($rows['theme']); // you don't need to echo anything just pass the value
?>
However i would recommend you use a switch statement to check the value on $rows['theme']
Simple:
<?php include($rows['theme']); ?>
You can:
<?php
$theme = $rows['theme'].".php";
include($theme);
?>
But you should validate input if it's coming from user.

Drupal 7 entityforms module: How to get value of a field with php?

I am using the entityform module to capture some user submitted data http://drupal.org/project/entityform
I need to pull in some of the entity field values into one of my templates. I was trying to do this with some code that works for regular node fields..
<?php echo $node->field_title[$node->language]['value']; ?>
I've tried..
<?php echo $entity->field_title[$node->language]['value']; ?>
But that doesn't work. Anyone have any ideas on how I can accomplish this?
<?php
$field_data = field_get_items('entityform',$entityform,'field_title');
echo render(field_view_value('entityform',$entityform,'field_title',$field_data));
?>
field_get_items documentation , field_view_value documentation
If you know the entityform_id of a submission, you can load it like this:
entity_load('entityform', $entityform_id);

Getting Last Inserted Id From MySQL in Yii

I want to show the last login id from the database in view > _form.php file.I have made the code in _form.php file like this
<div class="row">
<?php echo $form->labelEx($model,'id'); ?>
<?php echo Yii::app()->db->getLastInsertId('Form');?>
<?php echo $form->error($model,'id'); ?>
</div>
Here Form is the table and the model name.But Still I am getting ID:0.Where is the wrong part?
All the last_insert_id functions (be they PHP wrappers or the native mySQL one) typically refer to the last ID created using the current database connection. The last login was probably not created during the same request you are showing the table in, so this method won't work for you.
Use a normal SELECT to find out the newest login instead - e.g. by using ORDER by creationtime DESC LIMIT 1.
Related: How to get a highest field value in a table in MySQL?
Pekka's answer is good for common. But if you want to do that action in Yii Framework, try this:
$myModel = new $model;
$model -> savel(false);
echo $model->primaryKey; // Prints the last id.
Or you may try this too for general solution:
Yii::app()->db->getLastInsertID();
Finally, I suggest you to check out this
$max = Yii::app()->db->createCommand()->select('max(id) as max')->from('TABLENAME')->queryScalar();
Just add one to get the next.

Categories