PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label cakephp-2.1. Show all posts
Showing posts with label cakephp-2.1. Show all posts

Tuesday, November 1, 2022

[FIXED] How do I use fixed fields in CakeDC's csvUpload behavior in Util plugin

 November 01, 2022     cakedc, cakephp, cakephp-2.0, cakephp-2.1, plugins     No comments   

Issue

I am using the csvUpload behavior of the Utils plugin by CakeDC, on a CakePHP 2.2.1 install.

I have it working great it's processing a rather large csv successfully. However there are two fields in my table / Model that would be considered fixed, as they are based on ID's from from associated models that are not consistent. So I need to get these fixed values via variables which is easy enough.

So my question is, how do I use the fixed fields aspect of csvUpload? I have tried that following and many little variation, which obviously didn't work.

public function upload_csv($Id = null) {
    $unique_add = 69;
    if ( $this->request->is('POST') ) {
        $records_count = $this->Model->find( 'count' );
        try {
            $fixed = array('Model' => array('random_id' => $Id, 'unique_add' => $unique_add));
            $this->Model->importCSV($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
        } catch (Exception $e) {
            $import_errors = $this->Model->getImportErrors();
            $this->set( 'import_errors', $import_errors );
            $this->Session->setFlash( __('Error Importing') . ' ' . $this->request->data['Model']['CsvFile']['name'] . ', ' . __('column name mismatch.')  );
            $this->redirect( array('action'=>'import') );
        }

        $new_records_count = $this->Model->find( 'count' ) - $records_count;
        $this->Session->setFlash(__('Successfully imported') . ' ' . $new_records_count .  ' records from ' . $this->request->data['Model']['CsvFile']['name'] );
        $this->redirect(array('plugin'=>'usermgmt', 'controller'=>'users', 'action'=>'dashboard'));
    }
}

Any help would be greatly appreciated as I have only found 1 post concerning this behavior when I searching...


Solution

I made my custom method to achieve the same task. Define the following method in app\Plugin\Utils\Model\Behavior

public function getCSVData(Model &$Model, $file, $fixed = array())
{
    $settings = array(
              'delimiter' => ',',
              'enclosure' => '"',
              'hasHeader' => true
            );
    $this->setup($Model, $settings);
    $handle = new SplFileObject($file, 'rb');       
    $header = $this->_getHeader($Model, $handle);
    $db = $Model->getDataSource();
    $db->begin($Model);
    $saved = array();
    $data = array();
    $i = 0;
    while (($row = $this->_getCSVLine($Model, $handle)) !== false)
    {
        foreach ($header as $k => $col)
        {
            // get the data field from Model.field              
            $col = str_replace('.', '-', trim($col));
            if (strpos($col, '.') !== false)
            {
                list($model,$field) = explode('.', $col);
                $data[$i][$model][$field] = (isset($row[$k])) ? $row[$k] : '';
            }
            else
            {
                $col = str_replace(' ','_', $col);
                $data[$i][$Model->alias][$col] = (isset($row[$k])) ? $row[$k] : '';
            }
        }
        $is_valid_row = false;

        foreach($data[$i][$Model->alias] as $col => $value )
        {
            if(!empty($data[$i][$Model->alias][$col]))
            {
                $is_valid_row = true;                   
            }
        }
        if($is_valid_row == true)
        {
            $i++;
            $data = Set::merge($data, $fixed);  
        }
        else
        {
            unset($data[$i]);
        }
    }
    return $data;
}   

And you can use it using:

  $csv_data = $this->Model->getCSVData($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);

Here $csv_data will contain an array of all of those records from the csv file which are not empty and with the fixed field in each record index.



Answered By - Arun Jain
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, April 22, 2022

[FIXED] Why ajax calls doesn't load the layout in CakePHP 2.3

 April 22, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

I am wondering why if i use jquery $.load function or some pluging such as fancybox to load content dynamically on the site, the layoug is not loaded but only the view as if it were an element.

My $.load() calls a controller action as if it was a normal link, like:

$('#demo').load("http://"+ document.domain +"/tables/users/edit/", {input : data}, function(dat){
     //whatever
});

This is not something I personally dislike, like this I avoid creating elements and calling them using $this->render('/Elements/xxxx', false); from my controllers.

I want to know if this is the proper way to work with or if it is some kind of cheat or bug of cakephp.

How should we treat this type of content which is not a proper "view" (as won't have a layout, headers...etc), but an "element" loaded dynamically? As a view? As an element?

Thanks.


Solution

Check /Layouts/ajax.ctp this is the layout that is rendered for ajax calls. Usually you don't want to have all the header and footer around the element you request when doing an ajax call.



Answered By - floriank
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to disable input tag in cakephp

 April 22, 2022     cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

I want to disable input tag ,my demo code is

echo $this->Form->input('AreaMaster.GroupID', array('type'=>'select', 'div'=>'control-group', 
                    'cascadeFrom'=>'AreaMasterAreaType012',  'label'=>array('text' => 'Area Group','class'=>'control-label required'),'tabindex'=>'4', 'autoBind' => false,
                        'options'=>$dataSource, 'disabled' => 'true'));

I applied 'disabled' => 'true' but it not work . So please suggest me appropriate solution..


Solution

You are using 'disabled' => 'true' (notice the single quotes you have around true. This may have been the reason. I am using CakePHP 2.3 and it's working fine however.

You can also use any of the following:

'disabled'
'disabled' => 'disabled'
'disabled' => true

So your code would look like one of these:

echo $this->Form->input('AreaMaster.GroupID', array('type'=>'select', 'div'=>'control-group', 
                    'cascadeFrom'=>'AreaMasterAreaType012',  'label'=>array('text' => 'Area Group','class'=>'control-label required'),'tabindex'=>'4', 'autoBind' => false,
                        'options'=>$dataSource, 'disabled'));

echo $this->Form->input('AreaMaster.GroupID', array('type'=>'select', 'div'=>'control-group', 
                    'cascadeFrom'=>'AreaMasterAreaType012',  'label'=>array('text' => 'Area Group','class'=>'control-label required'),'tabindex'=>'4', 'autoBind' => false,
                        'options'=>$dataSource, 'disabled' => 'disabled'));

echo $this->Form->input('AreaMaster.GroupID', array('type'=>'select', 'div'=>'control-group', 
                    'cascadeFrom'=>'AreaMasterAreaType012',  'label'=>array('text' => 'Area Group','class'=>'control-label required'),'tabindex'=>'4', 'autoBind' => false,
                        'options'=>$dataSource, 'disabled' => true));                                                    


Answered By - bigmike7801
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to select different layout in Cake PHP Email template

 April 22, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3, php     No comments   

Issue

This is below email code working file

$email = new CakeEmail('smtp');
$fromConfig = EMAIL_FROM_ADDRESS;
$fromNameConfig = EMAIL_FROM_NAME;
$email->from(array( $fromConfig => $fromNameConfig));
$email->sender(array( $fromConfig => $fromNameConfig));
$email->to($this->data['Subscribe']['email']);
$email->subject('Newsletter Confirmation');
$email->template('subscribe');
$email->emailFormat('html');

my above code taking default.ctp layout file by default(app\View\Layouts\Emails\html\default.ctp), it's fine.

But my question is i have created a other fancy.ctp layout file(app\View\Layouts\Emails\html\fancy.ctp) not use default.ctp layout file

How to use this fancy.ctp layout file in Email


Solution

I have solved issue myself......... Below my working code

$email = new CakeEmail('smtp');
$fromConfig = EMAIL_FROM_ADDRESS;
$fromNameConfig = EMAIL_FROM_NAME;
$email->from(array( $fromConfig => $fromNameConfig));
$email->sender(array( $fromConfig => $fromNameConfig));
$email->to($this->data['Subscribe']['email']);
$email->subject('Newsletter Confirmation');
$email->template('subscribe','fancy');
$email->emailFormat('html');


Answered By - thecodedeveloper.com
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] What does $this->Model1->Model2 accomplish in CakePHP HABTM?

 April 22, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

Basically, I've implemented the HABTM successfully in CakePHP, but the trouble is, I don't understand why it works.

The thing I hate about the CakePHP cookbook is that is tells you what to do but make very little effort to explain the underlying segments of their code.

Essentially, my data model is like this.

Task HABTM Question

I don't understand this code fragment.

$this->set('questions', $this->Task->Question->find('list'))

In particular, what is $this->Task->Question supposed to accomplish?

Also how is the above code link to this code fragment in the view?

echo $this->Form->input('Question'); 

One thing that is very peculiar is that with the above code fragment, I get a multiple select option.

However, if I change the code to this,

echo $this->Form->input('question');

I get a single select drop down list.

I scoured the entire documentation and still cannot find a satisfactory explanation to my doubts.

Would really appreciate if anyone can clarify this issue for me.


Solution

1. Model chaining

When a model has an association to another model (like in your example an HABTM one) then you can call methods of the associated model by chaining it to the current model. This is explained early in Associations and an example of exactly how it works is given at the end of the first section.

When you are someplace in your TasksController normally you would expect that only your Task model would be available. Instead any association described in the Task model is chained to that model in the form of $this->Model1->Model2.

So $this->set('questions', $this->Task->Question->find('list')) means:

From current model Task that you know about, access the associated model Question and then call its find('list') method. Then $this->set the results to the view as variable questions.

2. FormHelper Conventions

When you use a CamelCased single name for field input, like in $this->Form->input('Question'); you are saying to FormHelper that the data contained in the questions variable come from a model named Question with a HABTM association, therefore they should be handled as a multiple select (as HABTM points to such an association).

With a field name of model_id, like in this example question_id, you're asking for a single select (select a single id of the connected model).

With anything else, FormHelper looks at the field definition and takes the decision itself, but of course your can override any default behavior you want using options.

This is explained in detail and I'm surprised you missed both. CakePHP has one of the best documentations available, almost everything you need is there.



Answered By - user221931
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] how to display max and min number from database in cakephp

 April 22, 2022     cakephp, cakephp-1.3, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

how to display max and min number in cakephp controller part .

$rs = $this->Item->find('all' , array ('fields' => array('Max(Item.no) as no')));

I want max number of Item table .

I need to add some rand number to that and save it in new row .


Solution

Use below query

$rs = $this->Item->find('all' , array ('fields' => array('Max(Item.no) as no')));

This query will return max number in below format

Array
(
    [0] => Array
        (
            [no] => 27
        )

)

$max = $rs[0]['no'];


Answered By - shimar
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] Why used App::uses('Controller', 'Controller') before all controllers

 April 22, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

