Welcome Guest, Not a member yet? Register   Sign In
Help with CI, PHP & Javscript
#1

[eluser]the_unforgiven[/eluser]
Hi All,

I have some code am using php and sql to grab the data, but what i want to know is how do i do the following:

When I link is clicked on on the left side of the page how do i get the content using a foreach method in php to display on the right side of the same page by using javascript or something similar?

My php code is (which is the display box on the right where I want the data from database to be displayed)
Code:
<div id="content-right">
              
        <div id="display">
        &lt;?php
        $query = $this->db->query("SELECT * FROM mcd_events ");

        foreach ($query->result() as $row)
        {
                $id = $row->id;
        ?&gt;
                <p>&lt;?php echo $row->description ?&gt;</a></p>
                
        &lt;?php } ?&gt;
        </div>
        <div class="clear"></div>
</div>


then on the left in another div i have some links which are like this:

Code:
<div id="content-left">
<div id="events" class="scroll-pane">

        &lt;?php
        $query = $this->db->query('SELECT * FROM mcd_events');

        foreach ($query->result() as $row)
        {
                $row->id;
        ?&gt;
                <h3>Date:&lt;?php echo date("F j, Y",strtotime($row->created_on)) ?&gt;</h3>
                
                <p><br /><a >id ?&gt;" id="linkid">&lt;?php echo $row->title ?&gt;</a></p>
                
        &lt;?php } ?&gt;

</div>
</div>


Can anyone help me with this please, thanks for reading and will look froward to your replies....
#2

[eluser]cideveloper[/eluser]
This is not how MVC works. you should have a controller call a function in a model to get your database results and then pass it to your view. After you deal with that can of worms, you don't really need javascript to do this. you just need to pass the values of the link to your controller and run the query for the right side. The thing about your code is that both the left and right have the exact same sql. You would assume the left side would have the sql to pull up a list while the right sql should pull up details based on the passed values. Once you get it all set up without javascript then you can add jquery and run some ajax calls to populate the right side. I always do it this way for public facing applications cause you don't know if someone will have javascript turned off.
#3

[eluser]the_unforgiven[/eluser]
Hi thanks for the reply,

So if i set this up in the controller then what should I do
#4

[eluser]the_unforgiven[/eluser]
I now have this in my controller

Code:
function index() {
        
    $data['events']=$this->Eventsmodel->getAllEvents();
        
    $this->load->view('events', $data);
}
My Model
Code:
&lt;?php

class Eventsmodel extends Model {

    function __construct()
    {
        parent::__construct();
        // load database class and connect to MySQL
        $this->load->database();
        
    
    }
    function getAllEvents(){
        
        $qs = "SELECT * FROM mcd_events WHERE published='1'";
        $query = $this->db->query($qs);
        
        if($query->num_rows()>0){
            // return result set as an associative array
            return $query->result_array();
        }
    }
    
    
}

Then i am just echoing out
Code:
&lt;?php echo $events['description'] ?&gt;

Firstly is all this correct, if so what do I do next to do what my first post suggests?

Thnx
#5

