Home > Software engineering >  Matching array of string to numbers to get database values
Matching array of string to numbers to get database values

Time:10-22

In my database, I have a status column where I'm using numbers to represent a status of a product. Each of these numbers represents a string value, for example 1 is open, 2 is closed, etc. Now to display a count of these statuses in my webpage, I am converting the following numbers into a string to display them to the user:

array(1=>'Open',8=>'Hot',2=>'Closed',3=>'Transacted',4=>'Dead',9=>'Follow Up',11=>'Working')

Now I have this count as a clickable link, where the user can click the count and it takes them to a new page showing the details of that item. For which I'm using this:

<a target='_blank' href='".site_url('reports/viewall?status=' . $status)."'>".$num."</a>

This correctly passes the argument to the URL and shows http://localhost/reports/viewall?status=Closed

Now I'm storing this variable in my controller as $status, but I cannot use this string value in my model query which is this since it is giving the string value of status and not the number related to it:

SELECT * from TABLE1 where status = $status

Controller Class where I'm storing the $status:

$status = $this->input->get('status');
$content['individualleads'] = $this->leads_model->get_all_leads($status )->result();

CodePudding user response:

I think the best approach would be to pass the integer in the URL and use the array text value only when it is text meant for the user. So just:

http://localhost/reports/viewall?status=2

Another alternative is to flip the array and access the text key:

$status_num = array_flip($array)[$status];

Or search the original array:

$status_num = array_search($status, $array);

Probably more standard might be to have a status_types table with the integer status_id and text status_text for each status and join this when retrieving the status.

  •  Tags:  
  • php
  • Related