I have a problem,im trying to convert php standard to CodeIgniter, But I dont know how to convert ths code, please help, and thanks a lot.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("ardefa");
$borneo=mysql_query("select* from borneo");
while($row=mysql_fetch_array($borneo))
{
?>
<a href="#"><li><img src="
<?php
$page = isset($_GET['page']) ? ($_GET['page']):"";
if ($page =='borneo')
{
echo $row["img"];
}
?>">
</li></a>
<?php
}
?>
Hope this will help you :
You don’t need to use db_select if you have single database, if multiple database you only need to use a different database on the same connection. You can switch to a different database when you need to using this $this->db->db_select('ardefa');
You can do like this :
//$this->db->db_select('ardefa');
$this->db->select('*');
$this->db->from('borneo');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
/*for multiple array*/
$result = $query->result_array();
/*print here to see the result
print_r($result);
*/
}
Use $result like this :
foreach($result as $row)
{
echo $row;
}
Or can also do it like this :
//$this->db->db_select('ardefa');
$query = $this->db->get('borneo');
if ($query->num_rows() > 0 )
{
/*for multiple array*/
$result = $query->result_array();
/*for single array
$row = $query->row_array();
*/
}
For more : https://www.codeigniter.com/user_guide/database/
Try this hope it will help you
MODEL
public function your_function(){
return $this->db->get('borneo')->reslut_array();
}
CONTROLLER
<?php
$this->load->model('model-name');
$data = $this->model-name->model_function();
foreach($data as $row){
if(isset($_GET['page']) && $_GET['page'] == "borneo"){ ?>
<li><img src="<?php echo $row['img']?>" /></li>
<?php } } ?>
Related
function mostViewedTracks() {
$this->load->database();
$this->db->select("*");
$this->db->from("file");
$this->db->order_by("views", "desc");
$this->db->limit(8);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return array();
}
when i run this model it gives error that invalid argument supplied for foreach()
this is my view code:
<?php if( !empty($most_viewed) ) { ?>
<?php foreach($most_viewed as $row): ?>
<a href="<?php echo base_url('index.php/home/track');?>/<?php echo $row->id;?>" class="list-group-item">
<img class="track-list-img" src="<?php echo base_url('assets/img/music');?>/<?php echo $row->id;?>.jpg">
<div class="list-track-infoo">
<h4 class="list-group-item-heading"><?php echo $row->title;?><span class="badge"><?php echo $row->views;?><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></span> </h4>
<p class="list-group-item-text"><?php echo $row->singer;?></p>
</div>
</a>
<?php endforeach; ?>
<?php} ?>
Updated
Controller
function track() {
$id = $this->uri->segment(3);
$data1['track'] = $this->view_models->track($id);
$data1['most_viewed'] = $this->view_models->mostViewedTracks();
$this->load->view('includes/header');
$this->load->view('track', $data1);
$this->load->view('includes/footer');
}
I have tried my best to solve this problem but all in vain
You need to change two mistakes,
You should return the `$result` array in your model like,
$results = $query->result();
return $results;
And in foreach change $row1 to $row which produces the error. see,
<?php foreach($most_viewed as $row): ?>
Instead of return array(); you have to return result set from models file
if($query->num_rows() > 0)
{
return $query->result();// return result set
}else{
return FALSE;//
}
IN controller you have to call your models function as
$data1['most_viewed'] = $this->view_models->getFeaturedTracks();// change mostViewedTracks to getFeaturedTracks
In view use it as
<?php foreach($most_viewed as $row): ?>// remove $row1 to $row
Function should like this....
function getFeaturedTracks() {
$results = array();
$this->load->database();
$this->db->select("*");
$this->db->from("file");
$this->db->order_by("views", "desc");
$this->db->limit(8);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
All I want is to get some place_ids from db, put them in array and echo that array in view. Could you please check the code below and help me to find the mistake. I think the problem is in view part.
Model:
$this->db->select('place_id');
$this->db->from('table');
$this->db->where('ses_id', $ses_id);
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
return $query->result_array();
}
else {return false;}
Controller:
$ses_id = $this->uri->segment(3);
$data["results"] = $this->mymodel->did_get($ses_id);
$this->load->view("my_view", $data);
View:
<?php
$place_id = array();
foreach($results as $row){
$place_id[] = $row['place_id'];
}
print_r($place_id);
?>
checking the $results is empty or not
<?php
$place_id = array();
if(!empty($results)
{
foreach($results as $row)
{
$place_id[] = $row['place_id'];
}
}else
{ echo "no data found";}
print_r($place_id);
?>
So im running a query and trying to return a result set back into my view however when I do <?php foreach ($form_records as $form_record) : ?><?php echo $form_record['fname']; ?> <?php endforeach; ?> I get Undefined variable: form_records. What am i doing wrong when I run the query (Select fname, lname from form_manager where username = "tester") I get a row returned successfully so the query is working.
Model
function retrieve_form_record($username) {
$this->db->select('fname,lname');
$this->db->from('form_manager');
$this->db->where('username', $username);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$form_records= $query->result_array();
return $form_records;
}
else {
return false;
}
}
}
Controller
function load_saved_forms() {
$username = $this->tank_auth->get_username();
$form_records = $this->Form_model->retrieve_form_record($username);
$this->load->view('welcome',$form_records);
}
The problem is how you pass parameter to view, change this line:
$this->load->view('welcome',$form_records);
to :
$data['form_records'] = $form_records; //<<--pass data to view right way.
$this->load->view('welcome',$data);
and the then you can perform this inside view:
<?php foreach ($form_records as $form_record) : ?>
<?php echo $form_record['fname']; ?>
<?php endforeach; ?>
Note that $form_records in view is an index of array $datayou passed at $this->load->view('welcome',$data);
The $form_records is probably an array and looking at the codeigniter's doc it will turn those into array elements when it is passed to the view.
See this link: http://ellislab.com/codeigniter/user-guide/general/views.html
But it is saying if you did this in your controller:
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$this->load->view('blogview', $data);
You can use that "todo_list" like this in your view:
<?php foreach ($todo_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
<?php
print_r($optimum);
$dataNumRows = count($optimum);
?>
<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
<?php echo $cFirstName; ?>
<?php echo $cLastName; ?>
<?php endfor; ?>
My print_r inserted in my VIEW shows the following:
Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) )
My MODEL is the following
//Get all the customers currently pending
//install for the user making the request.
function getAllCustomersPendingInstall()
{
$data=array();
//Need to use sessions to display proper
//records for each user. Temp set id to user #7
$id = 7;
//query the db and return all record where SalesRepId == $id
$query = $this->db->get_where('customers', array('SalesRepId' => $id));
//check logic, if rows exist RETURN all rows, else
//return message that no pending installs is available.
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data['cFirstName'][] = $row->customerFirstName;
$data['cLastName'] [] = $row->customerLastName;
}
} else {
$data = "No pending installs available!";
return $data;
}
//the following var_dump is only showing the last record.
//need to show all rows (which should be 2)
//var_dump($data); exit;
return $data;
}
My CONTROLLER is the following
{
$this->load->library('table');
$this->load->model('GetData');
$data['optimum'] = $this->GetData->getAllCustomersPendingInstall();
$this->load->view('welcome_message', $data);
}
And my question is how do I properly use the FOR loop in my VIEW so that I can loop through all the returned rows. As you can see the print_r is properly returning the proper rows- However I am unable to loop through them. Thanks for the help! Much appreciated!
Try this in your view:
<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
<?php echo $optimum['cFirstName'][$i]; ?>
<?php echo $optimum['cLastName'][$i]; ?>
<?php endfor; ?>
I think what you're trying to do is get an associative array for each row returned from the database. Correct me if I'm wrong about that.
Should fix your problem
$data = array();
$data_index = 0;
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data[$data_index]['cfirst'] = $row->customerFirstName;
$data[$data_index]['clast'] = $row->customerLastName;
$data_index++;
}
} else {
$data = "No pending installs available!";
return $data;
}
then in your view (where $customer is the $data array)
<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>
I want to create a simple menu function which can call it example get_menu()
Here is my current code.
<?php
$select = 'SELECT * FROM pages';
$query = $db->rq($select);
while ($page = $db->fetch($query)) {
$id = $page['id'];
$title = $page['title'];
?>
<?php echo $title; ?>
<?php } ?>
How to do that in?
function get_menu() {
}
Let me know.
Here is the function for that:
function get_menu(&$db)
{
$select = 'SELECT * FROM pages';
$query = $db->rq($select);
$menu = '';
while ($page = $db->fetch($query)) {
$id = $page['id'];
$title = $page['title'];
$menu .= '<a href="page.php?id=' . $id . '" &title="' . $title .'></a>'
}
return $menu;
}
.
Some Quick Corrections In Your Script:
You were missing = after id
You were missing & after title
Suggestion:
You can give your menu links a class and style as per your menu needs :)
get_menu() has to get reference to $db somehow. Probably the best and easiest way is to pass that reference as parameter:
function get_menu(MyDatabaseHandler $db) {
// code proposed by Sarfraz here
}
now here you already have a mistake:
<?php echo $title; ?>
notice the = after id
you can't be too careful
First, separate the part where you're doing something, and the one used to display things.
Second, the alternative syntax looks better for the display part.
<?php
function get_menu(){
$items = array();
$select = 'SELECT * FROM pages';
$query = $db->rq($select);
while ($page = $db->fetch($query)) {
$items[] = $page['id'];
}
return $items;
}
$menuItems = get_menu();
?>
<ul>
<?php foreach($menuItems as $item): ?>
<li><?php echo $item['title']; ?></li>
<?php endforeach;?>
</ul>
The code Sarfraz posted is going to create invalid anchor tags (i.e. links). They'll also be missing names. Here is the shorter/faster version:
function get_menu($db)
{
$result = $db->rq('SELECT id,title FROM pages');
$menu = '';
while ($page = $db->fetch($result))
{
$id = $page['id'];
$title = $page['title'];
$menu .= "<a href='page.php?id={$id}&title={$title}'>{$title}</a>\n";
}
return $menu;
}
To use that do this:
echo get_menu($db);
The error you were getting was probably resulting from not passing the database connection to the function.
NOTE: It's generally not a good idea to show database ID numbers to the user in the interest of security; slugs are much better for identifying pages and are SEO friendly. Also, there shouldn't be any need to pass the page title to page.php because if you've got the ID you can get that when you need it from the database. Here's the code with this in mind:
function get_menu($db)
{
$result = $db->rq('SELECT id,title FROM pages');
$menu = '';
while ($page = $db->fetch($result))
{
$menu .= "<a href='page.php?id={$page['id']}'>{$page['title']}</a>\n";
}
return $menu;
}
just put function get_menu() { above your code and } below
or like this??
function get_menu( $title, $id ) {
$menu = '';
$menu .= '<a href="page.php?id' . $id . '" title="' . $title .'></a>'
echo $menu;
}
------------------------
$select = 'SELECT * FROM pages';
$query = $db->rq($select);
while ($page = $db->fetch($query)) {
$id = $page['id'];
$title = $page['title'];
get_menu($title, $id );
}
function getMenu()
{
$select = 'SELECT FROM pages';
$query = $db->rq($select);
$menu = new Array;
while ($page = $db->fetch($query)) {
$menu[] = '';
}
return $menu;
}