Deepak Keynes
Deepak Keynes

Reputation: 2329

Pass every loop variable to Javascript function

The images are stored inside separate folders.

Both the folders and the images are saved in the database. In my gallery page, they are shown from the database in two steps.

  1. The folders are retrieved from the db and listed in the gallery page
  2. while on-click of a folder, the images are listed.

So far, I have passed the data from the database to the view. The following is my controller code.

function image_show() {
    $this->load->model('gallery_model');
    $data['folder_info'] = $this->gallery_model->get_folder_names();
}

and my view code is as follows:

<?php foreach($folder_info as $show){ ?>
<a class="glyphicon glyphicon-folder-close" style="font-size: 100px;" aria-hidden="true" onclick="imageslide(' .$show['folder_name'] .');" ></a>    
<?php } ?>

From the above code, the folders are listed in the gallery page. I am struck while trying to pass the $show['folder_name'] from the php foreach loop so that I can generate a image slider. My script is

function imageslide(name) {
    alert(name);
}

The error shown to me is

gallery:194 Uncaught SyntaxError: missing ) after argument list

Kindly help me out!

Upvotes: 1

Views: 62

Answers (1)

Alessio Cantarella
Alessio Cantarella

Reputation: 5201

You should pass the $show['folder_name'] variable inside a php block, i.e.:

<?php foreach($folder_info as $show){ ?>
<a class="glyphicon glyphicon-folder-close" style="font-size: 100px;" aria-hidden="true" onclick="imageslide('<?php echo $show['folder_name'] ?>');" ></a>
<?php } ?>

Upvotes: 2

Related Questions