join query with bonfire (codeigniter) - php

I'm trying to do a join query in bonfire (which is great for the beginner i am btw)
I first tried to set a public function in the method, but after a while, i though it would be better to put it in the controller (maybe i'm wrong...?)
However, i guess my join request is valid, but i can't get it in my view...
Here is my controller:
class Patient extends Admin_Controller {
public function __construct() {
parent::__construct();
$this->load->model('post_model');
Template::set_block('search/search', 'search/search');
//Template::set('toolbar_title', 'Manage Your Blog');
Template::set_theme('custom', 'default');
Template::set_block('nav', 'nav');
}
public function detail($id = null) {
$this->load->helper('typography');
$this->load->model('operation_model');
$this->db->select('*');
//$this->db->from ('operation AS ope');
$this->db->from('operation');
$this->db->join('patient', "patient.zkp_pat = operation.zkf_pat ");
$query = $this->db->get();
$post = $this->post_model->find($id);
//Template::set('post', $query);
Template::set('post', $post);
Template::set_view('detail');
Template::render();
}
}
And my view file:
<code>
<div class="post">
<?php
echo ($post->nom . ' ');
echo ($post->prenom . ' ');
echo ($post->zkp_pat . ' ');
echo ($post->dob);
foreach ($query as $row) :
echo ($row->zkf_pat . ' ' );
echo "titre de l'opération: " . ($row->titre);
endforeach;
?>
</div>
</code>
Thanks for your advices and help !

I asked for help on the BF forum too, and somebody gave me the solution=>hopefully this will help somebody else. The issue was with the $query , which is a DB_result object.
http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
to solve the problem i had to doo: $query->result()
Final code:
Controller:
public function detail ($id = null){
$this->load->helper('typography');
$this->db->from ('operation as ope');
$this->db->join ('patient',
"patient.zkp_pat = ope.zkf_pat " );
$this->db->where ('patient.zkp_pat', $id);
$query = $this->db->get();
$post = $this->post_model->find($id);
Template::set('query', $query);
Template::set('post', $post);
Template::set_view('detail');
Template::render()
}
And view file:
<div class="post">
<h2><?php e($post->nom); ?></h2>
<?php
echo ($post->nom . ' '); echo ($post->prenom . ' ');
echo($post->zkp_pat . ' ');
echo ($post->dob);
foreach ($query->result() as $row) :
echo '<div>'
echo " \n";
echo ($row->zkf_pat . ' ');
echo "titre de l'opération: " . ($row->titre);
?>
</div>
<?php
endforeach;
?>
</div>
$query->get()->result() returns an object with all fetched data,
whereas $query->get()->row()returns only a single result row, if the result has one row, it returns only the first one. The result is given as an object...
If you like my edit, please upvote, that would be very helpful !

Related

PHP function called on link click not giving any results

I'm trying to solve this for some time. I have PHP function that is called on link click. When I click on link it's directing me to this function but it's not giving any results.
Here is how I call function.
foreach ($bandsN as $aa) {
$str = explode(',', $aa);
$next = $str[1];
?>
<?php echo $str[0]?> </div> <?php
if (isset($_GET['other'])) {other($next);}
}
In this function it's called mysql stored procedure with parameter of php function.
function other($var)
{
echo $var;
if (!$mysqli->multi_query("CALL p($var)")) {
echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
do{
if($resul = $mysqli->store_result()){
$bands = array();
while($ro = $resul->fetch_row())
{
$bands[] = implode(',', $ro);
}
foreach ($bands as $aa) { ?>
<p > <?php echo $aa?> </p> </div><?php
}
$resul->free();
unset($bands);
}}
while($mysqli->more_results() && $mysqli->next_result());
}
I don't know which is the problem. This code inside function is working because I already have it in other part of app.
If somebody can see what is wrong I would be very grateful.

codeigniter how to restrict number of url segments in a page

I am having a codeigniter function that does searching which is working fine but the issues arises when I am making a new search on a single page whereby on clicking search button the same url of the single page duplicates on the url bar thus taking me to a wrong link. see how it behaves in the below snippets;
http://localhost/newsapp/bulletins/view/31
http://localhost/newsapp/bulletins/view/view/31
http://localhost/newsapp/bulletins/view/view/view/31
http://localhost/newsapp/bulletins/view/view/view/view/31
here are the functionss;
public function livesearch() {
$keyword = $this->input->post('keyword');
$query = $this->news_model->get_live_items($keyword);
foreach ($query as $row):
echo "<li><a href='view/$row->id'>" . $row->title . "</a></li>";
endforeach;
}
This one displays the search results in a another page:
public function search_keyword()
{
$keyword = $this->input->post('keyword');
$data['results'] =$this->news_model->get_live_items($keyword);
$data["top_news"] = $this->news_model->topnews();
$data["latest_news"] = $this->news_model->latestnews();
$this->load->view('result_view',$data);
}
finally this where all the magics are happening;
function view($id)
{
$data['news'] = $this->news_model->get_one_news($id);
$data["top_news"] = $this->news_model->topnews();
$data["latest_news"] = $this->news_model->latestnews();
$data['content'] = 'single'; // template part
$this->load->view('includes/template',$data);
}
Your livesearch method should be
public function livesearch() {
$keyword = $this->input->post('keyword');
$query = $this->news_model->get_live_items($keyword);
foreach ($query as $row):
echo "<li><a href='" . base_url('bulletins/view/' . $row->id) . "'>" . $row->title . "</a></li>";
endforeach;
}
Try giving a full link like this
echo "<li><a href='".base_url()."view/$row->id'>" . $row->title . "</a></li>";
You are just including the view/$row->id so it is adding the url rather than generating the required url.

function only returns one value multiple times

I have this function:
function get_content($text_to_match) {
$query = "SELECT * ";
$query .= "FROM table_name ";
$query .= "WHERE one_column_name LIKE '%{$text_to_match}%' OR another_column_name LIKE '%{$text_to_match}%'";
$cont = mysqli_query($connection, $query);
if($content = mysqli_fetch_assoc($cont)) {
return $content;
} else {
return null;
}
}
But when I call it like:
<div>
<?php
for ($i = 1; $i < count(get_content("text_to_match")); $i++) {
echo '<article>' .
'<h3>' . get_content("text_to_match")["string1"] . '</h3>'.
'<p>' . get_content("text_to_match")["string2"] . '</p>' .
'</article>';
}
?>
</div>
I only get the first match in the DB repeated as many times as the number of found items.
Where have I gone wrong?
use this code then fetch data properly
while($content = mysql_fetch_array($cont))
{
return $content;
}
Your logic is at fault. You are calling get_content function to get all matches for the loop, as well as to get individual elements out of the list. This is:
bad logic - the 2nd use case doesn't make sense
excessive - you shouldn't need to run a database query just to output an already retrieved result
What you probably want to do is:
foreach (get_content('text_to_match') as $content) {
echo '<article>';
echo '<h3>' . $content['string1'] . '</h3>';
echo '<p>' . $content['string2'] . '</p>';
echo '</article>';
}
With a few modifications in combination with tips from #Anant and #Unix One's answer, I arrived at this working solution:
Function definition
function get_content($text_to_match, $multiple=false) {
$query = "SELECT * ";
$query .= "FROM table_name ";
$query .= "WHERE one_column_name LIKE '%{$text_to_match}%' OR another_column_name LIKE '%{$text_to_match}%'";
$cont = mysqli_query($connection, $query);
if ($multiple) {
$content_array = [];
while($content = mysqli_fetch_array($cont)) {
$content_array[] = $content;
}
return $content_array;
} else {
if($content = mysqli_fetch_assoc($cont)) {
return $content;
} else {
return null;
}
}
}
Function calls
<?php
/* multiple items */
foreach(get_content("text_to_match", true) as $content) {
echo '<article>' .
'<h3>' . $content["string1"] . '</h3>' .
'<p>' . $content["string2"] . '</p>' .
'</article>';
}
?>
<?php
/* one item */
echo get_content("text_to_match")["string"];
?>

Codeigniter: foreach method or result array?? [Models +View]

I am currently following tutorials on viewing data from the database using the Framework Codeigniter. There are various ways in which I've learnt. Is there is a more realiable way- either displaying as an array or using 'foreach' in the view file? Any opinions would be helpful.
This is my code using the two methods:
Method 1 Model:
function getArticle(){
$this->db->select('*');
$this->db->from('test');
$this->db->where('author','David');
$this->db->order_by('id', 'DESC');
$query=$this->db->get();
if($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}$query->free_result();
}
}
Method 1 View file:
<?php foreach($article as $row){ ?>
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
<p><?php echo $row->author; ?></p>
<p><?php echo $row->date; ?></p>
<?php } ?>
Method 2 Model:
class News_model extends CI_Model {
function getArticle(){
$this->db->select('*');
$this->db->from('test');
$this->db->where('author', 'David');
$this->db->order_by('id', 'DESC');
$query=$this->db->get();
if ($query->num_rows()>0) {
return $query->row_array();
}
$query->free_result();
}
Method 2 View file:
<?php echo '<h3>' .$article['title'].'</h3>' ?>
<?php echo '<p>' .$article['content']. '</p>' ?>
<?php echo '<p>' .$article['author']. '</p>' ?>
<?php echo '<p>'. $article['date']. '</p>' ?>
I would do it like that:
Model
function getArticle() {
$this->db->select('*');
$this->db->from('test');
$this->db->where('author','David');
$this->db->order_by('id', 'DESC');
return $this->db->get()->result();
}
}
Controller
function get_tests() {
$data = array(
'tests' => $this->mymodel->getArticle()
}
$this->load->view('myview', $data);
}
View
<table>
<?php foreach($tests as $test) { ?>
<tr>
<td><?php echo $test->title;?></td>
<td><?php echo $test->content;?></td>
<td><?php echo $test->author;?></td>
<td><?php echo $test->date;?></td>
</tr>
</table>
If you wish to work with arrays rather than objects, in your model change line
return $this->db->get()->result();
to
return $this->db->get()->result_array();
and in views echo like
<td><?php echo $test['title'];?></td>
P.S.
In your code you use $query->free_result(); but it doesn't even run because when you use keyword return everything after that is not even parsed. It's not necessary to free results anyway.
P.S.2.
You use if($query->num_rows() > 0) { but don't have the else part, it means that it's not necessary too. If you'll return no lines to your foreach statement in the view, you won't get any errors.

PHP - Show results, conditional based on link

Looking for some help with the code below. I think there may be an issue with the way I've organized the php call and function calls. I'm a beginner and am trying to allow users to display results from different tables, depending on what link they click.
I get this error:
Parse error: syntax error, unexpected '{' in /home/content/c/e/l/celebrything/html/wp-content/themes/celebrything/sidebar.php on line 16
Any help in correcting the issue here would be amazing. Here's the code:
<div id="sidebar">
<div class="post">
<h2>
<font color="#333333">Most Popular Celebrities</font><br>
<font color="#333333">in last 24 hours</font>
<br>
<br>
Today
Week
Month
<?php
if (!in_array($table, array('today', 'week', 'month')) {
return false;
}
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';
}
}
?>
showTable($_GET['table']);
</h2>
</div>
</div>
<div class="clear"></div>
UPDATED CODE----------------
<div id="sidebar">
<div class="post">
<h2>
<font color="#333333">Most Popular Celebrities</font><br>
<font color="#333333">in last 24 hours</font>
<br>
<br>
Today
Week
Month
<?php
if (!in_array($table, array('today', 'week', 'month'))) {
return false;
}
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
urlencode($row->name) . '&search=Search">' . $row->name .
'</a> - ' . $row->count . ' Posts<br/>';
}
if (!empty($_GET['table'])) {
showTable($_GET['table']);
} else { showTable('today'); }
?>
</h2>
</div>
</div>
<div class="clear"></div>
1. Missing a ) on the first if block
if (!in_array($table, array('today', 'week', 'month'))) {
return false;
}
2. There is an extra } right before the closing ?>
}
}
?>
3. You need to put the showTable function before the closing ?> like:
showTable($_GET['table']);
?>
In Summary:
Get a code editor that supports syntax highlighting. You will love it.
Looks to me like you're referencing a function -- showTable() -- but you haven't set up your logic inside function (unless you're leaving something out of the code sample). Should be:
<?
//----------------------
//Create the showTable() function, which won't do anything until it's called. It can
//reside anywhere on the page, really. It's here just because this is where I put it.
function showTable($table) {
if (!in_array($table, array('today', 'week', 'month'))) {
return false;
}
global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo(''.$row->name.' - '.$row->count.' Posts<br/>');
}
}
//----------------------
//Here is where you actually call the function, to display some stuff on the page
if (!empty($_GET['table'])) {
showTable($_GET['table']);
} else {
showTable('today');
}
?>
The code above assumes that you're using proper/working functions built into Wordpress (I don't know Wordpress very well). But the syntax above should clear up any function syntax related issues, which I think is your main issue.

Categories