CodeIgniter Forums
params in controller functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: params in controller functions (/showthread.php?tid=51542)

Pages: 1 2


params in controller functions - El Forum - 05-08-2012

[eluser]rotivo[/eluser]
Hello, i am trying to pass like a second param in a controller function an string, but it seems that only recognize numbers like parameter.

I want to pass a query result like a parameter. Copy and paste a peace of the code:

Code:
if ($this->labola->getData($campo,$patron,$tabla)!==false)
  {
   $query = $this->labola->getData($campo,$patron,$tabla);
  
   redirect("secciones/pedidos/a/$query");
  }

I hope your help. Thanks.


params in controller functions - El Forum - 05-08-2012

[eluser]ojcarga[/eluser]
What does the $query var has in it?

If $this->labola->getData($campo,$patron,$tabla) returns a single value I dont see why you can't, but if that is and array maybe you can use serialize() and unserialize()


params in controller functions - El Forum - 05-08-2012

[eluser]rotivo[/eluser]
Thanks for response...the query returns some rows of three fields from a database..


params in controller functions - El Forum - 05-08-2012

[eluser]ojcarga[/eluser]
So, try that way

Code:
redirect("secciones/pedidos/a/".serialize($query));

And then, in the secciones/pedidos method just get the param and unserialize() it.

Just keep in mind that you should not pass sensitive information trough that way.


params in controller functions - El Forum - 05-08-2012

[eluser]rotivo[/eluser]
It´s not sensitive info...but, ¿How can i pass query results to controller, not being this form???? Thanks.


params in controller functions - El Forum - 05-08-2012

[eluser]ojcarga[/eluser]
I would do it using Flashdata:

Code:
$this->session->set_flashdata('the_name_you_want', $query);
redirect("secciones/pedidos/a")

And then, in the secciones/pedidos method you just get it using
Code:
$this->session->flashdata('the_name_you_want');
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html



params in controller functions - El Forum - 05-08-2012

[eluser]rotivo[/eluser]
Thanks for the help...

I´ve tried with sessions and now i could get to pass the query to the controller, but it fails to show it in a loop in the view. The error is:

Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;CI_DB_mysql_result&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in D:\

I would appreciate your help, thanks.


params in controller functions - El Forum - 05-08-2012

[eluser]ojcarga[/eluser]
Two things:

1) Please post the code you are using inside this method:
Code:
getData($campo,$patron,$tabla);

2) Post code of your View where you are trying to show it.

"...but it fails to show it in a loop in the view..."


params in controller functions - El Forum - 05-08-2012

[eluser]rotivo[/eluser]
OK:

FUNCTION:

Code:
public function getData($campo, $patron, $tabla)
{
  $this->db->select('*');
  $this->db->from($tabla);
  $this->db->where($campo, $patron);
  
  
  $query = $this->db->get();
  
  if ($query->num_rows() < 1)
   {
   return false;
   }
   else
   {
   return $query;
   }

}

CONTROLLER:

get from session flashdat and send to the view:
Code:
$data['buscar']=$this->session->flashdata('order');
$this->load->vars($data);

VIEW:

Code:
&lt;?php foreach ($buscar->result() as $row) { ?&gt;
       <tr>
        <td><img id="aceptado" src='&lt;?=base_url();?&gt;../skins/backend/images/aceptado.png' class='botones' alt='Aceptado' title='Aceptar'>/td>
        <td><img id="eliminar" src='&lt;?=base_url();?&gt;../skins/backend/images/eliminar.png' class='botones' alt='Eliminar' title='Eliminar'>/td>
        <td><img id="responder" src='&lt;?=base_url();?&gt;../skins/backend/images/nuevocorreo.png' class='botones' alt='NuevoCorreo' title='Contestar'>/td>
        <td><a href="#lightbox1" rel="lightbox1" class="lbOn"><img id="ver" src='&lt;?=base_url();?&gt;../skins/backend/images/ver.png' class='botones' alt='M&aacute;s informaci&oacute;n' title='Ver' /a></td>
        <td>&lt;input type='text' size="18" &lt;?=$row-&gt;Aceptado==1?' class="aceptado"':'';?&gt; readonly='readonly' value='&lt;?php echo $row-&gt;IdPedido; ?&gt;' /></td>
        <td>&lt;input type='text' size="18" &lt;?=$row-&gt;Aceptado==1?' class="aceptado"':'';?&gt; readonly='readonly' value='&lt;?php echo $row-&gt;Nombre; ?&gt;' /></td>  
        <td>&lt;input type='text' size="18" &lt;?=$row-&gt;Aceptado==1?' class="aceptado"':'';?&gt; readonly='readonly' value='&lt;?php echo $row-&gt;Telefono; ?&gt;'/></td>  
        <td>&lt;input type='text' size="18" &lt;?=$row-&gt;Aceptado==1?' class="aceptado"':'';?&gt; readonly='readonly' value='&lt;?php echo $row-&gt;Email; ?&gt;'/></td>
        <td>&lt;input type='text' size="18" &lt;?=$row-&gt;Aceptado==1?' class="aceptado"':'';?&gt; readonly='readonly' value='&lt;?php echo $row-&gt;Comentarios; ?&gt;'/></td>
       </tr>
      &lt;?php } ?&gt;



params in controller functions - El Forum - 05-08-2012

[eluser]ojcarga[/eluser]
Ok, right now I'm not in my pc so can't test this but I guess you are getting that error cause the <b>getData($campo, $patron, $tabla)</b> method is returning <b>return $query;</b> ,,, I would try using return <b>$query->result();</b> OR also <b>$query->result_array();</b>

And in the view change it for <b>foreach ($buscar as $row)</b>

http://ellislab.com/codeigniter/user-guide/database/results.html

Let me know what happened :-)