Possible to produce a javascript function from PHP?

#1
[eluser]Robb__[/eluser]
I have a problem I just can't solve. I retrieve a picture from my database that gets loaded as below in getclicklist.


Code:
function getclicklist($id)
    {
    $this->load->model('Load');
    $result = $this->Load->getclicklist($id);
     $resultrow[0] = $result ->row(0);
     $images['Image'] = $resultrow[0] ->Image;
    
     ... something here to produce the outcome below
    
echo $response;
        
        }

The result from this function in the controller should return something to my response function (this is done with AJAX). And in the end produce something that looks like this.

Code:
function show_get_clicklist_response(response) {
    eval(response.responseText)    

YAHOO.example.container.photobox = new YAHOO.widget.PhotoBox("photobox",
                {
                    effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.45},
                    fixedcenter:true,
                    constraintoviewport:true,
                    underlay:"none",
                    close:true,
                    visible:false,
                    draggable:true,
                    modal:true,
                    photos:[{src:"http://static.flickr.com/51/129586913_e78683c466.jpg",caption:"Linus"}, //these image adresses are the core component in the function, and where I would like to put the adress retrieved from getclicklist
                            {src:"http://static.flickr.com/50/129590195_0642f2d96a.jpg",caption:"Linus 2"},
                            {src:"http://static.flickr.com/8/12669712_be928a0d97.jpg",caption:"Dobb's Ferry, NY"},
                           ],
                    width:"500px"
                } );
                YAHOO.example.container.photobox.render();
                
                YAHOO.util.Event.addListener("show", "click", YAHOO.example.container.photobox.show, YAHOO.example.container.photobox, true);
    }

Is this possible? I have not come up with any way to manipulate javascript functions, this is the only way I can see work.

#2
[eluser]Crafter[/eluser]
Well, you want the Javascript code dynamically created right. One way is is embed them in an html file

Create a view result.php

Code:
function show_get_clicklist_response(response) {
    eval(response.responseText)    

YAHOO.example.container.photobox = new YAHOO.widget.PhotoBox("photobox",
                {
                    effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.45},
                    fixedcenter:true,
                    constraintoviewport:true,
                    underlay:"none",
                    close:true,
                    visible:false,
                    draggable:true,
                    modal:true,
                    photos:[
<?                foreach ($photolist as $photo ) ?>
                       {src:"http://static.flickr.com/<?= $photo['image']; ?>",caption:"<?= $photo['caption']; ?>"},
<?                } ?>
                           ],
                    width:"500px"
                } );
                YAHOO.example.container.photobox.render();
                
                YAHOO.util.Event.addListener("show", "click", YAHOO.example.container.photobox.show, YAHOO.example.container.photobox, true);
    }

Then, in your yor controller:
Code:
function getclicklist($id)
    {
    $this->load->model('Load');
    $result['photolist'] = $this->Load->getclicklist($id);      // <== NOTICE THE INDEX TO THE ARRAY
    // $result will have  :
    // array[
    //   [image] => '51/129586913_e78683c466.jpg', [caption] => 'Linus 1',
    //   [image] => '50/129590195_0642f2d96a.jpg', [caption] => 'Linus 2',
    //   [image] => '8/12669712_be928a0d97.jpg', [caption] => 'Dobb's Ferry, NY',
    // ]

    // Call the view result.php and pass $result to it
    $this->load->view('result', $result)
        
        }

#3
[eluser]Robb__[/eluser]
Ok, this looks promising. But before implementing, is the loading of result.php forcing the page to reload or "wander" away to an other page? (I'm trying to do this with AJAX)

Thanks for the help!

#4
[eluser]Robb__[/eluser]
I tried to implement this like

Code:
function getclicklist($sessionid)
    {
    $this->load->model('Load');
    $result['photolist'] = $this->Load->getclicklist($sessionid); //   gets the clicked items for the specified session id
    $noRows = $result['photolist']->num_rows();
    $result['NrRows'] = $noRows;
    $products['productlist'] = array();    
for($i=0; $i<$result['NrRows']; $i++){            
             $resultrow[$i] = $result['photolist']->row($i);
             $products['productlist'][$i] = $resultrow[$i]->Image;
// this now in my test run contains
//$products['productlist'][0]='myweb/www/pictures/earring_bali_with_chevron.jpg'
//$products['productlist'][1]='myweb/www/pictures/907_Gem_hoops.jpg'
//$products['productlist'][2]='myweb/www/pictures/0073481501016_500X500.jpg1'
    }

$this->load->view('result', $products)
        
        }

result.php below
Code:
function show_get_clicklist_response(response) {
    eval(response.responseText)    

YAHOO.example.container.photobox = new YAHOO.widget.PhotoBox("photobox",
                {
                    effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.45},
                    fixedcenter:true,
                    constraintoviewport:true,
                    underlay:"none",
                    close:true,
                    visible:false,
                    draggable:true,
                    modal:true,
                    photos:[
&lt;?                foreach ($products as $photo ) ?&gt;
                       {src:"&lt;?= $photo; ?&gt;",caption:"test"},
&lt;?                } ?&gt;
{src:"http://static.flickr.com/8/12669712_be928a0d97.jpg",caption:"Dobb's Ferry, NY"}                          
],
                    width:"500px"
                } );
                YAHOO.example.container.photobox.render();
                
                YAHOO.util.Event.addListener("show", "click", YAHOO.example.container.photobox.show, YAHOO.example.container.photobox, true);
    }


