Welcome Guest, Not a member yet? Register   Sign In
how to edit
#1

[eluser]Mitja[/eluser]
This is my code

Code:
$id = $this->uri->segment(3);    //segment iz URLja
$data['query'] = $this->db->get_where('arrangement', array('arrangement_id' => $id));
$this->load->view('admin/arrangement_edit', $data);

how can i in view/admin/arrangement_edit use this function without foreach.

Code:
<tr><td>&lt;input type="text" name="title" value="&lt;?= $query-&gt;title?&gt;"></td></tr>

Ofcourse this is not working, how must i use it?
#2

[eluser]Derek Allard[/eluser]
The short answer is, use foreach. It is specifically there to help you get through these types of structures.
#3

[eluser]Michael Wales[/eluser]
A few things here - we'll just chomp them down one at a time.

Code:
$id = $this->uri->segment(3);    //segment iz URLja

This is usually much easier to write as a parameter of the method - because then you can test for failure and degrade gracefully:
Code:
function my_method($id = NULL) {
  if ($id !== NULL) {
    // Continue with our app
  }
  redirect('');
}

This code is merely going to return TRUE or FALSE.
Code:
$data['query'] = $this->db->get_where('arrangement', array('arrangement_id' => $id));

All of the Active Record functions return TRUE/FALSE based on whether the query was successfully executed at your database. Based on your foreach() discussion, it seems as if you are just trying to return a single result (if you have more than one you pretty much always want to use foreach()), so I'll show you that.
Code:
$query = $this->db->get_where('arrangement', array('arrangement_id' => $id), 1, 0);
$data['article'] = $query->row();

Hope that gets you going in the right direction and if you need further help, just ask!
#4

[eluser]Mitja[/eluser]
thx for answer

Code:
function my_method($id = NULL) {
  if ($id !== NULL) {
    // Continue with our app
  }
  redirect('');
}

i add this function in model. But how can i with this function check if segment is integer ot string and if database query is not null?

I try to write a function which check if segment is integer and databse query is not null bot not working.

Thx
#5

[eluser]Chris Newton[/eluser]
Code:
if (is_int($this->uri->segment(3)) {
   //is an integer
} else {
    // is not an integer
}

Code:
$data['query'] = $this->db->get_where('arrangement', array('arrangement_id' => $id));
if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->id;
   }
}
else
{
// it's null
}
#6

[eluser]Mitja[/eluser]
thx but

Code:
$id = $this->uri->segment(3);    //segment iz URLja
        
if (is_int($id)) {

always thew an excpetion. $id is never int but it is. http://localhost/bewoop/index.php/admin/...nts_edit/1

$id is 1
#7

[eluser]Rick Jolly[/eluser]
Yep, $id is a string. Here is a function that I use to test for an integer string:
Code:
public static function int_str($num)
{
    if ((! isset($num)) || (strval(intval($num))) !== $num))
    {
        return false;
    }

    return true;    
}
Anyone have a better method?

If you just want to test for a numeric string you can use is_numeric().
#8

[eluser]Mitja[/eluser]
Rick Jolly where do you keep this function, under model or somewhere else?

I use it like this
Code:
if ($this->function_model->int_str($id))

Is this ok?

I also find that if i cast is_int work ok

Code:
$id = (int)$this->uri->segment(3);    //segment iz URLja
if (is_int($id))
#9

[eluser]Rick Jolly[/eluser]
I put that function in a static class, but using CI conventions you'd be best to put it in a helper - just drop the static keyword from the function definition.

If you cast to int without knowing if the $id is an integer string then you can get unexpected results:
Code:
$id = 'asdf';
echo((int)$id); // prints 0
$id = '8asdf';
echo((int)$id); // prints 8
$id = '8.9';
echo((int)$id); // prints 8
#10

[eluser]Mitja[/eluser]
Quote:I put that function in a static class, but using CI conventions you’d be best to put it in a helper - just drop the static keyword from the function definition.

what do you mean with just drop the static keyword from the function definition.




Theme © iAndrew 2016 - Forum software by © MyBB