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
Related
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;
?>
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
I created this code for a dropdown navigation menu. I have 2 tables in my database, one for parentitems and the other one for childitems. The parents getting out correctly, but the childitems won't.
The problem is, I only get one parent and one child at a time, or I get totally nothing.
Thanks in advance!
My code:
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('navigation',$con);
$query="select * from nav";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$m_id=$row['m_id'];
$m_title=$row['m_title'];
$child_query="select * from nav_child where parent_id='$m_id'";
$run_child=mysql_query($child_query);
while($row_child=mysql_fetch_array($run_child)) {
$child_id=$row_child['nav_id'];
$child_title=$row_child['child_title'];
echo"<ul>
<li><a href='menu.php'>$m_title</a>
<ul>
<li><a href='menu.php'>$child_title</a></li>
</ul>
</li>
</ul>";
}
}
?>
You need to split your html
while(mainquery) {
echo '<ul>' <-----note the location
while (subquery) {
echo '<li>subquery 1 stuff</li>'
}
echo '</ul>' <-----note the location
}
You're outputting it entirely in your subquery section, so EVERY child row gets its own complete <ul><li>...</li></ul> tag set.
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);
}; ?>
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
I have a search bar and a sql database with 3 movies in
Name:Star Wars: Description: Contains the word group
Name:301: Description: Contains the word group
Name:destroyer: Description: Contains the word group
BUT when ever I search for the word "group"
I only get one result usually:
if i search for group star wars will appear 3 time.
also i know its vulnerable to sql injection but at the moment that is not an issue
here is my php code:
if(!isset($_POST['search']))
{
header("Location:index.php");
}
$search_sql="SELECT * FROM php_item WHERE Name LIKE '%".$_POST['search']."%' OR Description LIKE '%".$_POST['search']."%'";
$search_query = mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)
{
$search_rs= mysql_fetch_assoc($search_query);
}
?>
</p>
<p> Search Results</p>
<?php
if(mysql_num_rows($search_query) != 0){
do{ ?>
<p>
<?php echo $search_rs['Name']; ?>
<?php }while($searchr_rs=mysql_fetch_assoc($search_query));
} else {
echo "No Results Found";
}
?>
If this is your code, there is a typo in the while. It should be $search_rs=mysql_fetch_assoc($search_query). You have an extra 'r'.
Good Luck!
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
I got an echo of an class value but would like to assign this value to an Variable:
<p class="<?echo $test="listprice"?>"></p>
I thought about something like this:
<?$testing = echo $test="listprice"?>
But this doesnt seem to work. Is it possible to get the class echo?
When you $test = "listprice"; you are already assigning it to the variable $test. To assign it to the variable $testing if you really want to then you would do this:
$testing = $test;
// display the new variable
echo $testing;
try to separate assignment and output:
<?
$test="listprice";
?>
<p class="<? echo $test; ?>"></p>
Use short open tag of php <? ?>
<p class="<?=$test="listprice"?>">Something</p>
More about
it should be
<?php
echo $test = "listprice";
echo $name = "ankit";
?>