But (not surprisingly) a blanc screen Sad

#5
[eluser]Crafter[/eluser]
Ok. Here's the thing.

Firstly in your controller.

You need to pass an array with indexes tthat will become local variables in your view.

So if you have in your controller $products['abc'] = 'Hello' , then in your view you can say:

Code:
<hr />
&lt;? echo $abc; ?&gt;
<hr />

If you have in your controller $products['colors'] = array('red', 'green', 'gold'), then in your view you can say:
Code:
&lt;? foreach ($colours as $thecolour) {
       echo $thecolour;
   }
?&gt;

Revisit your Code Igniter manual and make sure you get this.


Then I'm not sure what your full page looks like, but you need to send a full HTML page initially to your browser, from which it can request content using Ajax and such toys. Just sending Javascript code on its lonesome it not gonna do it for you.

#6
[eluser]Robb__[/eluser]
Ok, this is the whole flow.
in header.php
Code:
function get_clicklist(){
        
    var myAjax4 = new Ajax.Request('myweb/www/index.php/application/getclicklist/'  + sessionid ,{onComplete:show_get_clicklist_response});    //sessionid is a global variable.
        }
controller application.php
Code:
function getclicklist($sessionid)
    {
    $this->load->model('Load');
    $result['photolist'] = $this->Load->getclicklist($sessionid);      
    $noRows = $result['photolist']->num_rows();
    $result['NrRows'] = $noRows;
    $products['productlist'] = array();    
for($i=0; $i<$result['NrRows']; $i++){            
             $resultrow[$i] = $result['photolist']->row($i);
             $products['productlist'][$i] = $resultrow[$i]->Image;
    //echo $products['productlist'][$i];
    }

$this->load->view('result', $products)
        
        }

in the model load.php
Code:
function getclicklist ($sessionid)
        {
        $this->load->database();
        $sql = "SELECT J.Image FROM Jewellery J INNER JOIN Choice C ON J.ProductID = C.ProductID WHERE C.Nickname LIKE '" .$sessionid ."'";
        $query = $this->db->query($sql);
        
        return $query;
        }

The show_get_clicklist_response should be created by the $this->load->view('result', $products) call in controller application.php, function getclicklist.

result.php now looks like
Code:
function show_get_clicklist_response(response) {
    eval(response.responseText)    

YAHOO.example.container.photobox = new YAHOO.widget.PhotoBox("photobox",
                {
                    effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.45},
                    fixedcenter:true,
                    constraintoviewport:true,
                    underlay:"none",
                    close:true,
                    visible:false,
                    draggable:true,
                    modal:true,
                    photos:[
&lt;?php foreach ($productlist as $i => $photo ): ?&gt;
                       {src:"&lt;?= $photo; ?&gt;",caption:"test"},
&lt;?php endforeach; ?&gt;
{src:"http://static.flickr.com/8/12669712_be928a0d97.jpg",caption:"Dobb's Ferry, NY"}                          
],
                    width:"500px"
                } );
                YAHOO.example.container.photobox.render();
                
                YAHOO.util.Event.addListener("show", "click", YAHOO.example.container.photobox.show, YAHOO.example.container.photobox, true);
    }


So, hopefully I will have a response function to handle my call to the database, that delivers the clicklist images. The response function created from my call initiates the YUI-stuff needed to create my photobox.
I'm a bit confused now and have to lay down for a while :gulp:

#7
[eluser]Robb__[/eluser]
The screen get's blanc when uncommenting the
// $this->load->view('result', $products)
so the problem must be in result.php? What causes the screen to freeze / turn blanc?

#8
[eluser]Crafter[/eluser]
Robb

Not trying to be nasty here, but it looks like you are way over your head in terms of your understanding of the CI framework.

I suggest you revisit the Code Igniter manual, tutorials and WIKI and bed this down first, then you will find that it becomes easier.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.