Jump to content

Archived

This topic is now archived and is closed to further replies.

sonoton345

menu highlight to show current page in CakePHP

Recommended Posts

I have a menu in cakePHP created using css and would like to change the color of the text when on a corresponding page. The menu is in  default.ctp in the layout folder.

Can anyone advice please? Thank you in advance.

Share this post


Link to post
Share on other sites

So if I understand you correctly..

 

I will assume you have a 'home' page and a 'about us' page. When your on the 'about us' page you want it to highlight the 'about us' link, and if on the 'home' page, you want it to highlight 'home' somehow?

Share this post


Link to post
Share on other sites

You can pass variables to the layout from the controller. 

 

So, in your about controller, you can have something like...

 


function about() {

   // controller logic
   ...
   $this->set('page_for_layout', 'about');
   ... 

}

 

Then, depending on how your menu and css is set up, you can have something like this in your default.ctp

 


<ul class="<?php echo $page_for_layout; ?>">
  <li><a href='#'>Home</a></li>
  <li><a href='#'>About</a></li>
</ul>

 

In your css, you would have something like

 


.home a { //some style }
.about a { //some style }

 

There are probably other ways, but I like this one.

Share this post


Link to post
Share on other sites

I'm Ehutchison, and I approve this post.

You can pass variables to the layout from the controller. 

 

So, in your about controller, you can have something like...

 


function about() {

   // controller logic
   ...
   $this->set('page_for_layout', 'about');
   ... 

}

 

Then, depending on how your menu and css is set up, you can have something like this in your default.ctp

 


<ul class="<?php echo $page_for_layout; ?>">
  <li><a href='#'>Home</a></li>
  <li><a href='#'>About</a></li>
</ul>

 

In your css, you would have something like

 


.home a { //some style }
.about a { //some style }

 

There are probably other ways, but I like this one.

Share this post


Link to post
Share on other sites

Thanks for this...but in my own case the 4 of the corresponding pages to the navigations are static pages. They are all in the pages folder so I didn't have to create a controller. How do I go with this?

 

One other thing...Any idea why $form->month('mob') does not send the value of the month e.g January but sends an array to the database? I have a database error that shows the value being sent is Array

Share this post


Link to post
Share on other sites

Thank you for the menu tip, I'll look into that....

excerpts from the form code is this on the view side - registration.ctp

<p><label>Title:<span class="required">*</span></label>
	<?php 
	   $options=array('Mr'=>'Mr','Mrs'=>'Mrs','Ms'=>'Ms');
	   e($form->select('title', $options,array('id'=>'tx'))); ?></p>
<p><label>First Name:<span class="required">*</span></label><?php e($form->text('first_name', array('id'=>'txt'))); ?></p>
        <p><label>Last Name:<span class="required">*</span></label><?php e($form->text('last_name', array('id'=>'txt'))); ?></p>
        <p><label>Date Of Birth:<span class="required">*</span></label><?php e($form->month('mob',01)); e($form->day('dayborn',1));
e($form->year('yearborn',date('Y')-18, date('Y')-62,date('Y')-18));
<p><label>Address:<span class="required">*</span></label><?php e($form->text('address', array('id'=>'txt'))); ?></p>
        <p><label>Address 2:</label><?php e($form->text('address2', array('id'=>'txt'))); ?></p>
        <p><label>City:<span class="required">*</span></label><?php e($form->text('city', array('id'=>'txt'))); ?></p>
        <p><label>Zip Code:<span class="required">*</span></label><?php e($form->text('zip_code', array('id'=>'txt'))); ?></p>
        <p><label>Phone:<span class="required">*</span></label><?php e($form->text('phone', array('id'=>'txt'))); ?></p>
        
        
         <p><?php e($form->submit('Register',array('div'=>false,'class' =>'submitbutton'))) ?>
     <?php e($form->end()); ?>

in my controller, I have this...

function register()
{

	if (!empty($this->data)) {
	   $this->Jobseeker->create();
	   if ($this->Jobseeker->save($this->data)) {
		   $this->Session->setFlash('Congratulations! You have signed up!');
		   $this->redirect(array('controller'=>'employers','action'=>'index'));
	   } else {
		   $this->Session->setFlash('There was an error signing up. Please try again.');

	   }
	}
	// set page title
	$this->pageTitle = 'iWeev - Registration';
	// set layout file
	$this->layout = 'mytemp2';

}

This is the sql error SQL Error: 1054: Unknown column 'Array' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 525]

Query: INSERT INTO `jobseekers` (`status`, `username`, `password`, `title`, `first_name`, `last_name`, `mob`, `dayborn`, `yearborn`, `address`, `address2`, `city`, `zip_code`, `phone`, `modified`, `created`) VALUES ('0', 'olu@ymail.com', 'bad55116b77f7d180d4ba6b0c9732f2fdca73bab', 'Mr', 'Olu', 'Olus', Array, Array, Array, '1234 te', '', 'testing', 233444, '1233333333', '2009-06-14 23:14:14', '2009-06-14 23:14:14')

Instead of sending the values of date,month and year, it's sending Array, Array, Array

Share this post


Link to post
Share on other sites

First, you should run debug on $this->data. The problem appears to be that you are passing extra parameters to the form helper that is doesn't understand.

 

e($form->month('mob',01)); e($form->day('dayborn',1));

e($form->year('yearborn',date('Y')-18, date('Y')-62,date('Y')-18));

 

 

I don't know what 01 and 1 are doing in the month/day fields, and in year your should be passing min/max, you are sending 3 parameters. 

 

http://book.cakephp.org/view/204/Form-Element-Specific-Methods

Share this post


Link to post
Share on other sites

Thanks, when I wrote those form elements I actually followed the documentation you wrote in your response.

the 01 and 1 in month/day are selected values by default, so by default it selects January for month and 1 for date.

In the year my min is [current year] -18 and max is [current year] - 62 ,which means the lowest age will always be 18 by year and highest age will always be 62 by year. The third parameter here also is the selected value by default.

Share this post


Link to post
Share on other sites

I see, but selected should be a string so make sure you add some quotes around those values.  Did you run debug on $this->data ?

Share this post


Link to post
Share on other sites

from the cakephp doc it says

debug($var, $showHTML = false, $showFrom = true)

 

So it should be this?

debug($this->data)

Share this post


Link to post
Share on other sites

correct, but be sure you have the debug level properly set. should be in app/config/core.php

 

Configure::write('debug', 1);

 

Using debug is similar to doing a print_r(); with <pre></pre> tags

 

Share this post


Link to post
Share on other sites

Thanks once again. I removed all the extra parameters and this is what I got from using debug...

Array
(
    [Jobseeker] => Array
        (
            [username] => tester@ymail.com
            [password] => testers
            [password2] => testers
            [title] => Mr
            [first_name] => Tester1
            [last_name] => Tester2
            [mob] => Array
                (
                    [month] => 01
                )

            [dayborn] => Array
                (
                    [day] => 01
                )

            [yearborn] => Array
                (
                    [year] => 1991
                )

            [address] => 1234 rt
            [address2] => 
            [city] => dfgg
            [zip_code] => 56788
            [phone] => 2345566
        )

)

This is the part of my form now that is returning Array...

<p><label>Date Of Birth:<span class="required">*</span></label><?php e($form->month('mob')); e($form->day('dayborn')); 
e($form->year('yearborn',date('Y')-18, date('Y')-62)); ?></p>

Share this post


Link to post
Share on other sites

Cake is sending the correct data.  I'm not sure about how the model automagically handles the arrays when using the month/year helpers.  Did you recently change anything with your database?  Make sure you always clear the cache if you do.

 

Would it not be better to have 1 field for birthday set as  DATE type.  If you did that you could just have

 

$form->input('dob'])

 

The form helper will automatically create the drop down selects and handle the data array correctly. Seems to me better practice to use the correct field type for the data.

 

 

Share this post


Link to post
Share on other sites

Thanks but I want to do is limit the age to between 18 and 62. if I set one input field would I be able to set a minYear and maxYear?

Share this post


Link to post
Share on other sites

Thanks I have read this page and still not getting the right result. For now I'm just using text fields which is working quite fine but i'll prefer to have the form already populated.

Thanks for all your help.

Share this post


Link to post
Share on other sites

If you change your database schema to a single date field you can easily set the min/max year with the automagic field types.  If you really want to keep these three fields separate, you can try to grab the data from the array and then do your insert.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.