[eluser]boltsabre[/eluser]
Firstly, I think you'll need two model functions to achieve the functionality you want. First call, to make the list on the LHS, the second to populate the RHS with the relevant data. For the sake of maximising efficiency and minimising the workload the DB has to do, I'd make your first query, for the LHS, return just the fields you need. Your current query:
Code:
$qs = "SELECT * FROM mcd_events WHERE published='1'";
is pulling ALL the information from the DB table, where as I think you only need the id, created_on, and title yes? Change your query to this
Code:
public function getLeftNavigationList(){
   $qs = "SELECT id, created_on, title FROM mcd_events WHERE published='1'";
   ... rest of code

And then you want a second model function to just get the stuff you need for the RHS (description), like such:
Code:
public function getRightDetails($id){
   $qs = "SELECT description FROM mcd_events WHERE id=$id";
   ... rest of code
[/code]

This is much better, a tiny little bit more coding, two functions instead of one, but it should be obvious the advantages (your code is now more modular and the DB workload is much less!)

Okay, so you now have two different model functions returning the correct data. You'll now need a controller to handle them. Perhaps a controller (index, or another one? your choice of naming conventions) to display the page on the first load, displaying the LHS list, with no data in the RHS because the user hasn't clicked on one yet. That's fairly simple, you just need to pass the data from your model function getLeftNavigationList to the view and iterate through the result to make your list. You seem to have a handle on that, so I won't go into it.

Then in your view, set up anchor links, as you have, passing the $id. Direct there links to a controller, which is set up like such (getting the $id, and passing it to the correct model function):
Code:
public function getRHSDetails($id){
   if($id){
      call your model getRightDetails($id)
   }
   process the result if you need to.
   load view with this data
}

Anyway, that's it in a nutshell. Get that figured out and working first like cideveloper said - always design for javascript disabled, and if you have the time/budget, then integrate JS cool stuff!

Once you have the above working, google around for some "CodeIgniter AJAX tutorials". That'll help you integrate some client side funky functionality.
#6

[eluser]the_unforgiven[/eluser]
Thanks for that makes a great deal of sense now, much appreciated.
#7

[eluser]boltsabre[/eluser]
No worries, you just have to get into the habit and mindset that when pulling info from the db to only retrieve what you need, and nothing else.

And with that in mind, if you're just checking for an instance of something in a table and there could be more than one, use a limit = 1 (or perhaps there is an active records thingie for it, I haven't checked) so that way once it has found a match, it stops there.

Consider this:
Code:
$check = this->model->checkForSubCat($id)
   if ($check->num_rows() > 0{
      ....
  }

And your model function checkForSubCat($id) has one of these two queries:
Code:
"SELECT id FROM yourtable WHERE subcategory = ."$id".";
Or...:
Code:
"SELECT id FROM yourtable WHERE subcategory = ."$id". LIMIT 1";
The first will check the entire table. Even if it finds a match in row 1, it will keep checking, and adding to the result it passes back (there may be a million matches!), all the way down to row 999,999,999,999,999 or however many rows you have in that table. The second, once it finds a match, exits straight away and only returns one row.

Anyway, good luck with it all!
#8

[eluser]the_unforgiven[/eluser]
boltsabre thanks for the in depth explanation, helps really well, I'm still a noobie at CI and thanks to you I know have an idea on how to make this work.

Thanks again dude!
#9

[eluser]the_unforgiven[/eluser]
How would I call this in the views file now to retirve the info

like i did with thr foreach so that once the link on the left is clicked it will show the description on the right???
#10

[eluser]boltsabre[/eluser]
It's okay, it's a really nice helpful community here. We all had to start somewhere, and we've all had to ask questions (which I still do here on the forum on a regular enough basis).

Anyway, I cannot give an in depth answer, about to head out with the missus to watch some fireworks, but I'll give it a try.

Depending on you needs/requirements there are several ways you could populate the main content. But this one should do the trick for you! I'm assuming you already have your model function pulling your LHS list and another model function pulling the 'main contents', and that you have a controller which is loading a view and that you already have this view displaying your LHS list using a foreach loop?

In which case, you can just make an link/anchor in this foreach loop, and point it straight back to the same controller, but pass the $id in with it. You'll need to check in your controller if this $id is being passed to it, so that it knows if to make a call to the model to get the main contents. So you're controller will need to now look like this:
Code:
public function thisIsYourContollerFunction($id = null)
   //stuff you already have in here to get you LHS list
   // already being passed to view using the $data variable

   // if $id is passed, make call to model and get main contents and pass to view in $data
   if($id != null){
      $data[mainContents] = $this->modelName->getMainContent($id);
   }
   $this->load->view('yourView', $data);

Simple, now you have a check in your controller, and your only getting your main data if you pass the variable $id to it.

So in your view, in your foreach loop to make the LHS list, just make an anchor as pass $id to it.
Code:
foreach $lhsList as $list){
   echo anchor('contollerName/thisIsYourContollerFunction/'.$id, Whatever you want here to display);
}

And in your 'main area just run a check to see if there is main contents to display
Code:
if ($mainContents){
echo out your main contents
}else{
display generic stuff when there is no main contents
}




Theme © iAndrew 2016 - Forum software by © MyBB