Sorry to Ask but I can't understand why following line written before every controller.

App::uses('Controller', 'Controller');

If we write following code with extends controller that means all the property and function of Controller class we can access in AppController.

class AppController extends Controller{...}

So, My question is If I do not write App::uses('Controller', 'Controller'); that have any effect in my code or perfomance?


Solution

App::uses is a static method to register the location of a file (that corresponds with a class) to load for the PHP autoloader to be able to load it on-the-fly.

That App::uses('AppController', 'Controller'); line declares that if the AppController class is referenced (or extended, in this case) and the AppController class isn't yet defined, that it should look for the file within the app/Controller directory and construct the actual filename like so: AppController.php Part of that is cakePHP convention, part PHP5.

The reason nothing breaks when you remove this line is because that file is probably required somewhere else in the application (another controller, during bootstrap-- so php already has the AppController class in memory.

The reason it's included on the first line is because the controller you baked extends the AppController class. It has a hard dependency on the AppController class being loaded and perhaps in the cake shell or unit testing environments the app controller isn't automatically loaded before hand so the inclusion must occur. App::uses is like telling cake (and PHP) that 'hey, if you're looking for the AppController class and you don't already have it, you should look in app/Controller directory and just add .php on the end!' If the file has already been loaded, App::uses would have no effect.

See What is the purpose of App::uses('AppController', 'Controller') in each controller - cakephp



Answered By - Supravat Mondal
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, April 21, 2022

[FIXED] how to get fetch value in cakephp

 April 21, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

        $map=$this->Sessiondetails->find("all");    
        $this->set("map",$map);
    foreach($map as $maps){ 
   echo $maps['Sessiondetails']['latitude'];
    }

I want to fetch only 3rd row values . How to do it in cakephp. I am using cakephp 2x.


Solution

How would you do it in SQL? Think about it and then use cake syntax

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#retrieving-your-data

$map = $this->Sessiondetails->find(
    "all"
    array(
        'offset' => 2
        'limit' => 1
    )
);   

of course if you want to run a query that retrieves all the records but then just show the 3rd record you can simply do

echo $map[2]['Sessiondetails']['latitude'] 


Answered By - arilia
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to write update query in cakephp

 April 21, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

$test="test's";   
$contact='1234567890';
 $this->Page->updateAll(
        array('Page.order' => 0,"Page.name" => "'$test'","Page.contact" => "'$contact'"),
        array('Page.type' => 'PROMOTED')
    );

Above query having single quote conflict. Is there any other way to write update query . I am using cakephp 2x


Solution

NOTE: Must backslash characters that have another meaning in php if you want them to be recognized as string caharacters. e.g(test's needs be test\'s)

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-mixed-conditions

try:

$db = $this->getDataSource();
$test = $db->value('test\'s');
$contact = $db->value('1234567890');
$this->Page->updateAll(
        array('Page.order' => 0,"Page.name" => $test,"Page.contact" =>     $contact),
    array('Page.type' => 'PROMOTED')
);


Answered By - Jason Joslin
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to delete a sub record based on criteria in Cakephp?

 April 21, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

I have 2 tables "Topic" and "Post" linked in the model by hasMany

I want to delete the Post entry with id = 3 since it doesn't have a message.

In the model for table "Post", I have this "beforeSave" :

public function beforeSave($options = array()) {

  if ($this->data['Post']['message'] == '') {
    CODE TO DELETE HERE
  }
}

This is my $this->request->data :

Array
(
[Topic] => Array
(
    [id] => 1
    [topic_title] => This is my topic
)

[Post] => Array
(
    [1] => Array
        (
            [id] => 1
            [title] => Blah
            [message] => My message
        )

    [2] => Array
        (
            [id] => 2
            [title] => Second Blah
            [message] => Second My message
        )
    [3] => Array
        (
            [id] => 3
            [title] => Second Blah
            [message] => 
        )
    )
)

I can't figure out how to delete in the model or is this the wrong approach ?

I've now also tried this in the controller before saveAssociated :

$this->loadmodel('Post');

foreach ($this->request->data['Post'] as $i => $post) {

 if ($post['message'] == '') {
     unset($this->request->data['Post'][$i]);

     $options = array( 'conditions' => array( 'Post.id' => $i) );
     $this->Post->find('first', $options);

     if ($this->Post->delete()) {
      echo "Post id : " . $i . ' - Deleted';
    } else {
      echo "Post id : " . $i . ' - Not deleted';
    }
 }

}

This gives me "Post id xxx Deleted" however the Post record isn't deleted.


Solution

Since I'm using "saveAssociated" I also have to "unset" the post in "$this->request->data" otherwise it will be saved again.

$this->loadmodel('Post');

foreach ($this->request->data['Post'] as $i => $post) {

 if ($post['message'] == '') {
   unset($this->request->data['Post'][$i]);

 if ($this->Post->delete($post['id'], false)) {
  echo "Post id : " . $i . ' - Deleted';
 } else {
  echo "Post id : " . $i . ' - Not deleted';
 }
}


Answered By - flaggalagga
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] What is the best way to Update/Add/Delete multiple records in the same form with CakePHP

 April 21, 2022     cakephp, cakephp-2.0, cakephp-2.1, cakephp-2.3     No comments   

Issue

I have this Topic table who is linked to the table Post with hasMany in the model.

This is my $this->request->data

Array
(
[Topic] => Array
    (
        [id] => 1
        [topic_title] => This is my topic
    )

[Post] => Array
    (
        [1] => Array
            (
                [id] => 1
                [title] => Blah
                [message] => My message
            )

        [2] => Array
            (
                [id] => 2
                [title] => Second Blah
                [message] => Second My message
            )

    )

)

And I update it with :

$this->Topic->saveAssociated($this->request->data);

But What if I also wanted to have the ability to add and delete posts to this topic, how would I proceed ?


Solution

Keeping the same structure, you could do a $this->Topic->saveAssociated($this->request->data); and it will add any new ('id' => NULL or unset) items in the data array.

About the delete, the only case I know would delete at the same time, would be a HABTM when it's marked as 'unique' => true. Otherwise, you need to do a $this->Post->deleteAll(array('Post.topic_id' => $unwanted_topic_id), false);

I could think of making a new array keeping the ones you want deleted and sending them as condition for the deleteAll function.



Answered By - SrQ
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, March 17, 2022

[FIXED] How do i get the cakephp server command for version 2.1.2

 March 17, 2022     cakephp, cakephp-2.1, cakephp-3.x     No comments   

Issue

My present cake project is in version 2.1.2. I want to have a server console command like version 3.x provides. How do I get this working?


Solution

A cli is not needed

cd /path/to/your/app/webroot/
php -S localhost:8000

Is the equivalent of all the 3.x server command does.

I want a cli anyway

Well, the cli is very simple. So all you'd need is to create a command that does the same thing, in principle:

// app/Console/Command/ServerShell.php
<?php
App::uses('AppShell', 'Console/Command');

class ServerShell extends AppShell
{

    public function main()
    {
        $command = sprintf(
            "php -S %s:%d -t %s %s",
            'localhost',
            8080,
            escapeshellarg(WWW_ROOT),
            escapeshellarg(WWW_ROOT . '/index.php')
        );
        system($command);        
    }
}

Note that this only works with version 5.4+ of php as that's when the inbuilt webserver was introduced.



Answered By - AD7six
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, February 17, 2022

[FIXED] Get the digit of last page in Cakephp

 February 17, 2022     cakephp, cakephp-2.0, cakephp-2.1     No comments   

Issue

How do I get just the last page number, not the link that comes with $this->Paginator->last(), but the actual digit itself?


Solution

This link will help you to achieve the same.

<?php echo $this->Paginator->counter('{:pages}');
     //counter() method with all options
     //echo $this->Paginator->counter('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}');?>

You can call the counter() method into your view to get the last page number.



Answered By - Arun Jain
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, January 20, 2022

[FIXED] SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293

 January 20, 2022     cakephp, cakephp-2.0, cakephp-2.1     No comments   

Issue

I am working on a CakePHP 2 project. It originally started out in 2.0.x and then recently migrated to 2.1.0. Throughout the whole development process, I have been receiving the error message below.

It pops up at the top of the page unpredictably. It can be when I am just viewing different pages, or even after I add a record to the database (yet the record properly saves).

Warning:
SplFileInfo::openFile(/var/www/cake_prj/app/tmp/cache/persistent/cake_core_cake_console_): 
failed to open stream: 
Permission denied in 
     /var/www/cake_prj/lib/Cake/Cache/Engine/FileEngine.php on line 293

I recursively set the owner and group of the tmp folder to apache, and still received the message. In addition, I then recursively set the permissions to read, write, and execute for all (chmod 777). The error message still pops up.

Even after changing both the owner, group, and permissions, the file in question:

cake_prj/app/tmp/cache/persistent/cake_core_cake_console_

will have its owner and group set back to root, and its permissions set back to default.

What could be causing this problem? Is there a way to ensure that every time this file is generated, that it will always have be apache:apache with read/write/execute permissions?


Solution

There was a bug report there http://cakephp.lighthouseapp.com/projects/42648/tickets/2172 but it was considered as not being a bug.

What I personaly noticed is that some file owner may be modified when you use the cake script in the console (for instance to make a bake). The modified files then belong to the user you use in the console.

Would this mean you call cake while being root ? Or do you have any root cron job that calls a Cake shell script ?

Personaly I have now the habit to chmod the whole tmp folder content back to the apache user after having used the cake script and it seems to prevent the warning to appear.



Answered By - nIcO
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, January 1, 2022

[FIXED] How do I implement JavaScript onmouseover in CakePHP?

 January 01, 2022     cakephp, cakephp-2.1, dom-events, javascript, php     No comments   

Issue

What I want to do is convert the following to work with CakePHP:

<a href="URL ADDRESS"><img src="URL OF THE FIRST IMAGE GOES HERE" onmouseover="this.src='URL OF THE SECOND IMAGE GOES HERE'" onmouseout="this.src='URL OF THE FIRST IMAGE GOES HERE'" /></a>

I have the following so far:

<?php echo $this->Html->image("animalhealth.png", array(
    "alt" => "Animal Health",
    "onmouseover" => "this.src='animalhealthhover.png'",
    "onmouseout" => "this.src='animalhealth.png'",
    'url' => array('controller' => 'records', 'action' => 'index'
    )
)); ?>

The problem are the onmouseover & onmouseout event lines. I need to tell cake to somehow use the helper method otherwise it just selects no image. I don't want to have to put the entire address in as the is a navigation menu and there will be multiple instances of the app at different locations.


Solution

I have managed to build a work around using CSS.

Made a custom image for the button Icon.

This is what goes in the view/layout page:

<ul class="menu">
<li>
                        <div id="button_name"><?php
                    echo $this->Html->image("name_of_img.png", array(
                        "alt" => "Animal Health",
                        'url' => array('controller' => 'controllerName', 'action' => 'index')));
                    ?>
<?php echo $this->Html->link(__('Link Text'), array('controller' => 'controllerName', 'action' => 'index')); ?> 
                        </div>
                    </li> 
</ul>

If your not using cakePHP you could do it like this in a normal HTML page:

<ul class="menu">
<li>
                        <div id="button_name"><a href="/path/to/page.html"><img src="/path/to/img/imagename.png" alt="Animal Health" /></a><a href="/path/to/page.html">Animal Health</a> 
                        </div>
                    </li>
<ul>

This Makes both the text and the icon clickable.

Then the CSS:

.menu li {padding:0px; border: 0px; margin: 0px; list-style: none;
          float: left; margin-left: 0px; display: block; height: 36px;} //remove any stlying and set up for the menu.

#button_name{background-color: darkorange;
               float: left;
              margin-right: 5px;
               margin-top: 1px;
               margin-bottom: 0px;
               padding: 1px 3px 1px 3px;
               -webkit-border-radius: 5px 5px 0px 0px;
               border-radius: 5px 5px 0px 0px;
               border: 1px black;
               text-align: right;
               color: #6495ED;
               font-weight: bold;
               -webkit-transition: all 1000ms;
               -moz-transition: all 1000ms;
               -o-transition: all 1000ms;
               -ms-transition: all 1000ms;
               transition: all 1000ms;}
#button_name a {
    -webkit-transition: all 1000ms;
    -moz-transition: all 1000ms;
    -o-transition: all 1000ms;
    -ms-transition: all 1000ms;
    transition: all 1000ms;
    font-weight: bold;
    color: #6495ED; 
}
    #button_name:hover
 {background-color: #6495ED;}
    #button_name:hover a // VERY IMPORTANT see note
{
        font-weight: bold;
        color: darkorange;}

This makes a nice button with 2 rounded top corners. When you hover over the button anywhere the background and text colours transition between each other. ie: text goes from blue to orange and background goes from orange to blue.

Note about #button_name:hover a: You must set it as specified if you set it as "button_name a:hover" the text will stay the same color as the background.

Hopefully this will help someone else.

Still keen to hear any ideas about doing it with the JS.



Answered By - agit
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing