tag:blogger.com,1999:blog-18927801012392992152024-03-18T02:49:15.883-05:00PHP, MySQL, Drupal, .htaccess, Robots.txt, PhponwebsitesPHP, MySQL, Drupal, .htaccess, Robots.txt at phponwebsitesGuruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.comBlogger110125tag:blogger.com,1999:blog-1892780101239299215.post-58560904180526415242017-03-01T13:18:00.000-06:002017-03-01T13:20:59.899-06:00Update multiple fields using #ajax in Drupal 7 form<div dir="ltr" style="text-align: left;" trbidi="on">
We know how to replace a field using #ajax in a form. Can we update multiple fields using #ajax in a form? Yes, we can update multiple fields using #ajax in the Drupal 7. We can achieve it using ajax_command_replace() in Drupal 7. For more details about ajax commands on Drupal 7, please visit <a href="https://api.drupal.org/api/drupal/7.x/search/ajax_command.">https://api.drupal.org/api/drupal/7.x/search/ajax_command.</a><br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Update fields using #ajax in a Drupal form:</span></h3>
<div>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;"> </span>Consider the following example. In this example, I've created a form with fields First, Second & Third name. I tried to update the Second & Third name fields when focus out on the First name field.</div>
<br />
<div class="bdr">
<div>
<div>
/**</div>
<div>
* Implments hook_form()</div>
<div>
*/</div>
<div>
function phponwebsites_ajax_form($form, &$form_state) {</div>
<div>
$form['firstname'] = array(</div>
<div>
'#title' => t('First name'),</div>
<div>
'#type' => 'textfield',</div>
<div>
'#ajax' => array(</div>
<div>
'callback' => '_generate_textfield',</div>
<div>
'wrapper' => 'copied-text-field',</div>
<div>
)</div>
<div>
);</div>
<div>
<br /></div>
<div>
$form['secondname'] = array(</div>
<div>
'#title' => t('Second name'),</div>
<div>
'#type' => 'textfield',</div>
<div>
'#prefix' => '<div id="copied-secondname">',</div>
<div>
'#suffix' => '</div>',</div>
<div>
);</div>
<div>
<br /></div>
<div>
$form['thirdname'] = array(</div>
<div>
'#title' => t('Third name'),</div>
<div>
'#type' => 'textfield',</div>
<div>
'#prefix' => '<div id="copied-thirdname">',</div>
<div>
'#suffix' => '</div>',</div>
<div>
);</div>
<div>
<br /></div>
<div>
$form['submit'] = array(</div>
<div>
'#type' => 'submit',</div>
<div>
'#value' => 'Submit'</div>
<div>
);</div>
<div>
<br /></div>
<div>
return $form;</div>
<div>
}</div>
<div>
<br /></div>
<div>
function _generate_textfield($form, &$form_state) {</div>
<div>
if (!empty($form_state['values']['firstname'])) {</div>
<div>
$form['secondname']['#value'] = $form_state['values']['firstname'];</div>
<div>
$form['thirdname']['#value'] = $form_state['values']['firstname'];</div>
<div>
}</div>
<div>
$commands = array();</div>
<div>
$commands[] = ajax_command_replace('#copied-secondname', drupal_render($form['secondname']));</div>
<div>
$commands[] = ajax_command_replace('#copied-thirdname', drupal_render($form['thirdname']));</div>
<div>
return array('#type' => 'ajax', '#commands' => $commands);</div>
<div>
}</div>
</div>
<div>
</div>
</div>
<div>
<br /></div>
<div>
When you tried to execute the above codes, it'll populate the same name on the other 2 fields. It looks likes the below image:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-FKAtY9B-RKg/WLcddAbGs1I/AAAAAAAABYE/LtX9n-szpYge7wXW4v_cwDzqXuqK_HsQwCLcB/s1600/Drupal-Form-Ajax-update-multiple-fields-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Update multiple form fields using #ajax in Drupal 7" border="0" src="https://4.bp.blogspot.com/-FKAtY9B-RKg/WLcddAbGs1I/AAAAAAAABYE/LtX9n-szpYge7wXW4v_cwDzqXuqK_HsQwCLcB/s1600/Drupal-Form-Ajax-update-multiple-fields-phponwebsites.png" title="Update multiple form fields using #ajax in Drupal 7" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Now I hope you know how to populate multiple fields using #ajax in Drupal 7 forms.</div>
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2016/07/drupal-7-pathauto-remove-special-characters-url-alias.html" itemprop="relatedLink">Remove speical characters from URL alias using pathauto module in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/10/drupal-7-disable-future-dates-in-date-popup.html" itemprop="relatedLink"><b>Disable future dates in date pop-up calendar Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com2tag:blogger.com,1999:blog-1892780101239299215.post-27499243727313559792016-10-21T13:48:00.002-05:002017-03-01T13:20:22.365-06:00Add date pop-up calendar in custom drupal 7 form<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes how to add date pop-up calender to a custom form in the Drupal 7.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-q7PbmX7BXA8/WApg6WFSyhI/AAAAAAAAA7A/_zWGEgy2fDUcjWBhgSo3-mP1qz_6zbQkACLcB/s1600/use-date-pop-up-calendar-custom-form-drupal-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Use date pop-up calendar in custom form - drupal 7" border="0" src="https://2.bp.blogspot.com/-q7PbmX7BXA8/WApg6WFSyhI/AAAAAAAAA7A/_zWGEgy2fDUcjWBhgSo3-mP1qz_6zbQkACLcB/s1600/use-date-pop-up-calendar-custom-form-drupal-phponwebsites.png" title="Use date pop-up calendar in custom form - drupal 7" /></a></div>
<br />
<br />
The use case is if you want to use date pop-up calendar in a custom form, then how you can do it in the drupal 7. Actually, the drupal 7 <a href="https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x/">form API</a> provides lots of form types like textfield, checkbox, checkboxes etc to create a custom form. Similarly, the <a href="https://www.drupal.org/project/date">date module</a> also provides the form type called date_popup. We can use it in the custom form in order to display the date pop-up in the custom form.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Use date pop-up calendar with the custom form in drupal 7:</span></h3>
<br />
Let consider the below code snippet:<br />
<br />
<div class="bdr">
<br />
function phponwebsites_menu() {<br />
$items = array();<br />
<br />
$items['customform'] = array(<br />
'title' => t('Custom Form'),<br />
'type' => MENU_CALLBACK,<br />
'page callback' => 'drupal_get_form',<br />
'page arguments' => array('phponwebsites_display_date_popup_form'),<br />
'access callback' => TRUE,<br />
);<br />
<br />
return $items;<br />
}<br />
<br />
function phponwebsites_display_date_popup_form($form, &$form_state) {<br />
$form['date'] = array(<br />
'#type' => 'date_popup',<br />
'#default_value' => date('Y-m-d'),<br />
'#date_format' => 'Y-m-d',<br />
'#date_year_range' => '0:+5',<br />
'#datepicker_options' => array('minDate' => 0, 'maxDate' => 0),<br />
);<br />
<br />
return $form;<br />
}</div>
<br />
Where,<br />
'#date_format' => 'Y-m-d' if you need to display only date<br />
'#date_format' => 'Y-m-d H:i:s' if you need to display date & time<br />
'#date_year_range' => '0:+5' if you need to display only future 5 years<br />
'#datepicker_options' => array('minDate' => 0, 'maxDate' => 0) if you want to display only current date. We can hide the future & past dates using this option.<br />
<br />
Please add the above code into your module file and look into the "customform" page. It looks like the below image:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-zY9lHbutU6g/WAphfk-fOwI/AAAAAAAAA7E/idBJqd7NAdE3YXmH7EDMWY5fkXJ2GC8SACLcB/s1600/use-date-pop-up-custom-form.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Display only current date in date -pop-up - drupal 7" border="0" src="https://2.bp.blogspot.com/-zY9lHbutU6g/WAphfk-fOwI/AAAAAAAAA7E/idBJqd7NAdE3YXmH7EDMWY5fkXJ2GC8SACLcB/s1600/use-date-pop-up-custom-form.png" title="Display only current date in date -pop-up - drupal 7" /></a></div>
<br />
<br />
Now I've hope you know how to add date pop-up calendar with custom form in the drupal 7.
<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2016/07/drupal-7-pathauto-remove-special-characters-url-alias.html" itemprop="relatedLink">Remove speical characters from URL alias using pathauto module in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/10/drupal-7-disable-future-dates-in-date-popup.html" itemprop="relatedLink"><b>Disable future dates in date pop-up calendar Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2017/03/drupal-update-multiple-fields-using-ajax-callback-form.html" itemprop="relatedLink"><b>Update multiple fields using #ajax in Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com16tag:blogger.com,1999:blog-1892780101239299215.post-22764738548775382372016-10-07T13:15:00.002-05:002017-03-01T13:21:41.008-06:00Disable future dates in date popup - Drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes how to disable future dates in the Drupal 7. One of the features in the <a href="https://www.drupal.org/project/date">date</a> module is displayed the date in the pop-up.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-IrbEKctgLsw/V_fkDXe1hhI/AAAAAAAAA58/m-lKcI10B080I6ECD1u9Alkn4Z8Hqs9MwCLcB/s1600/drupal-disable-future-dates-date-popup-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Disable future dates in the date pop up - drupal 7" border="0" height="320" src="https://3.bp.blogspot.com/-IrbEKctgLsw/V_fkDXe1hhI/AAAAAAAAA58/m-lKcI10B080I6ECD1u9Alkn4Z8Hqs9MwCLcB/s320/drupal-disable-future-dates-date-popup-phponwebsites.png" title="Disable future dates in the date pop up - drupal 7" width="266" /></a></div>
<br />
<br />
The use case is if you want to display only past & current date rather than all the dates in the pop-up, then how to do it in Drupal 7. Actually, the date module provides API called hook_date_popup_process_alter to alter the date_popup widget elements.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Example for disabling future dates in Drupal 7:</span></h3>
<br />
For instance, I am going to disable future dates in the article content type. Please consider the following code snippet.<br />
<br />
<div class="bdr">
/**<br />
* Implement hook_date_popup_process_alter().<br />
*/<br />
function phponwebsites_date_popup_process_alter(&$element, &$form_state, &$context) {<br />
<br />
if ($form_state['complete form']['#form_id'] == 'article_node_form' && $element['#field']['field_name'] == 'field_date') {<br />
$max = 0;<br />
}<br />
<br />
if (isset($element['#datepicker_options']['maxDate'])) {<br />
$max = $element['#datepicker_options']['maxDate'];<br />
}<br />
<br />
if (isset($max)) {<br />
$element['#datepicker_options'] = array(<br />
'maxDate' => "+$max D",<br />
);<br />
}<br />
$element['date'] = date_popup_process_date_part($element);<br />
}</div>
<br />
I've disabled the dates only if the form is article & the field name is field_date. After added the above code to your module, you could see disabled future dates in the date pop up. It looks like the below image:<br />
<br />
<span id="goog_1407751232"></span><span id="goog_1407751233"></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-9Khwkpv5y-g/V_fl9s9CAII/AAAAAAAAA6Y/xoUhhcZUwb8-6lfv1lSiplYerLIDDqxuACLcB/s1600/drupal-date-popup-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Disable future dates in the date pop-up in drupal 7" border="0" height="320" src="https://4.bp.blogspot.com/-9Khwkpv5y-g/V_fl9s9CAII/AAAAAAAAA6Y/xoUhhcZUwb8-6lfv1lSiplYerLIDDqxuACLcB/s320/drupal-date-popup-phponwebsites.png" title="Disable future dates in the date pop-up in drupal 7" width="268" /></a></div>
<br />
<br />
Now I've hope you know how to disable the future dates at the date module in Drupal 7.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2016/07/drupal-7-pathauto-remove-special-characters-url-alias.html" itemprop="relatedLink">Remove speical characters from URL alias using pathauto module in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2017/03/drupal-update-multiple-fields-using-ajax-callback-form.html" itemprop="relatedLink"><b>Update multiple fields using #ajax in Drupal 7</b></a>
</div>
<br /></div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com3tag:blogger.com,1999:blog-1892780101239299215.post-29614128402945430322016-08-02T09:38:00.003-05:002017-03-01T13:21:48.773-06:00Multiple URL alias for a node in pathauto - drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
As we discussed in my <a href="http://www.phponwebsites.com/2016/07/drupal-7-pathauto-remove-special-characters-url-alias.html">previous post</a>, clean URL is one of the option to improve SEO. We've module called pathauto to clean URLs in drupal 7. It can allow us to set alias for content types, files, users & taxonomies. But we can set only one URL alias for a content type in drupal 7. You can set URL alias for a content type at admin/config/search/path/patterns. It looks like below image:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-6O6uGaVsC4Q/V5po0Sp9N1I/AAAAAAAAAwE/p1A8uwifsXcCct6QBazcw5SMdkF3el8jQCLcB/s1600/drupal-7-pathauto-set-url-alias.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Pathauto module patterns in drupal 7" border="0" height="155" src="https://2.bp.blogspot.com/-6O6uGaVsC4Q/V5po0Sp9N1I/AAAAAAAAAwE/p1A8uwifsXcCct6QBazcw5SMdkF3el8jQCLcB/s320/drupal-7-pathauto-set-url-alias.png" title="Pathauto module patterns in drupal 7" width="320" /></a></div>
<br />
<br />
Suppose you need two path for a content. For instance, the URL alias for a article need to node title and also article/node-title. Is it possible to set multiple path alias for a content type in drupal 7? Yes it is possible in drupal 7. We can set multiple URL alias for a conten type programmatically using pathauto module in drupal 7. We need to insert our path alias into the "url_alias" table while inserting & updating a node and remove path alias When delete a node.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Add URL alias programmatically when insert and update a node using pathauto module in drupal 7:</span></h3>
<br />
For instance, I've choosen article content type. We need to insert & update a URL alias into the "url_alias" table using hook_node_insert() & hook_node_update() in drupal 7.<br />
<br />
<div class="bdr">
<br />
/**<br />
* Implements hook_node_insert()<br />
*/<br />
function phponwebsites_node_insert($node) {<br />
if ($node->type == 'article') {<br />
//save node alias<br />
_phponwebsites_insert_update_alias($node);<br />
}<br />
}<br />
<br />
/**<br />
* Implements hook_node_update()<br />
*/<br />
function phponwebsites_node_update($node) {<br />
if ($node->type == 'article') {<br />
//update node alias<br />
_phponwebsites_insert_update_alias($node);<br />
}<br />
}<br />
<br />
/**<br />
* Insert and update alias for course<br />
*/<br />
function _phponwebsites_insert_update_alias($node) {<br />
module_load_include('inc', 'pathauto');<br />
$title = pathauto_cleanstring($node->title);<br />
<br />
$values['source'] = 'node/' . $node->nid . '/article';<br />
$values['alias'] = 'article/' . $title;<br />
<br />
$all_values = array($values);<br />
<br />
foreach ($all_values as $all) {<br />
$query = db_merge('url_alias')<br />
->fields(array('source' => $all['source'], 'alias' => $all['alias'], 'language' => LANGUAGE_NONE))<br />
->key(array('source' => $all['source']))<br />
->execute();<br />
}<br />
}<br />
<br /></div>
<br />
<br />
Where,<br />
pathauto_cleanstring is obey the pathatuo module's rules which is mentioned at admin/config/search/path/settings. To know more details of pathauto_cleanstring, please visit <a href="http://www.drupalcontrib.org/api/drupal/contributions!pathauto!pathauto.inc/function/pathauto_cleanstring/7">http://www.drupalcontrib.org/api/drupal/contributions!pathauto!pathauto.inc/function/pathauto_cleanstring/7</a><br />
<br />
After added the above code into your custome module(clear cache), you will create a article. You just test your url at admin/config/search/path in the pathauto's list. It looks like below image:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-D0Im-Y977cM/V5po8OYKBhI/AAAAAAAAAwI/8YFiXsiO3G4P1iYutAInEe81pwQl0UahQCLcB/s1600/drupal-7-pathauto-multiple-url-alias.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Pathauto module URL alias list in drupal 7" border="0" height="159" src="https://1.bp.blogspot.com/-D0Im-Y977cM/V5po8OYKBhI/AAAAAAAAAwI/8YFiXsiO3G4P1iYutAInEe81pwQl0UahQCLcB/s320/drupal-7-pathauto-multiple-url-alias.png" title="Pathauto module URL alias list in drupal 7" width="320" /></a></div>
<br />
<br />
Now you could access the article by both node-title as well as article/node-title.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-on0Fp7LWlxc/V5ppctqky-I/AAAAAAAAAwM/e6zgtdgCNlUjsuTX_bbIt43nrzpBU5FgwCLcB/s1600/drupal-7-pathauto-multiple-url-alias-for-node.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Multiple URL alias for a node using pathauto module in drupal 7" border="0" height="320" src="https://3.bp.blogspot.com/-on0Fp7LWlxc/V5ppctqky-I/AAAAAAAAAwM/e6zgtdgCNlUjsuTX_bbIt43nrzpBU5FgwCLcB/s320/drupal-7-pathauto-multiple-url-alias-for-node.png" title="Multiple URL alias for a node using pathauto module in drupal 7" width="278" /></a></div>
<br />
<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Delete URL alias programmatically when delete a node using pathauto module in drupal 7:</span></h3>
<br />
We've inserted 2 URL alias for a node. So we need to delete those from "url_alias" table when delete a node. We can trigger it using hook_node_delete() in drupal 7. Consider the below code:<br />
<br />
<br />
<div class="bdr">
<br />
/**<br />
* Implements hook_node_delete()<br />
*/<br />
function arep_node_delete($node) {<br />
if ($node->type == 'article') {<br />
//delete node alias for ceu and non-ceu course<br />
module_load_include('inc', 'pathauto');<br />
$source[0] = 'node/' . $node->nid . '/article';<br />
<br />
foreach ($source as $s) {<br />
$path = path_load(<br />
array('source' => $s)<br />
);<br />
path_delete($path['pid']);<br />
}<br />
<br />
}<br />
}</div>
<br />
<br />
Where,<br />
path_load returns the details of a URL alias like source, alias, path id & language. To know more details of path_load(), please visit <a href="https://api.drupal.org/api/drupal/includes!path.inc/function/path_load/7.x.">https://api.drupal.org/api/drupal/includes!path.inc/function/path_load/7.x.</a><br />
<br />
After added the above code into your customer module(clear cache), you will delete a node and check your URL alias at admin/config/search/path. Now tt should not be displayed here.<br />
<br />
Now I've hope you know how to set multiple URL alias for a content type.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2016/07/drupal-7-pathauto-remove-special-characters-url-alias.html" itemprop="relatedLink">Remove speical characters from URL alias using pathauto module in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2017/03/drupal-update-multiple-fields-using-ajax-callback-form.html" itemprop="relatedLink"><b>Update multiple fields using #ajax in Drupal 7</b></a>
</div>
<br /></div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com3tag:blogger.com,1999:blog-1892780101239299215.post-11127568844461917012016-07-27T14:54:00.000-05:002017-03-01T13:21:54.919-06:00Pathauto added special characters in url alias - drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
To improve SEO, we need to clean our URLs. By default in drupal, we've an option called clean URLs at the configuration. In drupal 7, we can also manage the URLs. For instance, you have a content type called services. You wanted to each service page have url like services/page-name. To do that, we've a <a href="https://www.drupal.org/project/pathauto">pathauto</a> module in drupal 7. The pathauto module allow us to manage the URLs for every content types, files, taxonomy & users and also we can remove some unnecessary words from URL like an, the and so on.<br />
<br />
The pathauto module can remove some unnecessary words like a, an, the and so on & also remove special characters like !, @, $ and so on. Unfortunately, it doesn't included some other symbols like copyright(©), trademark(™), registered(®) and so on. But it provide a hook to add new symbols into the punctuation settings called hook_pathauto_punctuation_chars_alter. After created a content with some symbols which are represented above, your page URL looks like below image:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-xaUxuLAPV34/V5kML8eMYNI/AAAAAAAAAvk/XL7F55R8OkkYaSF24fMpu9gG65E-4EQswCLcB/s1600/drupal-pathauto-special-characters.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Drupal 7 - remove special characters from url using pathauto module" border="0" src="https://4.bp.blogspot.com/-xaUxuLAPV34/V5kML8eMYNI/AAAAAAAAAvk/XL7F55R8OkkYaSF24fMpu9gG65E-4EQswCLcB/s1600/drupal-pathauto-special-characters.png" title="Drupal 7 - remove special characters from url using pathauto module" /></a></div>
<br />
<br />
<br />
<div class="bdr">
/**<br />
* Implements hook_pathauto_punctuation_chars_alter().<br />
*/<br />
function phponwebsites_pathauto_punctuation_chars_alter(array &$punctuation) {<br />
$punctuation['copyright'] = array('value' => '©', 'name' => t('Copyright'));<br />
$punctuation['registered'] = array('value' => '®', 'name' => t('Registered trademark'));<br />
$punctuation['trademark'] = array('value' => '™', 'name' => t('Trademark'));<br />
}</div>
<br />
After implemented above code into your module, you cold see added symbols are listing on Pathauto module's settings page at /admin/config/search/path/settings. If You didn't get these symbols, clear cache & test it again. It looks like below image:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-dRSIb-LHXkg/V5kPURZoSvI/AAAAAAAAAvw/unIIVfRzYgAaW6TW_EiilgPD3GdSAxeYQCLcB/s1600/drupal-pathauto-special-characters.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Drupal 7 - pathauto settings after hook_pathauto_punctuation_chars_alter" border="0" src="https://1.bp.blogspot.com/-dRSIb-LHXkg/V5kPURZoSvI/AAAAAAAAAvw/unIIVfRzYgAaW6TW_EiilgPD3GdSAxeYQCLcB/s1600/drupal-pathauto-special-characters.png" title="Drupal 7 - pathauto settings after hook_pathauto_punctuation_chars_alter" /></a></div>
<br />
<br />
Now you can create a content with those symbols. The pathauto module didn't added those symbols into the URL.<br />
<br />
Now I hope you know how to remove some special characters from URL alias using pathauto module in drupal 7.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/08/drupal-7-pathauto-multiple-url-alias.html" itemprop="relatedLink"><b>How to set multiple URL alias for a node using pathauto module in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2017/03/drupal-update-multiple-fields-using-ajax-callback-form.html" itemprop="relatedLink"><b>Update multiple fields using #ajax in Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com12tag:blogger.com,1999:blog-1892780101239299215.post-74304017398066880062016-06-28T14:15:00.000-05:002016-06-28T14:26:20.952-05:00PHP get particular key value from multidimensional array<div dir="ltr" style="text-align: left;" trbidi="on">
You can get specific key from multidimensional array using any one of the below methods:<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">1. Get Specific key value form multidimensional array using array_column:</span></h3>
<br />
The array_column() returns the values from a single column in the input array. It works only from PHP version 5.5. You've a multidimentional array with details of user name and country. You tried to get the name of every array from multidimentional array. Consider the following exampale:<br />
<br />
<div class="bdr">
<?php<br />
<br />
$array = array(<br />
0 => array(<br />
'name' => 'Guru',<br />
'country' => 'India'<br />
),<br />
1 => array(<br />
'name' => 'Clark',<br />
'country' => 'USA'<br />
),<br />
2 => array(<br />
'name' => 'Smith',<br />
'country' => 'United Kingdom'<br />
),<br />
3 => array(<br />
'name' => 'John',<br />
'country' => 'USA'<br />
),<br />
);<br />
<br />
$namesArray = array_column($array, 'name');<br />
print_r($namesArray);exit;</div>
<br />
When you run this program in the browser, you will get the output like below one:<br />
<br />
Array ( [0] => Guru [1] => Clark [2] => Smith [3] => John )<br />
<br />
For more details about array_coulmn, visit <a href="http://php.net/manual/en/function.array-column.php">http://php.net/manual/en/function.array-column.php</a>.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;"> 2. Get Specific key value form multidimensional array using array_map:</span></h3>
<br />
The array_map() applies the callback to the elements of the given arrays. It works from PHP version 4.0.6. The array_map() is alternate of array_column() in PHP<br />
<br />
<div class="bdr">
<?php<br />
$namesArray = array_map(function($arr){<br />
return $arr['name'];<br />
}, $array);<br />
print_r($namesArray);exit;</div>
<br />
It will give same output as array_column. For more details about array_map, visit <a href="http://php.net/manual/en/function.array-map.php">http://php.net/manual/en/function.array-map.php</a>.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;"> 3. Get Specific key value form multidimensional array using foreach loop:</span></h3>
<br />
<div class="bdr">
<?php<br />
$namesArray = array();<br />
foreach($array as $key => $val) {<br />
$namesArray[] = $val['name'];<br />
}<br />
print_r($namesArray);exit;</div>
<br />
It will also give sample output as array_coulmn & array_map.<br />
<br />
Now I've hope you know how to get particular key value from multidimensional array in PHP.<br />
<br />
Releated Articles:</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com2tag:blogger.com,1999:blog-1892780101239299215.post-86179526906597216772016-04-28T10:24:00.001-05:002016-04-28T10:24:47.619-05:00Create a node programmatially in Drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about how to create a
new node programmatically in Drupal 7. If you want to add a new node, you can done
at node/add by default. In Drupal, you can also add a node programmatically.
Let see the below code.
<br />
<br />
<div class="bdr">
<?php<br />
// create object<br />
$node = new stdClass();<br />
// set title for a node<br />
$node->title = t('Created node programmatically');<br />
// set node type<br />
$node->type = 'article';<br />
// set node language<br />
$node->language = LANGUAGE_NONE;<br />
// set value to node body<br />
$node->body[LANGUAGE_NONE][0]['value'] = t('This node has been created programmatically in Drupal 7');<br />
// set value to node body summary<br />
//$node->body[LANGUAGE_NONE][0]['summary'] = text_summary(t('This node has been created programmatically in Drupal 7'));<br />
// set node body format like plain_text, filtered_html, full_html<br />
$node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';<br />
node_object_prepare($node);<br />
// author for a node<br />
$node->uid = 1;<br />
// status of node 0 - unpublished, 1 - published<br />
$node->status = 1;<br />
// promoted to front page or not<br />
$node->promote = 0;<br />
// sitcky at top of tha page<br />
$node->sticky = 0;<br />
// comments 0 - hidden, 1 - closed, 2 - opened<br />
$node->comment = 1;<br />
<br />
// add term<br />
$node->field_tags[$node->language][]['tid'] = 1;<br />
<br />
// get the file path<br />
$file_path = drupal_get_path('module', 'phponwebsites') . '/Desert.jpg';<br />
// create file object<br />
$file = (object) array(<br />
'uid' => 1,<br />
'uri' => $file_path,<br />
'filemime' => file_get_mimetype($file_path),<br />
'status' => 1,<br />
);<br />
// Save the file to the public directory. You can specify a subdirectory, for example, 'public://images'<br />
$file = file_copy($file, 'public://');<br />
// assign the file object into image field<br />
$node->field_image[LANGUAGE_NONE][0] = (array)$file;<br />
// Prepare node for a submit<br />
$node = node_submit($node);<br />
//save the node<br />
node_save($node);<br />
<br /></div>
<br />
After ran this code, you can see newly created node at admin/content.
When you view that node, it looks like below image:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-5PAKeqNMo3o/VyIivmlM2pI/AAAAAAAAAss/D71ly5d9wAEbFqr4jS20oDfOhvzq4A1MACLcB/s1600/drupal-7-create-node-programmatically-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Create a new node programmatially in Drupal 7 at Phponwebsites" border="0" src="https://1.bp.blogspot.com/-5PAKeqNMo3o/VyIivmlM2pI/AAAAAAAAAss/D71ly5d9wAEbFqr4jS20oDfOhvzq4A1MACLcB/s1600/drupal-7-create-node-programmatically-phponwebsites.png" title="Create a new node programmatially in Drupal 7 at Phponwebsites" /></a></div>
<br />
<br />
<div>
Now I’ve hope you know how to create a new node programmatically in
Drupal 7.
</div>
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users into any page after logged into a site in Drupal 7</b></a>
</div>
</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-53957738055535326412016-04-21T11:42:00.002-05:002016-04-21T11:42:55.813-05:00Redirect users after login in Drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about how to redirect users after logged into a site in Drupal 7. By default, Drupal redirects users to user page after logged into a site. Suppose you want to redirect users into any other pages as you want. Then you can done that in Drupal 7.<br />
You can redirect users after login in Drupal using the following two ways:<br />
1.<span class="Apple-tab-span" style="white-space: pre;"> </span>Redirect users after logged into a site using hook_user_login()<br />
2.<span class="Apple-tab-span" style="white-space: pre;"> </span>Redirect users after logged into a site using custom form submit<br />
<br />
<a href="https://1.bp.blogspot.com/-nvpgMT1lGR4/VxEhZTNv6ZI/AAAAAAAAAsU/iQQIhkrODVwH4YvNfDtW6_IlGU6rY4HngCLcB/s1600/login-email-username-drupal-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Redirect users after form submit in Drupal 7" border="0" src="https://1.bp.blogspot.com/-nvpgMT1lGR4/VxEhZTNv6ZI/AAAAAAAAAsU/iQQIhkrODVwH4YvNfDtW6_IlGU6rY4HngCLcB/s1600/login-email-username-drupal-phponwebsites.png" title="Redirect users after form submit in Drupal 7" /></a>
<br />
<br />
<h3 style="text-align: left;">
Redirect users after logged into a site using hook_user_login:</h3>
<br />
Drupal provides hook called hook_user_login to make changes while user login successfully. Let see the below code.<br />
<br />
<div class="bdr">
/**<br />
* Implement hook_user_login()<br />
*/<br />
function phponwebsites_user_login(&$form, &$form_state) {<br />
//add page here to where you want redirect users after login<br />
$form['redirect'] = '<front>';<br />
}</div>
<br />
Now you can check whether you redirect to front page or not after login. Now Drupal will be redirect you to front page.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">
Redirect users after logged into a site using custom form submit:</span></h3>
<br />
Drupal have alternate method to redirect users after login. Ie, You need to add custom form submit handler to a form using hook_form_alter(). Then add a page to redirect users in that custom form submit handler in Drupal 7. Let see the below code.<br />
<br />
<div class="bdr">
<br />
/**<br />
* Implement hook_form_alter().<br />
*/<br />
function phponwebsites_form_alter(&$form, &$form_state, $form_id) {<br />
if ($form_id == "user_login" || $form_id == "user_login_block") {<br />
$form['#submit'][] = 'phponwebsites_custom_login_submit';<br />
}<br />
} <br />
function phponwebsites_custom_login_submit(&$form, &$form_state) {<br />
//page to be redirect<br />
$form['redirect'] = '<front>';<br />
}</div>
<br />
<br />
Now you will be redirect to front page after logged into a drupal site. Now I’ve hope you should know how to redirect users after logged into a site in Drupal 7.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a>
</div>
</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com4tag:blogger.com,1999:blog-1892780101239299215.post-84717754704642066812016-04-19T09:46:00.001-05:002016-04-21T11:46:08.019-05:00Create page without header and footer in Drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about create only page contents without header and footer in Drupal 7. All of you know almost all of the pages in Drupal have header and footer. Suppose you want to create a page without header and footer in Drupal 7. Is it possible? Yes, it is possible in Drupal 7. You can create a page without header and footer using 'delivery callback' in hook_menu.<br />
<br />
<h3 style="text-align: left;">
Render a page without header and footer in Drupal 7:</h3>
<br />
Drupal provide a option to create page without header and footer. Let see the below code for render a page without header and footer in Drupal 7.<br />
<br />
<div class="bdr">
/**<br />
* Implement hook_menu().<br />
*/<br />
function phponwebsites_menu() {<br />
$items['sample-wo-header-footer'] = array(<br />
'title' => 'A page without header and footer in Drupal 7',<br />
'access callback' => TRUE,<br />
'page callback' => 'phponwebsites_without_header_footer',<br />
'type' => MENU_CALLBACK,<br />
'delivery callback' => 'deliver_plain',<br />
);<br />
return $items;<br />
}<br />
<br />
function deliver_plain($page_callback_result) {<br />
print $page_callback_result;<br />
}<br />
<br />
/**<br />
* Implement phponwebsites_without_header_footer().<br />
*/<br />
function phponwebsites_without_header_footer() {<br />
return 'This is the page without header and footer';<br />
}</div>
<br />
<br />
You could see the page without any header and footer when you view page in a browser. Now I've hope you how to render a page without header and footer in Drupal 7.
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b><br /></b>
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com2tag:blogger.com,1999:blog-1892780101239299215.post-54983791885338358882016-04-16T02:21:00.000-05:002016-04-21T11:46:35.161-05:00Clear views cache when insert, update and delete a node in Drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes how to clear views cache while inserting, updating and deleting a node in Drupal 7. If we want to improve site performance, then views caching is one of the options.<br />
<br />
For example, you have views which display list of records. It will update occasionally. Then we can render views data from cache rather than server if we set cache for views. We can set views cache at its settings page. Suppose you have cached views for 5 mins. Then it didn't display updated data until 5 mins even if new node is added to that views. It displays updated data only after 5 mins because the views is cached for 5 mins. In that situation, the user can't view new data in cached views. So we need to clear views cache when add , update and delete a node. So only we can see new data in views and also data is rendered from cache.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-2dZJrEzybyo/Vw54vofxgNI/AAAAAAAAAr0/t1_yNPbeeY8VKgDshXJUmfE410aul9MvQCLcB/s1600/drupal-7-clear-views-cache-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Clear views cahce when insert, update and delete a node in drupal 7" border="0" src="https://3.bp.blogspot.com/-2dZJrEzybyo/Vw54vofxgNI/AAAAAAAAAr0/t1_yNPbeeY8VKgDshXJUmfE410aul9MvQCLcB/s1600/drupal-7-clear-views-cache-phponwebsites.png" title="Clear views cahce when insert, update and delete a node in drupal 7" /></a></div>
<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Clear views cache when insert a new node in Drupal 7:</span></h3>
The newly added node has not been displayed in views list if the cache is applied to a views. So we need to clear views cache when insert a new node using hook_node_insert(). Lets see the code for clear views cache while inserting a node:<br />
<br />
<div class="bdr">
<?php<br />
/**<br />
* Imeplement hook_node_insert().<br />
*/<br />
function phponwebsites_node_insert($node) {<br />
if ($node->type == 'tasks') {<br />
//clear views cache<br />
$viewsname = 'activity';<br />
cache_clear_all($viewsname, 'cache_views_data', TRUE);<br />
}<br />
}</div>
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Clear views cache when update a node in Drupal 7:</span></h3>
When you tried to update a node, the updated data in that node has not been displayed in views. So we need to clear views cache when update a node using hook_node_update(). Lets see the code for clear views cache while updating a node:<br />
<br />
<div class="bdr">
<?php<br />
/**<br />
* Imeplement hook_node_update().<br />
*/<br />
function phponwebsites_node_update($node) {<br />
if ($node->type == 'article') {<br />
//clear views cache<br />
$viewsname = 'articles';<br />
cache_clear_all($viewsname, 'cache_views_data', TRUE);<br />
}<br />
}</div>
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Clear views cache when delete a node in Drupal 7:</span></h3>
After delete a node, you could see the deleted node is displayed in the views. So we need to clear views when delete a node using hook_node_delete(). Lets see the code for clear views cache while deleting a node:<br />
<br />
<div class="bdr">
<?php<br />
/**<br />
* Imeplement hook_node_delete().<br />
*/<br />
function phponwebsites_node_delete($node) {<br />
if ($node->type == 'article') {<br />
//clear views cache<br />
$viewsname = 'articles';<br />
cache_clear_all($viewsname, 'cache_views_data', TRUE);<br />
}<br />
}</div>
<br />
You can see the performance of views page will be increased and you can see changes in your views. Now I've hope you know how to clear views cache when insert, update and delete a node in Drupal 7.
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b><br /></b>
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/login-using-email-and-username-in-drupal-7.html" itemprop="relatedLink"><b>Login using both email and username in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com23tag:blogger.com,1999:blog-1892780101239299215.post-79416632267372617972016-04-15T12:31:00.000-05:002016-04-21T11:47:03.125-05:00Login using email and username in Drupal 7<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about how to login using both email and username in Drupal 7. All of you know we could login using only username in Drupal 7.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-nvpgMT1lGR4/VxEhZTNv6ZI/AAAAAAAAAsU/iQQIhkrODVwH4YvNfDtW6_IlGU6rY4HngCLcB/s1600/login-email-username-drupal-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Login using mail address and usename in Drupal 7" border="0" src="https://1.bp.blogspot.com/-nvpgMT1lGR4/VxEhZTNv6ZI/AAAAAAAAAsU/iQQIhkrODVwH4YvNfDtW6_IlGU6rY4HngCLcB/s1600/login-email-username-drupal-phponwebsites.png" title="Login using mail address and usename in Drupal 7" /></a></div>
<br />
<br />
I've tried to login using email without any contrib modules. Finally i got the code. First alter form to add custom form validation. In custom form validation, get the name from user table by email and set that value into name field in form. Let see the code:<br />
<br />
<div class="bdr">
<?php<br />
/**<br />
* Implement hook_form_alter().<br />
*/<br />
function phponwebsites_form_alter(&$form, &$form_state, $form_id) {<br />
<br />
if ($form_id == "user_login" || $form_id == "user_login_block") {<br />
$form['name']['#title'] = t('Username or E-mail Address');<br />
// Ensure a valid validate array.<br />
$form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();<br />
// login using username or email address<br />
array_unshift($form['#validate'],'phponwebsites_user_login_validate');<br />
}<br />
}<br />
<br />
/**<br />
* Implement phponwebsites_user_login_validate()<br />
*<br />
* Return name by its email address<br />
*/<br />
function phponwebsites_user_login_validate($form, &$form_state) {<br />
if (isset($form_state['values']['name']) && strpos($form_state['values']['name'], '@') !== false) {<br />
$name = db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)", array(':name' => $form_state['values']['name']))->fetchField();<br />
}<br />
if (isset($name)) {<br />
form_set_value($form['name'], $name, $form_state);<br />
}<br />
}</div>
<br />
Now you can login using both username and email. I've hope you know how to login using both username and email in
Drupal 7.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/clear-views-cache-when-insert-update-delete-node-drupal-7.html" itemprop="relatedLink"><b>Clear views cache when insert, update and delete a node in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/render-page-without-header-footer-drupal-7.html" itemprop="relatedLink"><b>Create a page without header and footer in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com20tag:blogger.com,1999:blog-1892780101239299215.post-84858780173791946712016-01-10T09:54:00.001-06:002016-04-21T11:47:57.062-05:00Drupal 7 – Hide Promoted to front page & Sticky at top of lists options<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes how to hide "Promoted to front page" and "Sticky at top of lists" options from node form in drupal 7. When adding or editing a node, you can see "Publishing options" at bottom of the page which contains 'Published', 'Promoted to front page' and 'Sticky at top of lists' checkbox options. It should look like below image:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-VGTPCA5Zw2Y/VnmMi7ltcdI/AAAAAAAAAqQ/un-F_tNk3SY/s1600/drupal7-hide-promoted-front-page-sticky-at-top-of-lists-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Promoted to front page & Sticky at top of lists in Drupal 7" border="0" src="http://4.bp.blogspot.com/-VGTPCA5Zw2Y/VnmMi7ltcdI/AAAAAAAAAqQ/un-F_tNk3SY/s1600/drupal7-hide-promoted-front-page-sticky-at-top-of-lists-phponwebsites.png" title="Promoted to front page & Sticky at top of lists in Drupal 7" /></a></div>
<br />
The "Published" option is used to publish the content. The "Promoted to front page" option is used to display content in the front page. The 'Sticky at top of lists' option is used to keep the content sticked to the top of front page. If you don't want to show "Promoted to front page" and "Sticky at top of lists" options, then you can hide those options using hook_form_alter(), hook_form_FORM_ID_alter() and hook_form_BASE_FORM_ID_alter().<br />
<br />
<h3 style="text-align: left;">
Hide Promoted to front page & Sticky at top of lists options in single node form:</h3>
<div>
<br /></div>
<div>
If you want to hide "Promoted to front page" and "Sticky at top of lists" options only in single node form, then you can remove those options from node form using either hook_form_alter() or hook_form_FORM_ID_alter() in drupal 7. For example, we go to hide those options from article node form.</div>
<br />
<div class="bdr">
/**<br />
* Implement hook_form_alter().<br />
*/<br />
function phponwebsites_form_alter(&$form, &$form_state, $form_id) {<br />
// to hide promoted to front page option<br />
if (isset($form['options']['promote'])) {<br />
$form['options']['promote']['#access'] = FALSE;<br />
}<br />
<br />
// to hide sticky at top of lists option<br />
if (isset($form['options']['sticky'])) {<br />
$form['options']['sticky']['#access'] = FALSE;<br />
}<br />
}</div>
<br />
<div>
Now you go to article node form and check whether "Promoted to front page" and "Sticky at top of lists" options are hidden or not. You couldn’t see those options in article node form. It should look like below image:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-FdGal_qWVNc/VnmO8sgubhI/AAAAAAAAAqc/NS54xtYp3cs/s1600/drupal-7-remove-promoted-to-front-page-sticky-at-top-of-lists-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Hide Promoted to front page & Sticky at top of lists in drupal 7" border="0" src="http://4.bp.blogspot.com/-FdGal_qWVNc/VnmO8sgubhI/AAAAAAAAAqc/NS54xtYp3cs/s1600/drupal-7-remove-promoted-to-front-page-sticky-at-top-of-lists-phponwebsites.png" title="Hide Promoted to front page & Sticky at top of lists in drupal 7" /></a></div>
<div>
<br /></div>
<h3 style="text-align: left;">Hide Promoted to front page & Sticky at top of lists options in multiple node forms:</h3>
<div>
<br />
If you want to hide "Promoted to front page" and "Sticky at top of lists" options in all node forms, then you can remove those options using hook_form_BASE_FORM_ID_alter() in drupal 7.</div>
<div>
<br /></div>
<div class="bdr">
/**<br />
* Implement hook_form_BASE_FORM_ID_alter().<br />
*/<br />
function phponwebsites_form_node_form_alter(&$form, &$form_state, $form_id) {<br />
// to hide promoted to front page option<br />
if (isset($form['options']['promote'])) {<br />
$form['options']['promote']['#access'] = FALSE;<br />
}<br />
<br />
// to hide sticky at top of lists option<br />
if (isset($form['options']['sticky'])) {<br />
$form['options']['sticky']['#access'] = FALSE;<br />
}<br />
}</div>
<br />
<div>
Now you could not see those options in all node forms. Now you know how to hide "Promoted to front page" and "Sticky at top of lists" options from node form in drupal 7.</div>
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink"><b>Add custom fields to search api index in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com4tag:blogger.com,1999:blog-1892780101239299215.post-75557224796579710332015-12-29T02:05:00.001-06:002016-04-21T11:48:46.744-05:00Drupal 7 - Add custom fields to Search API solr index<div dir="ltr" style="text-align: left;" trbidi="on">
This is blog describes about how to add custom field to Search API solr index in Drupal 7.<br />
<br />
Suppose we need a new field, we can add new fields for a content type at <b>/admin/structure/content/<wbr></wbr>types</b>. Then all fields are showed at <b>/admin/config/search/search_<wbr></wbr>api/index/default_node_index/<wbr></wbr>fields</b>. Now you can add desired fields to solr index.<br />
<br />
Suppose you want to show custom field to Search API results but that field is not created for any specific content types. Is it possible in Search API? Yes you can done this with use of hook_entity_property_info_<wbr style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;"></wbr>alter().<br />
<br />
<div class="bdr">
/**<br />
* Implement phponwebsites_get_nodecountviews_nid()<br />
*<br />
* Return count of views by particular nid<br />
*/<br />
function phponwebsites_get_nodecountviews_nid($<wbr></wbr>nid) {<br />
$result = db_query("SELECT COUNT(*) as count FROM {nodeviewcount} WHERE nid=:nid", array(':nid' => $nid))->FetchAssoc();<br />
return $result['count'];<br />
}<br />
<br />
/**<br />
* Implements hook_entity_property_info_<wbr></wbr>alter().<br />
*/<br />
function phponwebsites_entity_property_info_<wbr></wbr>alter(&$info) {<br />
$info['node']['properties']['<wbr></wbr>is_nodeviewcount'] = array(<br />
'type' => 'integer',<br />
'label' => t('Node view count'),<br />
'description' => t("Number of views."),<br />
'sanitized' => TRUE,<br />
'getter callback' => 'phponwebsites_get_is_nodeviewcount_<wbr></wbr>callback',<br />
);<br />
}<br />
<br />
/**<br />
* Implement phponwebsites_get_is_nodeviewcount_<wbr></wbr>callback()<br />
*/<br />
function phponwebsites_get_is_nodeviewcount_<wbr></wbr>callback($item) {<br />
$count = phponwebsites_get_nodecountviews_nid(<wbr></wbr>$item->nid);<br />
$total = (int) $count;<br />
return $total;<br />
}</div>
<br />
After added above code into your custom module, go to <b>/admin/config/search/search_<wbr></wbr>api/index/default_node_index/<wbr></wbr>fields</b>. Now you could see new custom field is displayed as in below images.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-rQJuD09CTKg/Vm61x2zx_VI/AAAAAAAAAo4/9It-vCDbA30/s1600/arep-serach-api-solr-index.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Add custom fields to search API solr index in Drupal 7" border="0" height="132" src="http://3.bp.blogspot.com/-rQJuD09CTKg/Vm61x2zx_VI/AAAAAAAAAo4/9It-vCDbA30/s400/arep-serach-api-solr-index.png" title="Add custom fields to search API solr index in Drupal 7" width="400" /></a></div>
<br />
<br />
Now you can add your custom field into search api solr index and index that field. The custom field is listed in views add field section. Now you can add custom field into search results.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b>
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a></div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-69594631722149740832015-12-22T11:13:00.000-06:002016-04-21T11:49:30.718-05:00Drupal 7 - Create menu tab programmatically<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about how to create menu tab programmatically in drupal 7. We can create menu items using hook_menu().<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-HDf4Lf8p7Zo/VnmCm3YDxZI/AAAAAAAAAp8/vmdGb0FLiPs/s1600/drupal-7-menu-tab-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Create menu tab programmatically" border="0" src="http://4.bp.blogspot.com/-HDf4Lf8p7Zo/VnmCm3YDxZI/AAAAAAAAAp8/vmdGb0FLiPs/s1600/drupal-7-menu-tab-phponwebsites.png" title="Create menu tab programmatically" /></a></div>
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Menu tab creation in Drupal 7:</span></h3>
<br />
Consider below code snippet to create menu tab in drupal 7.<br />
<br />
<div class="bdr">
/**<br />
* Implement hook_menu()<br />
*/<br />
function phponwebsites_menu() {<br />
<br />
$items['test'] = array(<br />
'title' => t('Create Menu Tab'),<br />
'page callback' => 'testpage_tab1',<br />
'access callback' => TRUE,<br />
);<br />
<br />
$items['test/tab1'] = array(<br />
'title' => t('First Tab'),<br />
'type' => MENU_DEFAULT_LOCAL_TASK,<br />
'page callback' => 'testpage_tab1',<br />
'access callback' => TRUE,<br />
);<br />
<br />
$items['test/tab2'] = array(<br />
'title' => t('Second Tab'),<br />
'type' => MENU_LOCAL_TASK,<br />
'page callback' => 'testpage_tab2',<br />
'access callback' => TRUE,<br />
);<br />
<br />
$items['test/tab3'] = array(<br />
'title' => t('Third Tab'),<br />
'type' => MENU_LOCAL_TASK,<br />
'page callback' => 'testpage_tab3',<br />
'access callback' => TRUE,<br />
);<br />
<br />
return $items;<br />
}<br />
<br />
/**<br />
* Implement testpage_tab1()<br />
*/<br />
function testpage_tab1() {<br />
$str = t('Hi this is first tab');<br />
return $str;<br />
}<br />
<br />
/**<br />
* Implement testpage_tab2()<br />
*/<br />
function testpage_tab2() {<br />
$str = t('Hi this is second tab');<br />
return $str;<br />
}<br />
<br />
/**<br />
* Implement testpage_tab3()<br />
*/<br />
function testpage_tab3() {<br />
$str = t('Hi this is third tab');<br />
return $str;<br />
}</div>
<br />
<br />
Where,<br />
In hook_menu,<br />
Title – page title<br />
Type – type of menu item,<br />
MENU_CALLBACK, MENU_DEFAULT_LOCAL_TASK, MENU_LOCAL_TASK, MENU_LOCAL_ACTION, MENU_NORMAL_ITEM, MENU_SUGGESTED_ITEM are types of<br />
menu item in drupal 7.<br />
<br />
MENU_CALLBACK – register path for a menu item<br />
MENU_DEFAULT_LOCAL_TASK – default tab for a menu<br />
MENU_LOCAL_TASK – additional tabs for menu<br />
MENU_LOCAL_ACTION – actions for menu items<br />
MENU_NORMAL_ITEM – add menu item into any menus like main_menu, user_menu<br />
MENU_SUGGESTED_ITEM – module may suggest menu items<br />
<br />
Page callback – callback for a menu<br />
Access callback – who can access the page<br />
<br />
Now i’ve hope you should know how to create menu tab programmatically in drupal 7.<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink"><b>Add class into menu item in Drupal 7</b></a><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink">Add custom fields into apache solr search api index in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a></div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-28167700456499299552015-12-18T04:32:00.000-06:002016-04-21T11:51:34.679-05:00Drupal 7 – Add class into menu item using hook_menu()<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about how to add class into menu item that is created programmatically using hook_menu() in drupal 7.<br />
We know how to add custom menu item into already created menu in drupal7. Is it possible to add class to that menu item in durpal 7? Yes you can add custom classes into menu item using hook_menu() in drupal 7.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Add class into menu item in drupal 7:</span></h3>
<br />
Consider below program to add class into menu item in drupal 7.<br />
<br />
<div class="bdr">
/**<br />
* Implement hook_menu()<br />
*/<br />
function phponwebsites_menu() {<br />
$items['sample'] = array(<br />
'title' => t('Sample page'),<br />
'type' => MENU_NORMAL_ITEM,<br />
'menu_name' => 'main-menu',<br />
'page callback' => 'samplepage',<br />
'access callback' => TRUE,<br />
'options' => array(<br />
'attributes' => array(<br />
'class' => array('drupal-menu-class')<br />
)<br />
),<br />
);<br />
<br />
return $items;<br />
}<br />
<br />
/**<br />
* Implement samplepage()<br />
*/<br />
function samplepage() {<br />
$str = t('Hi this is sample page');<br />
return $str;<br />
}</div>
<br />
Where,<br />
type – MENU_NORMAL_ITEM<br />
menu-name – name of the menu to add new link<br />
options – add any attributes like class, id<br />
<br />
You need to clear cache to see created menu items with custom class into main menu.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Add multiple classes into menu item in drupal 7:</span></h3>
<br />
Similarly you can add multiple classes into menu item using hook_menu() in drupal 7. Consider below program to add multiple<br />
classes into menu item.<br />
<br />
<div class="bdr">
function phponwebsites_menu() {<br />
$items['sample'] = array(<br />
'title' => t('Sample page'),<br />
'type' => MENU_NORMAL_ITEM,<br />
'menu_name' => 'main-menu',<br />
'page callback' => 'samplepage',<br />
'access callback' => TRUE,<br />
'options' => array(<br />
'attributes' => array(<br />
'class' => array('drupal-menu-class' , 'drupal-menu-new-class')<br />
)<br />
),<br />
);<br />
<br />
return $items;<br />
}</div>
<br />
Now i’ve hope you know how to add class programmatically to menu item using hook_menu() in drupal 7.<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-link-into-menu-programmatically.html" itemprop="relatedLink">Add new menu item into already created menu in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink"><b>Create menu tab programmatically in Drupal 7</b></a><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink">Add custom fields into apache solr search api index in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a></div>
</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-2832177445182627032015-12-16T08:23:00.002-06:002016-04-21T11:50:20.790-05:00Drupal 7 - add link into menu programmatically using hook_menu()<div dir="ltr" style="text-align: left;" trbidi="on">
<a href="https://www.blogger.com/blogger.g?blogID=1892780101239299215" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"></a><br />
This blog describes about how to add a new menu item into menu like main menu, user menu in drupal 7.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-teZNknruIdY/VnBdIE068yI/AAAAAAAAApY/cjUeSGOeqH4/s1600/drupal-7-add-menu-item-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="drupal 7 - add link into menu programmatically using hook_menu()" border="0" src="http://2.bp.blogspot.com/-teZNknruIdY/VnBdIE068yI/AAAAAAAAApY/cjUeSGOeqH4/s1600/drupal-7-add-menu-item-phponwebsites.png" title="drupal 7 - add link into menu programmatically using hook_menu()" /></a></div>
<br />
<div class="MsoNormal">
<br />
We can create a menu item using hook_menu in drupal 7. Can we add menu item into already created menu in drupal7? Yes you can add a link into menu using hook_menu().<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">
Add new menu item into main menu in drupal 7:</span></h3>
<div>
<br /></div>
<div>
Consider below program to add new menu item into main menu in drupal 7.</div>
<br />
<div class="bdr">
/**<br />
* Implement hook_menu()<br />
*/<br />
function phponwebsites_menu() {<br />
$items['sample'] = array(<br />
'title' => t('Sample page'),<br />
'type' => MENU_NORMAL_ITEM,<br />
'menu_name' => 'main-menu',<br />
'page callback' => 'samplepage',<br />
'access callback' => TRUE,<br />
);<br />
<br />
return $items;<br />
}<br />
<br />
/**<br />
* Implement samplepage()<br />
*/<br />
function samplepage() {<br />
$str = t('Hi this is sample page');<br />
return $str;<br />
}</div>
<div>
<br /></div>
<div>
<div>
<br /></div>
<div>
Where,</div>
<div>
type – MENU_NORMAL_ITEM</div>
<div>
menu-name – name of the menu to add new link</div>
<div>
</div>
<div>
You need to clear cache to see created new menu item in main menu. Now i’ve hope you know how to add new link programmatically to already created menu in drupal 7.<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related articles:</b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-add-class-into-menu-item-programmatically.html" itemprop="relatedLink">Add class into menu item in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-hook-menu-create-menu-tab-programmatically.html" itemprop="relatedLink">Create menu tab programmatically in Drupal 7</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/12/drupal-7-add-custom-fields-to-search-api-solr-index.html" itemprop="relatedLink">Add custom fields into apache solr search api index in Drupal 7</a></b><br />
<a href="http://www.phponwebsites.com/2016/04/drupal-7-redirect-users-after-login.html" itemprop="relatedLink"><b>Redirect users after login in Drupal 7</b></a></div>
</div>
</div>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com6tag:blogger.com,1999:blog-1892780101239299215.post-43548256857585945612015-12-13T23:47:00.000-06:002015-12-16T07:37:38.437-06:00Get multiple selected values from drop down box using PHP<div dir="ltr" style="text-align: left;" trbidi="on">
You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Now the doubt is rise. How to select multiple values from drop down box and how to get multiple selected input data from drop down box in PHP. This post describes retrieve multiple selected values from drop down box using PHP.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<h3 style="text-align: left;">
<span style="font-weight: normal; white-space: pre;">How to select multiple values from drop down box in HTML</span></h3>
<br />
You know select single value in drop down box. Then how to select multiple values in drop down box. You have to add attribute "<b>multiple</b>" and set name as <b>array</b> in drop down box in order to select multiple values from drop down box. If you want to select more than one values in drop down box, then press "<b>ctrl</b>" while selecting values in drop down box. Let consider below example to make HTML formwith select multiple values in drop down box.<br />
<br />
<div class="bdr">
<!DOCTYPE html><br />
<html><br />
<body><br />
<form action="#" method="post"><br />
<select name="country[]" multiple><br />
<option value="USA">USA</option><br />
<option value="United Kingdom">United Kingdom</option><br />
<option value="Russia">Russia</option><br />
<option value="Brazil">Brazil</option><br />
<option value="India">India</option><br />
</select><br />
<input type="submit" name="submit"><br />
</form><br />
</body><br />
</html></div>
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Retrieve multiple selected values from drop down box using PHP</span></h3>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div>
<span style="font-weight: normal;"> </span>You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Then display each selected items through <b>foreach</b> loop in PHP. The PHP script describes how to select multiple selected values from drop down box.</div>
<br />
<div class="bdr">
<!DOCTYPE html><br />
<html><br />
<body><br />
<form action="#" method="post"><br />
<select name="country[]" multiple><br />
<option value="USA">USA</option><br />
<option value="United Kingdom">United Kingdom</option><br />
<option value="Russia">Russia</option><br />
<option value="Brazil">Brazil</option><br />
<option value="India">India</option><br />
</select><br />
<input type="submit" name="submit"><br />
</form><br />
<?php<br />
if(isset($_POST['submit']))<br />
{<br />
$country=$_POST['country'];<br />
foreach($country as $val)<br />
{<br />
echo $val.'<br>';<br />
}<br />
}<br />
?><br />
</body><br />
</html></div>
<br />
<br />
While submitting form with selected multiple values from drop down box, you can get multiple selected values using PHP.</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-8252068993801602792015-08-26T11:29:00.000-05:002015-08-26T11:29:38.219-05:00Find prime numbers between 1 and 100 in PHP<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
This blog describes how to find prime numbers between 1 and
100 using PHP. Prime number is a number which is divided by 1 and itself. Consider the number 5. It is prime number
because it can be divided by 1 and 5.</div>
<div class="MsoNormal">
<br /></div>
<h3>
PHP program for prime numbers:</h3>
<br />
<div class="MsoNormal">
Consider the
below program which is used to find prime numbers from 1 to 100. </div>
<br />
<div class="bdr">
<?php
<br />
for($i = 1; $i
<= 100; $i++)
<br />
$mm = 0;
<br />
for($j = 2; $j
<= $i/2; $j++) {<br />
//only not
prime numbers<br />
if ($i
% $j == 0) {<br />
$mm++;<br />
break;<br />
}<br />
}<br />
if ($mm == 0) {<br />
echo"$i
is prime number<br/>";<br />
}<br />
}<br />
?></div>
<br />
<div class="MsoNormal">
In above
program, if a number is divided by any number except 1 and itself, then it is
not prime number. Otherwise it is a prime number.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-7oRHJ7mP9pc/Vd3mrCyjaTI/AAAAAAAAAns/4_XTynmR8Bs/s1600/phponwebsites-prime-numbers-php.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="PHP program for find prime numbers from a to 100" border="0" src="http://2.bp.blogspot.com/-7oRHJ7mP9pc/Vd3mrCyjaTI/AAAAAAAAAns/4_XTynmR8Bs/s1600/phponwebsites-prime-numbers-php.png" title="PHP program for find prime numbers from a to 100" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Now you got prime numbers between 1 and 100 using PHP.</div>
<b>Related Post </b><br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b><a href="http://www.phponwebsites.com/2015/06/php-fibonacci-series-of-given-number.html" itemprop="relatedLink">Fibonacci series program in PHP</a></b><br />
<a href="http://www.phponwebsites.com/2015/06/php-factorial-of-given-number.html" itemprop="relatedLink"><b>Factorial of given number in PHP</b></a><br />
<a href="http://www.phponwebsites.com/2015/08/php-armstrong-numbers-between-1-to-1000.html" itemprop="relatedLink"><b>PHP program for find Armstrong Number</b></a></div>
</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com9tag:blogger.com,1999:blog-1892780101239299215.post-85253556434644779092015-08-19T12:11:00.001-05:002015-08-26T12:12:51.451-05:00Find Armstrong Numbers between 1 to 1000 in PHP<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes how to find Armstrong Numbers between 1 to 1000 using PHP?. Before we are going to program part, we should know what is Armstrong Number?. So only we can write program for find Armstrong Number in any languages like C, C++, PHP.<br />
<br />
<h3>
what is Armstrong Number?</h3>
<div>
<br /></div>
<div>
Armstrong Number is sum of 3 power of each digits equals to given number.</div>
<div>
For example: </div>
<div>
153 is Armstrong Number.</div>
<div>
1^3 + 5^3 + 3 ^3</div>
<div>
1 + 125 + 27</div>
<div>
153</div>
<br />
<h3 style="text-align: left;">
PHP Program to find Armstrong Numbers:</h3>
<br />
<div>
Consider below example which is find Armstrong Numbers between 1 to 1000.</div>
<br />
<div class="bdr">
<?php<br />
for($i = 1; $i <= 1000; $i++) {<br />
$sum = 0;<br />
$qu = $i;<br />
while($qu != 0) {<br />
$remainder = $qu % 10;<br />
$thrice = $remainder * $remainder * $remainder;<br />
$sum += $thrice;<br />
$qu = $qu / 10;<br />
}<br />
if ($sum == $i) {<br />
echo "<p>$i is armstrong number</p>";<br />
}<br />
}<br />
?></div>
<div>
<br /></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-GeklbnML19g/VczLqwh6iRI/AAAAAAAAAnQ/3Y0NyI_xhDc/s1600/phponwebsites-php-armstrong-numbers.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="PHP program for find Armstrong Numbers" border="0" src="http://2.bp.blogspot.com/-GeklbnML19g/VczLqwh6iRI/AAAAAAAAAnQ/3Y0NyI_xhDc/s1600/phponwebsites-php-armstrong-numbers.png" title="PHP program for find Armstrong Numbers" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Now you got Armstrong Numbers between 1 to 1000.</div>
<div>
<br /></div>
<div>
<b>Related Post:</b></div>
<div itemscope="" itemtype="http://schema.org/WebPage">
<b><a href="http://www.phponwebsites.com/2015/06/php-fibonacci-series-of-given-number.html" itemprop="relatedLink">Fibonacci series program in PHP</a></b><br />
<a href="http://www.phponwebsites.com/2015/06/php-factorial-of-given-number.html" itemprop="relatedLink"><b>Factorial of given number in PHP</b></a><br />
<a href="http://www.phponwebsites.com/2015/08/get-value-from-drop-down-box-using-php.html" itemprop="relatedLink"><b>Get value of multiple drop down box using PHP</b></a><br />
<a href="http://www.phponwebsites.com/2015/08/php-program-find-prime-numbers-between-1-and-100.html" itemprop="relatedLink"><b>Find prime numbers between 1 and 100 using PHP</b></a>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-83016786279861966852015-08-13T10:10:00.003-05:002015-08-13T10:10:50.353-05:00Get value from drop down box using PHP<div dir="ltr" style="text-align: left;" trbidi="on">
The drop down box allows you to select one or more values from it. The HTML tag "select" is also known as "drop down" or "pul down" box.You can get value from drop down box using PHP while form submitting.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">HTML form with drop down box</span></h3>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div>
<span style="font-weight: normal;"> The below example describes how to create HTML form with drop down box.</span></div>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div class="bdr">
<html><br />
<body><br />
<form action="#" method="post"><br />
<select name="country"><br />
<option value="USA">USA</option><br />
<option value="United Kingdom">United Kingdom</option><br />
<option value="France">France</option><br />
<option value="Russia">Russia</option><br />
<option value="India">India</option>
<br />
</select><br />
<input type="submit" name="submit" value="Submit"><br />
</form><br />
</body><br />
</html>
</div>
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Retrieve input data from drop down box using PHP</span></h3>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div>
<span style="font-weight: normal;"> You can retrieve input data from drop down box using following types in PHP.</span></div>
<div>
<span style="font-weight: normal;"> $_POST['drop_down_name'] (if method is post)</span></div>
<div>
$_GET['drop_down_name'] (if method is gett)</div>
<div>
$_REQUEST['drop_down_name'] (if method is either post or get)</div>
<div>
The following PHP script describes how to retrieve input data from drop down box while form submitting.</div>
<div>
<br /></div>
<div class="bdr">
<html><br />
<body><br />
<form action="#" method="post"><br />
<select name="country"><br />
<option value="USA">USA</option><br />
<option value="United Kingdom">United Kingdom</option><br />
<option value="France">France</option><br />
<option value="Russia">Russia</option><br />
<option value="India">India</option><br />
</select><br />
<input type="submit" name="submit" value="Submit"><br />
</form><br />
<?php
<br />
if(isset($_POST['submit']))
<br />
{<br />
$country=$_POST['country'];<br />
echo'You selected '.$country;<br />
}<br />
?><br />
</body><br />
</html>
</div>
<div>
<br /></div>
<div>
While submitting form with select country from drop down box, you can get selected country as output.<br />
<br />
<b>Related Post:</b><br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b><a href="http://www.phponwebsites.com/2014/03/php-mysql-insert-data-into-table.html" itemprop="relatedLink" title="Store form values into MySQL table using PHP">Store form values into MySQL table using PHP</a></b></div></div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-36220123810184421942015-06-17T13:08:00.002-05:002015-08-19T12:14:03.520-05:00Fibonacci series of given number in PHP<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" rbidi="on">
This blog describes about how to find Fibonacci series of given number in PHP. Fibonacci series is a sequence of number generated by adding previous two numbers. If the given number is 'n', then Factorial of 'n' is defined as 'Fn'. In mathematical terms, Fibonacci series for 'n' number looks like this:<br />
<br />
<b>Fn = F</b>n-1<b> + F</b>n-2<b>;</b><br />
<br />
Where,<br />
<b> F0 = 0</b>;<br />
<b>F</b>1<b> = 1</b>;<br />
<br />
Fibonacci series for 'n' number is calculated recursively by above formula.<br />
<br />
<h3>
Simple PHP program Fibonacci series:</h3>
<br />
This is the simple PHP program for find Fibonacci series.<br />
<br />
<br />
<div class="bdr">
function fibonacci_series($n) {<br />
$f1 = -1;<br />
$f2 = 1;<br />
<br />
for ($i = 1; $i <= $n; $i++) {<br />
$f = $f1 + $f2;<br />
$f1 = $f2;<br />
$f2 = $f;<br />
echo "$f<br />"; <br />
}<br />
}<br />
<br />
echo fibonacci_series(5);</div>
<br />
<br />
The above PHP program returns 0, 1, 1, 2, 3. As i said, Fibonacci series is a sequence of number generated by adding previous two numbers. In above program, we have generated Fibonacci series for 5.<br />
We have assumed $f1 = -1; and $f2 = 1; We need to run for loop until 5.As per above program,<br />
<br />
<b>If $i = 1:</b><br />
$f = -1 + 1 = 0;<br />
$f1 = 1;<br />
$f2 = 0;<br />
<br />
<b>If $i = 2:</b><br />
$f = 1 + 0 = 1;<br />
$f1 = 0;<br />
$f2 = 1;<br />
<br />
<b>If $i = 3:</b><br />
$f = 0 + 1 = 1;<br />
$f1 = 1;<br />
$f2 = 1;<br />
<br />
<b>If $i = 4:</b><br />
$f = 1 + 1 = 2;<br />
$f1 = 1;<br />
$f2 = 2;<br />
<br />
<b>If $i = 5:</b><br />
$f = 1 + 2 = 3;<br />
$f1 = 2;<br />
$f2 = 3;<br />
<br />
<h3>
Fibonacci series for 'n' number in PHP:</h3>
<br />
The below PHP program is used to find Fibonacci series of given number.<br />
<br />
<br />
<div class="bdr">
<html><br />
<head><br />
<style type="text/css"><br />
body {<br />
color:white;<br />
font-size:14px;<br />
}<br />
.contact {<br />
text-align:center;<br />
background: none repeat scroll 0% 0% #8FBF73;<br />
padding: 20px 10px;<br />
box-shadow: 1px 2px 1px #8FBF73;<br />
border-radius: 10px;<br />
width:510px;<br />
}<br />
#number {<br />
width: 250px;<br />
margin-bottom: 15px;<br />
background: none repeat scroll 0% 0% #AFCF9C;<br />
border: 1px solid #91B57C;<br />
height: 30px;<br />
color: #808080;<br />
border-radius: 8px;<br />
box-shadow: 1px 2px 3px;<br />
padding: 3px 4px;<br />
}<br />
#submit<br />
{<br />
background:none repeat scroll 0% 0% #8FCB73;<br />
display: inline-block;<br />
padding: 5px 10px;<br />
line-height: 1.05em;<br />
box-shadow: 1px 2px 3px #8FCB73;<br />
border-radius: 8px;<br />
border: 1px solid #8FCB73;<br />
text-decoration: none;<br />
opacity: 0.9;<br />
cursor: pointer;<br />
color:white;<br />
}<br />
#er {<br />
color: #F00;<br />
text-align: center;<br />
margin: 10px 0px;<br />
font-size: 17px;<br />
}<br />
</style><br />
</head><br />
<body><br />
<div class="contact"><br />
<h1>Fibonacci series in PHP</h1><br />
<form action="#" method="POST"><br />
Enter number : <input type="text" name="number" id="number" /></br><br />
<input type="submit" name="submit" id="submit" value="Submit"></input><br />
</form><br />
<br />
<?php<br />
if(isset($_POST['submit'])) {<br />
$n = $_POST['number'];<br />
<br />
function fibonacci_series($a) {<br />
$f1 = -1;<br />
$f2 = 1;<br />
<br />
for ($i = 1; $i <= $a; $i++) {<br />
$f = $f1 + $f2;<br />
$f1 = $f2;<br />
$f2 = $f;<br />
echo "$f<br />"; <br />
}<br />
}<br />
<br />
echo "Fibonacci series of $n is:<br />";<br />
echo fibonacci_series($n);<br />
<br />
}<br />
<br />
<br />
?><br />
</div><br />
</body> <br />
</html></div>
<br />
<br />
<div>
When you open above PHP file in browser, it looks like below image:</div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-u9vfZ9T0aSo/VYGyj9E5znI/AAAAAAAAAmk/FNV3L5KaVdc/s1600/phponwebsites-fibonacci-series-php.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Find Fibonacci series for Given number in PHP" border="0" src="http://3.bp.blogspot.com/-u9vfZ9T0aSo/VYGyj9E5znI/AAAAAAAAAmk/FNV3L5KaVdc/s1600/phponwebsites-fibonacci-series-php.png" title="Find Fibonacci series for Given number in PHP" /></a></div>
<br />
<br />
If you type number 6 in textbox, it returns 0, 1, 1, 2, 3, 5. It looks like below image:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-XyGkpO2THr8/VYGzJAPDENI/AAAAAAAAAms/4LkTrCSqVnQ/s1600/php-fibinacci-series-phponwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Simple Fibonacci series program in PHP" border="0" src="http://3.bp.blogspot.com/-XyGkpO2THr8/VYGzJAPDENI/AAAAAAAAAms/4LkTrCSqVnQ/s1600/php-fibinacci-series-phponwebsites.png" title="Simple Fibonacci series program in PHP" /></a></div>
<br />
<br />
Now you can find Fibonacci series for any number in PHP. I've hope, you got an idea for generate Fibonacci series of given number.<br />
<br />
<br /></div>
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related Post:</b><br />
<b><a href="http://www.phponwebsites.com/2015/06/php-factorial-of-given-number.html" itemprop="relatedLink">PHP program for Factorial number</a></b><br/>
<b><a href="http://www.phponwebsites.com/2015/08/php-armstrong-numbers-between-1-to-1000.html" itemprop="relatedLink">Find Armstrong Number between 1 and 1000 in PHP</a></b>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com1tag:blogger.com,1999:blog-1892780101239299215.post-84544243636525029282015-06-16T12:47:00.000-05:002015-08-19T12:14:24.071-05:00Factorial of given number in PHP<div dir="ltr" style="text-align: left;" trbidi="on">
This blog describes about how to find factorial of given number in PHP. The factorial is one of the maths concept. It is multiplication of numbers from 1 to given number. If the given number defined as 'n', then 'n' factorial represented as 'n!'. The factorial formula for 'n' number looks like:<br />
<br />
<b>n! = 1 * 2 * 3 ... n;</b><br />
<br />
For example, the factorial of 5 looks like this:<br />
<br />
<b>5! = 1 * 2 * 3 * 4* 5 </b><b> = 120;</b><br />
<br />
<h3>
Simple PHP program for Factorial:</h3>
<br />
Consider the below example which is the sample PHP program for find factorial of number:
<br />
<br />
<div class="bdr">
function factorial($a) {<br />
$fact = 1;<br />
for($i = 1; $i <= $a; $i++) {<br />
$fact = $fact * $i;<br />
}<br />
return $fact;<br />
}<br />
$f = factorial(4);<br />
echo $f;</div>
<br />
Where,<br />
'n' is 4.<br />
n! = 4! = 1 * 2 * 3 * 4 = 24.<br />
<br />
<h3>
Factorial for 'n' number in PHP:</h3>
<br />
<div>
Consider the below example which is the PHP program for find factorial of given number. For example, if you enter 4, it will return factorial of 4. Like if you enter 'n' number, it will return factorial of 'n'.</div>
<br />
<br />
<div>
<div class="bdr">
<html>
<br />
<div>
<head></div>
<div>
<style type="text/css"></div>
<div>
body {</div>
<div>
color:white;</div>
<div>
font-size:14px;</div>
<div>
}</div>
<div>
.contact {</div>
<div>
text-align:center;</div>
<div>
background: none repeat scroll 0% 0% #8FBF73;</div>
<div>
padding: 20px 10px;</div>
<div>
box-shadow: 1px 2px 1px #8FBF73;</div>
<div>
border-radius: 10px;</div>
<div>
width:520px;</div>
<div>
}</div>
<div>
#number {</div>
<div>
width: 250px;</div>
<div>
margin-bottom: 15px;</div>
<div>
background: none repeat scroll 0% 0% #AFCF9C;</div>
<div>
border: 1px solid #91B57C;</div>
<div>
height: 30px;</div>
<div>
color: #808080;</div>
<div>
border-radius: 8px;</div>
<div>
box-shadow: 1px 2px 3px;</div>
<div>
padding: 3px 4px;</div>
<div>
}</div>
<div>
#submit</div>
<div>
{</div>
<div>
background:none repeat scroll 0% 0% #8FCB73;</div>
<div>
display: inline-block;</div>
<div>
padding: 5px 10px;</div>
<div>
line-height: 1.05em;</div>
<div>
box-shadow: 1px 2px 3px #8FCB73;</div>
<div>
border-radius: 8px;</div>
<div>
border: 1px solid #8FCB73;</div>
<div>
text-decoration: none;</div>
<div>
opacity: 0.9;</div>
<div>
cursor: pointer;</div>
<div>
color:white;</div>
<div>
}</div>
<div>
#er {</div>
<div>
color: #F00;</div>
<div>
text-align: center;</div>
<div>
margin: 10px 0px;</div>
<div>
font-size: 17px;</div>
<div>
}</div>
<div>
</style></div>
<div>
</head></div>
<div>
<body> </div>
<div>
<div class="contact"></div>
<div>
<h1>Factorial in PHP</h1></div>
<div>
<form action="#" method="POST"></div>
<div>
Enter number : <input type="text" name="number" id="number" /></br></div>
<div>
<input type="submit" name="submit" id="submit" value="Submit"></input></div>
<div>
</form></div>
<div>
<br /></div>
<div>
<?php</div>
<div>
if(isset($_POST['submit'])) {</div>
<div>
$n = $_POST['number'];</div>
<div>
</div>
<div>
function factorial($a) {</div>
<div>
$fact = 1;</div>
<div>
for($i = 1; $i <= $a; $i++) {</div>
<div>
$fact = $fact * $i;</div>
<div>
}</div>
<div>
return $fact;</div>
<div>
}</div>
<div>
$f = factorial($n);</div>
<div>
echo "<div class=''>Factorial of $n is $f</div>";</div>
<div>
}</div>
<div>
?></div>
<div>
</div></div>
<div>
</body> </div>
</html></div>
<br /></div>
<br />
<div>
When you open above program in browser, it looks like this:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-a3XEoiESrQQ/VYBc3LTg3zI/AAAAAAAAAmM/CUWjyjeyPiw/s1600/phponwebsites-factorials-php.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Simple PHP program for Factorials" border="0" src="http://4.bp.blogspot.com/-a3XEoiESrQQ/VYBc3LTg3zI/AAAAAAAAAmM/CUWjyjeyPiw/s1600/phponwebsites-factorials-php.png" title="Factorials in PHP" /></a></div>
<br />
<br />
<div>
Whatever you enter number in the textbox, it returns factorial of given number.</div>
<br />
<br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related Post:</b><br />
<b><a href="http://www.phponwebsites.com/2015/06/php-fibonacci-series-of-given-number.html" itemprop="relatedLink">PHP program for Fibonacci series</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/08/php-armstrong-numbers-between-1-to-1000.html" itemprop="relatedLink">Find Armstrong Number between 1 and 1000 in PHP</a></b>
</div>
</div>Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com3tag:blogger.com,1999:blog-1892780101239299215.post-18416415488212341002015-06-04T10:47:00.000-05:002015-06-04T10:47:31.407-05:00Ajax upload file or image in PHP using jQuery<div dir="ltr" style="text-align: left;" trbidi="on">
You can upload files and images using PHP. Basically it can be uploaded by form submitting. For more details visit<b> <a href="http://www.phponwebsites.com/2014/10/php-file-upload.html">how to upload file or image using PHP</a></b>. When i tried to upload images using Ajax with jQuery, i can't get it work. All of you may be face this problem. This blog describes how to upload file or image in PHP using jQuery with Ajax.<br />
<br />
Consider the following example for upload files using Ajax and PHP.<br />
<br />
<h3>
<span style="font-weight: normal;">
Html form for upload image:</span></h3>
<div>
<br />
To upload file or image, your html form should be like below form:<br />
<br /></div>
<div class="bdr">
<form name="multiform" id="multiform" action="#" method="POST" enctype="multipart/form-data"><br />
Image : <input type="file" name="file" id="file" /></br><br />
<input type="button" id="multi-post" value="Upload"></input><br />
</form></div>
<br />
where,<br />
<b class="bkg">enctype="multpart/form-data</b> - must be present in form. Otherwise file does not upload.<br />
<b class="bkg">type="file"</b> in <input> tag is used to take input as a file.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Ajax and jQuery for upload image:</span></h3>
<div>
<br />
Before add Ajax code to your html file, you need to add <b>jquery-1.7.2.min.js</b> library file. To upload image, you have to write Ajax code like below:</div>
<div>
<br /></div>
<div class="bdr">
<div>
$(document).ready(function(){</div>
<div>
$("#multiform").submit(function(e) {</div>
<div>
var formObj = $(this);</div>
<div>
</div>
<div>
if(window.FormData !== undefined) {</div>
<div>
var formData = new FormData(this);</div>
<div>
$.ajax({</div>
<div>
url: 'upload.php', //url to process the sent data</div>
<div>
type: 'POST', //form type</div>
<div>
data: formData, //form data to send</div>
<div>
mimeType:"multipart/form-data", //type of form data</div>
<div>
contentType: false, //content type of form</div>
<div>
cache: false, //disable cache the request page</div>
<div>
processData: false, //to send DOMDcoument or non processed data file</div>
<div>
success: function(data, textStatus, jqXHR) {</div>
<div>
$("#er").html(data);</div>
<div>
},</div>
<div>
error: function(jqXHR, textStatus, errorThrown) {</div>
<div>
$("#er").html('textStatus=' + textStatus + ', errorThrown=' + errorThrown);</div>
<div>
} </div>
<div>
});</div>
<div>
<br />
//reset file field after form submitted</div>
<div>
$('#file').val('')<br />
//prevent the page is refreshed</div>
<div>
e.preventDefault();</div>
<div>
//e.unbind();</div>
<div>
}</div>
<div>
});</div>
<div>
<br /></div>
<div>
$("#multi-post").click(function() {</div>
<div>
//submit the form and send data</div>
<div>
$("#multiform").submit();</div>
<div>
});</div>
<div>
<br /></div>
<div>
});</div>
</div>
<div>
<br />
<br />
When user click the upload button, the form is submitted, ajax is called and send form data as well.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Html file with Ajax for upload image:</span></h3>
<div>
<br /></div>
<div>
We need to combine html form and ajax code to make a full html file for upload file or image. You may add some styles to html file in order to give attractive presence to users. After combined above all things, your html code should be like below one:<br />
<br />
<b>upload-ajax.html:</b></div>
<div>
<br /></div>
<div class="bdr">
<div>
<html></div>
<div>
<head></div>
<div>
<style type="text/css"></div>
<div>
body {</div>
<div>
color:white;</div>
<div>
font-size:14px;</div>
<div>
}</div>
<div>
.contact {</div>
<div>
text-align:center;</div>
<div>
background: none repeat scroll 0% 0% #8FBF73;</div>
<div>
padding: 20px 10px;</div>
<div>
box-shadow: 1px 2px 1px #8FBF73;</div>
<div>
border-radius: 10px;</div>
<div>
width:520px;</div>
<div>
}</div>
<div>
#file {</div>
<div>
width: 250px;</div>
<div>
margin-bottom: 15px;</div>
<div>
background: none repeat scroll 0% 0% #AFCF9C;</div>
<div>
border: 1px solid #91B57C;</div>
<div>
height: 30px;</div>
<div>
color: #808080;</div>
<div>
border-radius: 8px;</div>
<div>
box-shadow: 1px 2px 3px;</div>
<div>
padding: 3px 4px;</div>
<div>
}</div>
<div>
#multi-post</div>
<div>
{</div>
<div>
background:none repeat scroll 0% 0% #8FCB73;</div>
<div>
display: inline-block;</div>
<div>
padding: 5px 10px;</div>
<div>
line-height: 1.05em;</div>
<div>
box-shadow: 1px 2px 3px #8FCB73;</div>
<div>
border-radius: 8px;</div>
<div>
border: 1px solid #8FCB73;</div>
<div>
text-decoration: none;</div>
<div>
opacity: 0.9;</div>
<div>
cursor: pointer;</div>
<div>
color:white;</div>
<div>
}</div>
<div>
#er {</div>
<div>
color: #F00;</div>
<div>
text-align: center;</div>
<div>
margin: 10px 0px;</div>
<div>
font-size: 17px;</div>
<div>
}</div>
<div>
</style></div>
<div>
</head></div>
<div>
<body> </div>
<div>
<script type="text/javascript" src="../test/js/jquery-1.7.2.min.js"></script></div>
<div>
<script type="text/javascript"></div>
<div>
$(document).ready(function(){</div>
<div>
$("#multiform").submit(function(e) {</div>
<div>
var formObj = $(this);</div>
<div>
</div>
<div>
if(window.FormData !== undefined) {</div>
<div>
var formData = new FormData(this);</div>
<div>
$.ajax({</div>
<div>
url: 'upload.php',</div>
<div>
type: 'POST',</div>
<div>
data: formData,</div>
<div>
mimeType:"multipart/form-data",</div>
<div>
contentType: false,</div>
<div>
cache: false,</div>
<div>
processData:false,</div>
<div>
success: function(data, textStatus, jqXHR) {</div>
<div>
$("#er").html('<pre><code>'+data+'</code></pre>');</div>
<div>
},</div>
<div>
error: function(jqXHR, textStatus, errorThrown) {</div>
<div>
$("#er").html('textStatus=' + textStatus + ', errorThrown=' + errorThrown);</div>
<div>
} </div>
<div>
});</div>
<div>
<br /></div>
<div>
$('#file').val('')</div>
<div>
e.preventDefault();</div>
<div>
//e.unbind();</div>
<div>
}</div>
<div>
});</div>
<div>
<br /></div>
<div>
$("#multi-post").click(function() {</div>
<div>
//sending form from here</div>
<div>
$("#multiform").submit();</div>
<div>
console.log();</div>
<div>
});</div>
<div>
<br /></div>
<div>
});</div>
<div>
<br /></div>
<div>
</script></div>
<div>
<div class="contact"></div>
<div>
<h1>Upload files using Ajax and PHP</h1></div>
<div>
<div id="er"></div></div>
<div>
<form name="multiform" id="multiform" action="#" method="POST" enctype="multipart/form-data"></div>
<div>
Image : <input type="file" name="file" id="file" /></br></div>
<div>
<input type="button" id="multi-post" value="Upload"></input></div>
<div>
</form></div>
<div>
</div></div>
<div>
</body> </div>
<div>
</html></div>
<div>
<br /></div>
</div>
<br />
When you view the upload-ajax.html in browser, it should be like below image:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-VYHaCcy-otw/VW9Hv4SwyWI/AAAAAAAAAlo/UdxytTKgBXQ/s1600/ajax-upload-image-php-jquery.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="How to upload images using PHP, and Ajax with jQuery" border="0" src="http://1.bp.blogspot.com/-VYHaCcy-otw/VW9Hv4SwyWI/AAAAAAAAAlo/UdxytTKgBXQ/s1600/ajax-upload-image-php-jquery.png" title="How to upload images using PHP, and Ajax with jQuery" /></a></div>
<br />
<br />
<h3>
<span style="font-weight: normal;">PHP snippet for upload file:</span></h3>
<br />
<div>
When ajax is called, url which is in ajax call received form data. Then the file or image is to be uploaded using following PHP code.</div>
<br />
<b>upload.php:</b>
<br />
<br />
<div class="bdr">
<div>
<?php</div>
<div>
$file=$_FILES['file']['name'];</div>
<div>
if($file!='')</div>
<div>
{</div>
<div>
//upload file</div>
<div>
move_uploaded_file($_FILES['file']['tmp_name'],$_SERVER['DOCUMENT_ROOT'].'/test/uploaded_img/'.$file);</div>
<div>
//check whether file is exists or not in a folder</div>
<div>
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/test/uploaded_img/'.$file)) {</div>
<div>
echo'Uploaded successfully';</div>
<div>
}</div>
<div>
else {</div>
<div>
echo'File is not uploaded';</div>
<div>
}</div>
<div>
}</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="font-weight: normal;"> Where,</span></div>
<div>
<b class="bkg">move_uploaded_file()</b> - used to upload the file</div>
<div>
<b class="bkg">file_exists()</b> - used to check whether the file is already exists in a folder or not after upload the file</div>
<div>
<b class="bkg">$_SERVER['DOCUMENT_ROOT']</b> - used to represent the directory </div>
<div>
<br /></div>
<div>
Now you can add either file or image using PHP and jQuery with Ajax.<br />
<br /></div>
</div>
<div itemscope="" itemtype="http://schema.org/WebPage">
<b>Related Post:</b><br />
<b><a href="http://www.phponwebsites.com/2014/10/php-file-upload.html" itemprop="relatedLink">PHP - Upload file</a></b><br />
<b><a href="http://www.phponwebsites.com/2014/10/php-mysql-upload-images-to-database.html" itemprop="relatedLink">PHP - Upload images into MySQL database</a></b><br />
<a href="http://www.phponwebsites.com/2015/06/php-upload-multiple-files-images.html" itemprop="relatedLink"><b>Upload multiple files or images using PHP</b></a><br />
<b><a href="http://www.phponwebsites.com/2015/03/php-mysql-retrieve-images-from-database.html" itemprop="relatedLink">Retrieve images from MySQL database using PHP</a></b><br />
<br /></div>
<div>
<br /></div>
</div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com1tag:blogger.com,1999:blog-1892780101239299215.post-22514444167815606962015-06-01T11:52:00.001-05:002015-06-04T12:32:12.273-05:00Upload multiple files/images using PHP<div dir="ltr" style="text-align: left;" trbidi="on">
You can upload single file and image easily in PHP. Now we are going to know how to upload multiple files and images using PHP. You can upload more than one images or files at the time using PHP. It can done using by array concepts.<br />
<br />
<h3 style="text-align: left;">
<span style="font-weight: normal;">Form for upload multiple images:</span></h3>
<div>
<span style="font-weight: normal;"> </span></div>
<div>
<span style="font-weight: normal;"> First you need to make form for upload more images. Your form should be like following:</span></div>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div class="bdr">
<form action="#" method="post" enctype="multipart/form-data"><br />
<input type="file" name="file[]" id="file" multiple="multiple"><br />
<input type="file" name="file[]" id="file" multiple="multiple"><br />
<input type="submit" id="submit" name="submit" value="submit"><br />
</form>
</div>
<div>
<br /></div>
<div>
You should add <b>enctype="multipart/form-data"</b> for upload files. You can see above where the name of file tag should be array like <b>name="file[]"</b> for store multiple values.</div>
<div>
<br /></div>
<h3 style="text-align: left;">
<span style="font-weight: normal;">Upload multiple images using PHP:</span></h3>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div>
<span style="font-weight: normal;"> </span> Similarly upload single image, you will get image values by <b>$_FILES['file']['name'] </b>in PHP while submitting a form. You need temporary name of image to pass one of the parameter in <b>move_uploaded_file() </b>in order to upload files. If you've a single image, then you can upload easily by <b>move_uploaded_file($_FILES['file']['tmp_name'],$name)</b>. But you need to upload multiple images. So you've to get values as an array. Then upload each image using loop. Consider the following example. Here it describes how to get temporary name of each image one by one using loop in PHP.<br />
<br /></div>
<div class="bdr">
<br />
<?php<br />
if(isset($_POST['submit']))<br />
{<br />
$name=$_FILES['file']['name'];<br />
foreach($name as $val => $name1)<br />
{<br />
$file_name=$name1;<br />
$tmp=$_FILES['file']['tmp_name'][$val];<br />
move_uploaded_file($tmp,$_SERVER['DOCUMENT_ROOT'].'img/'.$file_name);<br />
}<br />
}<br />
<br />
?><br />
<html><br />
<body><br />
<form action="#" method="post" enctype="multipart/form-data"><br />
<table><br />
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr><br />
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr><br />
<tr><td><input type="submit" id="submit" name="submit" value="submit"></td></tr><br />
</table><br />
</form><br />
</body><br />
</html></div>
<br />
<br />
<br />
Where,<br />
<b>move_uploaded_file()</b> - to upload files to desired location.<br />
<b>$_SERVER['DOCUMENT_ROOT']</b> gives the root directory of current script executing.<br />
You can upload multiple files as well as images using this PHP script.<br />
<br />
<b>Related Posts:</b><br />
<div itemscope="" itemtype="http://schema.org/WebPage">
<b><a href="http://www.phponwebsites.com/2015/05/php-multiple-checkbox-values.html" itemprop="relatedLink">How to get multiple checked checkbox values in PHP</a></b><br />
<a href="http://www.phponwebsites.com/2014/10/php-file-upload.html" itemprop="relatedLink"><b>How to upload image or file in PHP</b></a><br />
<b><a href="http://www.phponwebsites.com/2014/10/php-mysql-upload-images-to-database.html" itemprop="relatedLink">How to store image path into MySQL database in PHP</a></b><br />
<b><a href="http://www.phponwebsites.com/2015/06/ajax-jquery-php-upload-file-image.html" itemprop="relatedLink">How to Upload images using Ajax with jQuery in PHP</a></b><br />
<a href="http://www.phponwebsites.com/2015/03/php-mysql-retrieve-images-from-database.html" itemprop="relatedLink"><b>Display images from MySQL database in PHP</b></a></div>
<br />
<br /></div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0tag:blogger.com,1999:blog-1892780101239299215.post-58148329043183129102015-05-13T11:26:00.000-05:002015-05-13T11:28:00.987-05:00Get multiple values from checked checkboxes using PHP<div dir="ltr" style="text-align: left;" trbidi="on">
Normally you can get single value from textbox, checkbox and radio buttons. Can you retrieve multiple values from selected checkbox.? Yes you can get multiple values from checked checkbox using PHP.<br />
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
Get value from checked checkbox using PHP:</h3>
<div>
<br />
You can retrieve value from single checkbox using php and html. The following php code is used for get values from checked checkbox.
<br />
<br />
<div class="bdr">
<div>
<html></div>
<div>
<body></div>
<div>
<div>
<?php</div>
<div>
if(isset($_POST['submit']))</div>
<div>
{</div>
<div>
echo'You checked following value:<br>';<br />
echo $_POST['check'];<br />
}</div>
<div>
else<br />
{</div>
<div>
?></div>
<form action="#" method="post"></div>
<div>
<input type="checkbox" name="check" value="apple">apple <br></div>
<div>
<input type="submit" name="submit" value="submit"> <br></div>
<div>
</form></div>
<div>
<?php<br />
}<br />
?></div>
<div>
</body></div>
<div>
</html></div>
</div>
</div>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
<br /></h3>
<h3 style="text-align: left;">
Retrieve values from multiple checked checkboxes using php:</h3>
<div>
<br />
If you want to get multiple values from checked checkbox, then you use name as an array for your checkbox. It should be like following.<br />
<div>
<br />
<div>
<b class="bkg"><input type="checkbox" name="check[]" value="apple">apple</b></div>
<div>
<b class="bkg"><input type="checkbox" name="check[]" value="orange">orange</b></div>
<div>
<b class="bkg"><input type="checkbox" name="check[]" value="mango">mango</b></div>
</div>
<br />
The following php code is used to get values from multiple checked checkbox.</div>
<div>
<br />
<br /></div>
<div class="bdr">
<div>
<html></div>
<div>
<body></div>
<div>
<div>
<?php</div>
<div>
if(isset($_POST['submit']))</div>
<div>
{</div>
<div>
echo'You checked following value(s):<br>';<br />
foreach($_POST['check'] as $val)</div>
<div>
{</div>
<div>
echo $val.'<br>';</div>
<div>
}</div>
<div>
}<br />
else<br />
{</div>
<div>
?></div>
<form action="#" method="post"></div>
<div>
<input type="checkbox" name="check[]" value="apple">apple <br></div>
<div>
<input type="checkbox" name="check[]" value="orange">orange <br></div>
<div>
<input type="checkbox" name="check[]" value="mango">mango <br></div>
<div>
<input type="submit" name="submit" value="submit"> <br></div>
<div>
</form></div>
<div>
<?php<br />
}<br />
?></div>
<div>
</body></div>
<div>
</html></div>
</div>
<div>
<br /></div>
<br />
where ,<br />
<b class="bkg">$_POST['check']</b> is used to get values from checkbox using php.<br />
<b class="bkg">foreach()</b> is used to get array values from checkbox and display them<br />
<br />
When you run this php file, your output like as below:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-jUbLd-P-bbg/U0QojWpffVI/AAAAAAAAAek/vlBEk2nR8Ws/s1600/checkbox.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="get values from multiple check box" border="0" src="http://4.bp.blogspot.com/-jUbLd-P-bbg/U0QojWpffVI/AAAAAAAAAek/vlBEk2nR8Ws/s1600/checkbox.png" title="get values from multiple check box" /></a></div>
<br />
Suppose, you select check boxes like below:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-Gb-tGs0aXao/U0QolfATKXI/AAAAAAAAAew/DMZ8gWd7F7Y/s1600/checked_checkbox.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="get values from checked checkbox using php" border="0" src="http://2.bp.blogspot.com/-Gb-tGs0aXao/U0QolfATKXI/AAAAAAAAAew/DMZ8gWd7F7Y/s1600/checked_checkbox.png" title="get values from checked checkbox using php" /></a></div>
<br />
You'll get output like as below<br />
<br />
You checked following value(s):<br />
apple<br />
mango<br />
<br />
<br /></div>
Guruhttp://www.blogger.com/profile/00326436856590034534noreply@blogger.com0