Create a handy search id function with Codeigniter

The Digital Image Archive for The Robin and Lucienne Day Foundation is a great project to work on from the point of view of web development, because from time to time new issues emerge and the system needs add-ons or changes. I have already built a complex search function to cross-reference the academic cataloguing in the Design Archive, but sometimes what’s needed to help office administration is a very straightforward bit of magic! In this case the person in the office needs to look up a particular reference number (called “Archive ID”), or a list of numbers, which they know are in the Archive but can only go to by scrolling through pages of image thumbnail sets.

As an aside; recently I have been enjoying working with Jamie Rumbelow’s MY_model in Codeigniter which helps enormously in cutting down model classes and functions, making it simpler to re-visit systems and add capability. But… and you knew I was going to say this… I wrote the Day Foundation Archive system before diving into MY_model so just had a bit more unravelling to do to remind myself where this widget would fit. It’s always great to see when I’ve progressed my understanding and implementation of my PHP systems and even though I’m kicking myself that I didn’t get into it earlier, Jamie Rumbelows principles – “fat model, slim controller” – give me a working philosophy for this update.

So here’s my plan for a “search for Archive ID” widget in Codeigniter (without Jamie Rumbelow I’m afraid), which can search for an individual ID or comma-separated list of IDs, and return a thumbnail preview as well as a list of invalid IDs, (so that the viewer knows the numbers were received and weren’t just discarded by the system).

My search form is made up of two views which go in Codeigniter’s “views” folder.

The form itself:

The search id form – id_search_form.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

echo form_open("welcome/searchid");

$data=array(

"name"=>"term",

"id"=>

"term",

"maxlength"=>"64",

"size"=>"15",

"style"=>

"width: 150px",

"placeholder"=>"id or comma separated"

);

echo form_input($data);

echo form_submit("submit","search for accession id");

echo form_close();

and the results view:

Search ID results – searchid.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

if(!empty($missing)){

echo'These accession ids are not valid: '.$missing;

}

else

{

echo'';

}echo'Search Results';

if(count($results)){

foreach($results as$key=>$list)

{

$filename=asset_url().'thumb/'.$list['image'];

$thumb=array(

'src'=>$filename,'

alt'=>$filename,

'class'=>''

);

echo img($thumb).'<br />';

echo$list['id'].'<br />';

echo$list['name'];}

}else{

echo"<p>Sorry, no records were found to match your search term.</p>";

}

In my controller (welcome.php) I have the searchid() function

1

2

3

4

5

6

7

8

9

10

11

12

13

functionsearchid(){

if($this->input->post('term')){

$this->session->set_userdata('termdata',$this->input->post('term'));

}

$term=$this->session->userdata('termdata');

$results=$this->MProducts->searchid($term);

$data['results']=$results[0];

$data['missing']=$results[1];

$data['main']='searchid';//I also have a template view which loads $main using

//<?php$this->load->view($main);?>

$this->load->vars($data);

$this->load->view('template');

}

In my model class (models/mproducts.php) there are two functions: one to check if an individual ID exists at all (so that I can show any ids that don’t exist as $missing) and one to return each ID’s columns.

A note on the file naming convention for Codeigniter – Class names must have the first letter capitalised with the rest of the name lowercase. When I started working on this application, model filenames could be in lower case, but in more recent versions the filename must match the class name, so also with the first letter capitalised.

The function to check if product id exists. This function will get used within the main function:

You can find out more about the Robin and Lucienne Day Foundation at their website www.robinandluciennedayfoundation.org. The Archive is not open to the general public, but image loans may be possible for press and research purposes.