Yii How to foreach data from DB [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How yii foreach data from DB ? One developer have multiple game, but when i call the data out from DB it only show one data
Following is my coding :
In Controller
$id = Yii::app()->user->getState('id');
$thedb = GamesDevelopersApp::model()->find('developer_id='.$id);
$gametitle = CHtml::encode($thedb->gametitle);
$version = CHtml::encode($thedb->version);
$create_time = CHtml::encode($thedb->uploaddate);
$status = CHtml::encode($thedb->status);
$this->render('applist',array('gametitle'=>$gametitle,'version'=>$version,'create_time'=>$create_time,'status'=>$status));
In HTML
<td class="apptd2">
<?php foreach($models as $model){
echo CHTML::encode($model->gametitle);
}; ?>
</td>

In your DB request you are just retrieving one entry because you are using the find method:
$thedb = GamesDevelopersApp::model()->find('developer_id='.$id);
And in the Yii doc you'll see fot he find method:
Finds a single active record with the specified condition.
This is why the following loop has no sense
<?php foreach($gametitle as $title){
echo $title;
}; ?>
For me the best would be to use findAll in the controller:
$id = Yii::app()->user->getState('id');
$models = GamesDevelopersApp::model()->findAll('developer_id='.$id);
$this->render('applist',array('models'=>$models));
And the following loop in the view:
<?php foreach($models as $model){
echo CHTML::encode($model->gametitle);
}; ?>

Related

What is the equivalent method of javascript looping data from an object and rendering it via html for php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So in javascript its pretty simple to create an object like so...
var myObject = [
{
name: 'hello',
url: 'https://www.google.com',
img: 'ico1.png'
},
{
name: 'hello2',
url: 'https://www.something.com',
img: 'ico2.png'
},
and then i'd just run a simple loop and it would render out my data like this
let renderedHtml=myOpject.map(function(item){
return `<h1>${item.name}</h1><img src="${item.imgFile}"/>`
});
<h1>hello1</h1>
<img src="ico1.jpg">
<h1>hello2</h1>
<img src="ico2.jpg">
Whats the easiest way to do this in php?
Any loop can get it done but the easiest one is foreach():
More on Foreach()
<?php
foreach($myObject as $key => $value){
?>
<h1><?= $value->name ?></h1><img src="<?= $value->imgFile ?>"/>
<?php
}
?>

Getting variable value from another page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I was trying to get variable value from a page and echo it out in another page
for example I have 2 pages pg1.php and pg2.php:
On pg2.php I have:
<?php
$vr = "Hello";
?>
Now I want to echo this out on pg1.php, I have tried this:
<?php
require "pg2.php";
echo $vr;
?>
It works, but the problem is whatever else I have on pg2.php will be displayed on pg1.php.
It would be best to restructure your code so that variables are defined in one file that then includes another file with output etc.
<?php
// vars.php
$vr = "Hello";
?>
<?php
// pg1.php
require "vars.php";
echo $vr;
?>
<?php
// pg2.php
require "vars.php";
// other stuff
// ...
// ...
?>
But for this issue in general, buffer output and then delete the buffer:
<?php
// pg1.php
ob_start();
require "pg2.php";
ob_end_clean();
echo $vr;
?>

Call variable without echo or print [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to call variable with in variable and below attempts are working fine
Attempt 01:
<?php
$var1 = "Hello";
echo $var1;
?>
Attempt 02:
<?php
$view = '$var1 = "Hello";
echo $var1;';
echo $view;
?>
But I'm trying to call variable without using "echo" or "print" command, as mentioned below
<?php
$view = '$var1 = "Hello";
echo $var1;';
$view;
?>
Is there any other way to achieve this? Please advise.
<?php
$view = '$var1 = "Hello";
echo $var1;';
eval($view);
?>

Looking for proper way to inject HTML table into PHP code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let me start by saying that i'm sorry if this is a complete noob question.
while using phpStorm, I have an array of $teams and by invoking foreach loop I tried to put each of the array section into an HTML table
here is what I did so far
I am getting cannot use [] for reading, I can't find an answer how to do that properly, thx in advance.
<table>
<tr>
<th>Team</th>
<th>ID</th>
</tr>
<?php
include dirname(__FILE__) . "/stats.php";
global $teams;
foreach ($teams['data'] as $team) {
echo "<tr>" ;
echo "<td> $team['name']</td>" ;
echo "<td> $team ['id']</td> " ;
echo "</tr>";
}
?>
</table>
To get the string substitution working correct, you have to suround it with {} if it's something more complex than a simple var - especially array access.
echo "<td> {$team['name']}</td>";

Get query data without foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Currently I make my query like this:
<?php
function Data() {
$db = DB::getInstance();
$query = $db->query("SELECT * FROM users");
$results = $query->results();
return ($results);
}
$listdata = Data();
?>
And when I want to see my information I have something like this:
<div class="container">
<h1>Title</h1>
<hr>
<div class="row">
....
...
..
.
<?php foreach ($listdata as $v1) { ?>
<p><?=$v1->username?></p>
<?php } ?>
</div>
</div>
But if I want to put information see for example in the footer of the page I have to open a new foreach
As I can transform this code to open the query to the top of the page and simply llabar variables where I want without opening another foreach?
Even better would defeat the function, I just want to put the query and data wherever.
Thank you!
You could try echo $listdata[0]->column_name thou this will print only the first row......so to be precise use a where clause in your sql statment
Edit: It will print only the first index in the array that contains the Fetched Data

Categories