Welcome Guest, Not a member yet? Register   Sign In
Alphabetical Menu
#1

[eluser]kashi[/eluser]
Hi everyone.
im new with CI and im having a problem: i want to create an alphabetical menu, you know,
Code:
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z


and what i want (as you imagine) is that when you click on the "A" you get the results of all the objects starting with the letter A. in mysql is something like this

Code:
SELECT *
FROM  `items`
WHERE name LIKE  'a%'
ORDER BY  `items`.`name` ASC
LIMIT 0 , 30

but i dont know how to adapt it to CI.

Hope you can help me!

PS: sorry for my english, is not my native language.
#2

[eluser]Ckirk[/eluser]
Check out the manual section on Active Records

The part, which interests you here is "$this->db->like();"

your code would look something like:
Code:
$this->db->like('name', 'A', 'after');
// set up the where clause -  WHERE name LIKE 'A%'

$query = $this->db->get('items', 0, 30);
// gets the data with a limit of 30 results and an offset of zero

// Now you have a resultset so can echo them out for testing purposes:
foreach ($query->result() as $row)
{
    echo $row->name;
}

Hope that helps
#3

[eluser]kashi[/eluser]
thanks for helping me!

so i should make 26 pages one for each view? or there is a simpler way to do it?
#4

[eluser]Pert[/eluser]
You can use controller attributes to pass in a letter, for example

Code:
class items extends CI_controller
{
   function listing($letter = 'a')
   {
      $this->db->like('name', $letter, 'after');
      ...
   }
}

Now you can do something like this with your url
Code:
/items/listing
/items/listing/a
/items/listing/b
...
#5

[eluser]kashi[/eluser]
And how i add the letters? Using href =item/listing/a
Or there is something else i should know

Again sorry for asking everything.
Thanks !
#6

[eluser]Pert[/eluser]
One option, in your view file

Code:
$this->load->helper('url');
$letters = array();
for ($x= 97; $x< 172; ++$x)
{
   $letter = chr($x);
   $letters[] = anchor(site_url('items/listing/'.$letter), $letter);
}
echo implode(' - ', $letters);
#7

[eluser]kashi[/eluser]
Hi again! haha still not working... i will tell you what i've done

first of all i created a new controller called alphabet.php

alphabet.php
Code:
&lt;?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Alphabet extends Controller
{
   function listing($letters = array())
   for ($x= 65; $x< 91; ++$x)
  
   {
   $letter = chr($x);
   {
      $this->db->like('name', $letter, 'after');
   }
   }
   $this->load->view('letters');
}

then i add this in my layout view (this worked fine)

Code:
&lt;?php $this->load->helper('url');
$z= 35;
  $numeral = chr($z);
  $numerales[ ] = anchor(site_url('letter/'.$numeral), $numeral);

$letters = array();

for ($x= 65; $x< 91; ++$x)
{

   $letter = chr($x);
  
   $letters[] = anchor(site_url('letter/'.$letter), $letter);
  
}
  echo implode($numerales);
  echo " - ";
echo implode(' - ', $letters); ?&gt;

and then i add this to config/routes.php

Code:
$route['alphabet/(:any)'] = 'letter/$1';

the result is: the alphabet menu appeared on the website, and when you click on a letter it links you to, for example, http://mysite.com/letter/a

but that page gives me an 404 error. Oh and i created a new view called letters.php

im totally lost, i dont know what to do, i hope you can help me!

thanks so much!




Theme © iAndrew 2016 - Forum software by © MyBB