• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help newbie with query

#1
It's a couple of weeks that I'm playing with CodeIgniter in my spare time, I like it, but I'm totally newbie so please .... be patient !

In my DB I have 2 tables 'authors' and 'songs'

CREATE TABLE `authors`(`Anome` varchar(160) ...

CREATE TABLE `songs` (`Bnome` varchar(160) ,
                         `Bautore` varchar(160) ....

in "views/myappl/index.php" I list all the authors
<a href="<?php echo site_url('myapp/indexAutore'); ?>">lista indexAutore</a>

"views/myappl/indexAutore.php" contains
foreach ($authors as $authors_item):
   $autore=$authors_item['Anome'];
   echo "<p><a href=". site_url('myapp/braniXautore/'.rawurlencode($autore));
   echo ">view detail</a></p>";
endforeach;

when the user click on an author (href) I would like to list all his songs (all the songs of 'Anome' in authors <=> Bautore in songs)

in "views/myappl/braniXautore.php" there is
foreach ($songs as $songs_item):
   $brano=$songs_item['Bnome'];
   echo $brano;
endforeach;

Unfortunately 'Bnome' appears as null, so all the songs of all the authors are listed
:-(
What am I missing ?
What am I doing worng ?

THANKS for your PATIENCE !!!

the model contais:
------------------------------------------------------------------------------------------
   public function get_authorsBrani($Bnome = FALSE)  {
      if ($Bnome === FALSE) {
         $query = $this->db->get('songs');
         return $query->result_array();
      }
      $Bnome=urldecode($Bnome);
      $query = $this->db->get_where('songs', array('Bnome' => $Bnome));
      return $query->row_array();
   }
------------------------------------------------------------------------------------------

in routes there is:
------------------------------------------------------------------------------------------
$route['myappl/indexAutore'] = 'songs/indexAutore';
$route['myappl/braniXautore'] = 'songs/braniXautore/$1';
$route['myappl/braniXautore/(:any)'] = 'songs/braniXautore/$1';
$route['myappl/(:any)'] = 'songs/braniXutore/$1';
$route['myappl'] = 'songs';
------------------------------------------------------------------------------------------
Reply

#2
1. In models, You should separate the methods where you get the single item and multiple items. For example it is better to have a method to get specific song, and another method to get multiple songs.


2. Example of method to get songs:
//replace column names for your needs.
PHP Code:
public function get_songs($filter$order = array('column' =>'released_at''dir' => 'desc')){

$this->db->select('*')
->
from('songs s');

if(isset(
$filter['user_id'])){
$this->db->where('s.user_id');
}
$this->db->order_by($order['column'], $order['dir']);

$query $this->db->get();
if(
$query){
return 
$query->result_array();
}

return 
false;

3. I suggest you to filter by id's not by name, so you won't have to use urldecode() function.
if in your model you did not encode url you should not decode it in model. 
Reply

#3
thx !
this helped me A LOT !
have a nice week-end.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.