Welcome Guest, Not a member yet? Register   Sign In
Getting rows from database and manipulating before snding to the view.
#1

[eluser]Taff[/eluser]
I'm having a little trouble manipulating data after I get it from a database.
The date I am displaying should be in a different format to how it is being stored in the database (2008-04-30) for example.

Currently the user selects a date by using a date picker which creates a date in this format (which is nice and readable at least from a European point of view): 25/04/2008

Before I put these into the database field which has a "type" of date I am manipulating them using

Code:
function add(){
$_POST['start']=$this->_makeDate($_POST['start']);
$this->db->insert('projects',$this->db->escape($_POST));
}

function _makeDate($str){
        $splitDate = explode("/",$str);
        $newDate = $splitDate[2]."-".$splitDate[1]."-".$splitDate[0];
        return $newDate;
    }

which works well, and then I'm throwing it into the database.
Ideally I would like to do the opposite when I'm retrieving it from the database before sending it to the view. No matter what I have tried, I'm getting nowhere fast.
Code:
$tmp = $this->db->query($sql, array($id));
$tmp2 = $tmp->result();
// returns array(1) { [0]=>  object(stdClass)#14 (5) { ["id"]=>  string(2) "30" ["title"]=>  string(11) "new project" ["notes"]=>  string(12) "changed name" ["start"]=>  string(10) "2008-04-26" ["company"]=>  string(1) "0" } }
$tmp3 = $tmp2[0];
//returns object(stdClass)#14 (5) { ["id"]=>  string(2) "30" ["title"]=>  string(11) "new project" ["notes"]=>  string(12) "changed name" ["start"]=>  string(10) "2008-04-26" ["company"]=>  string(1) "0" }

Any attempts to retrieve anything from $tmp3 return
Quote:Cannot use object of type stdClass as array in

How could I access that part of the object, manipulate the date and then return the result?

The second part of my question is that I can't help but wonder if I could be placing the date (in it's original format) into the database and still be able to access it

I'm aware that these questions are possibly not CI related, more to my lack of knowledge of objects in PHP.

Thanks in advance!

Taff
#2

[eluser]GSV Sleeper Service[/eluser]
you could use date() and strtotime() to format your dates. I would store the dates as mysqdl date type (YYYY-MM-DD)
#3

[eluser]Taff[/eluser]
Thanks for your reply, I will look into optimising that part.

Does anyone have any idea as to how I could be accessing the start part of that object?

Cheers,
Taff
#4

[eluser]ekeretex[/eluser]
Code:
$start = $tmp3->start;
should give you your date

To format, use:
Code:
$formatted_start = date('d/m/y', strtotime($start));
#5

[eluser]xwero[/eluser]
Taff if you use the result method you get an object and then you have to use the object syntax. If you use result_array method you can use the code from your snippet to get the date.
#6

[eluser]Taff[/eluser]
Quote:To format, use:
$formatted_start = date('d/m/y', strtotime($start));

Thanks for that, I had tried to get it working, but without any joy, this will help, not just here but in the future too Smile


Quote:Taff if you use the result method you get an object and then you have to use the object syntax. If you use result_array method you can use the code from your snippet to get the date.

That is the difference, I had tried everything mentioned at http://ellislab.com/codeigniter/user-gui...sults.html to no avail, once you said that that was the solution, I looked for the problem somewhere else and solved it in minutes.

Thanks to you both for your assistance, as already mentioned I think my problem lies as much in learning MVC & OOP techniques as it does with CI (probably more), but looking back at what I've built so far I'm happy.

I can see quite a lot of code repeating itself in my controllers, i.e. milestones and to do lists are pretty much identical, in that they contain an id, a title, a project_id, who its assigned to etc, so maybe I could create a class and extend it for each of these 2 controllers with all the basic functionality which I just need to point to the correct model (I had to abandon using a model for the time being, I got totally list between the 3 parts, I could get the model to return something, but couldnt get it into the controller and and and).

Thank you all once again for your help, and not least listening to my ramble. Any links to tutorials that use models would be very much appreciated.

EDIT:

OK, so I can now return the position I need to edit with

Code:
$data['p_query'] = $this->db->query($sql, array($id));
$test2 = $data['p_query']->result_array();
var_dump($test2[0]['start']);

but how can I target that instance directly as opposed to a clone (which is what I think is happening if I change $test2[0]['start'])

Code:
$data['p_query']->result_array()[0]['start']="new content";
doesn't work unfortunately :-)

Taff




Theme © iAndrew 2016 - Forum software by © MyBB