Array to string conversion php form_helper error - php

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: helpers/form_helper.php
Line Number: 522
Here is my view that contains form
<?php
// open the form using CI form helper
echo form_open('loginController', array('id' => 'login'));
// first input field
echo "<div>";
echo form_label('Username', array('for' => 'login_username'));
echo form_input(array(
'type' => 'text',
'name' => 'username',
'id' => 'login_username',
'value' => set_value('username')
));
echo "</div>";
// second input field
echo "<div>";
echo form_label('Password', array('for' => 'login_password'));
echo form_password(array('type' => 'password', 'name' => 'password', 'id' => 'login_password'));
echo "</div>";
// submit button
echo "<div class='submit'>";
echo form_button(array('type' => 'submit', 'content' => 'Log in'));
echo "</div>";
echo "<div class='errors'>";
// display if incorrect username/password
if ($this->session->flashdata('login_error'))
echo 'Incorrect username/password';
// display if incorrect input data
echo validation_errors();
echo "</div>";
// close the form
echo form_close();
?>
Here is form_helper.php (line 513 to 537) that is mentioned in PHP Error
if ( ! function_exists('form_label'))
{
function form_label($label_text = '', $id = '', $attributes = array())
{
$label = '<label';
if ($id != '')
{
$label .= " for=\"$id\"";
}
if (is_array($attributes) AND count($attributes) > 0)
{
foreach ($attributes as $key => $val)
{
$label .= ' '.$key.'="'.$val.'"';
}
}
$label .= ">$label_text</label>";
return $label;
}
}

Here
echo form_label('Username', array('for' => 'login_username'));
and here
echo form_label('Password', array('for' => 'login_password'));
You are passing an array for the id parameter of the function that should be a string try
echo form_label('Username', "lblUsername", array('for' => 'login_username'));
and
echo form_label('Username', "lblPassword", array('for' => 'login_username'));
Or simply pass null in as the value of the id.

You are calling function in wrong way. The 3rd parameter is array but you are passing 2nd parameter as array.
Use below kind of function call for all form_label.
echo form_label('Username', 'Username',array('for' => 'login_username'));
^^^^^^^^^^^^
Function definition
function form_label($label_text = '', $id = '', $attributes = array())

Related

Wordpress Not Saving Custom Post Input Fields

I am trying to save a custom post type. I can't figure out why the save isn't working for the input fields listed below.
I included all the relevant code, as I can't see where the problem might be. It seems like everything is working except for the save function (listed last).
I am only trying to save inputs where 'type' => 'singleinput'. I haven't tried to save the 'fulltext' ones.
Can anyone point out where I am going wrong?
//input boxes for custom post
$specialpage->inputs = array(
'item1' => array(
'type' => 'singleinput',
'headline' => 'Page headline',
'bullet1' => 'Bullet Point 1:',
'bullet2' => 'Bullet Point 2:',
'bullet3' => 'Bullet Point 3:'
),
'item2' => array(
'type' => 'fulltext',
'promo1' => 'Promo text 1:',
'promo2' => 'Promo text 2:',
'promo3' => 'Promo text 3:'
),
'item3' => array(
'type' => 'singleinput',
'quote' => 'The quote'
)
);
This function is called to display the meta box.
public function display_meta_box_($post_object){
foreach($this->inputs as $item){
foreach($item as $name => $title){
if ($title == 'singleinput'){
$activate = 'singleinput';
continue;
}
if ($title == 'fulltext'){
$activate = 'fulltext';
continue;
}
if ($activate == 'singleinput'){
$this->singleinput[$name] = $title;
${$this->name . $name} = get_post_meta($post_object->ID, $this->name . $name, true);
echo '<span class="meta_titles">' . $title . '</span>';
?>
<input type='text' class='input_single' name='<?php echo $this->name . $name;?>_name' value='<?php echo ${$this->name . $name}; ?>' >
<?php
}
}
This is the save function
public function save_profile( $post_id, $post_object ) {
if( $this->name == $post_object->post_type){
//check if singleinput are defined for this custom post type
if ($this->singleinput){
//save single line text input boxes
foreach ($this->singleinput as $name => $title){
update_post_meta($post_id, $this->name . $name, $_POST[$this->name . $name . '_name']);
}
}
}
Edit: Added Save Function
$simplejoiner->saveNow();
public function saveNow(){
add_action( 'save_post', array($this, 'save_profile'), 10, 2 );
}

Echo value in array_push

I have this code:
include "xmlapi.php";
$pass = 'testing1234';
$auser = 'testing';
$server = "0.0.0.0";
$port = 2087;
$remote_api = new xmlapi($server);
$remote_api->password_auth($auser, $pass);
$remote_api->set_port($port);
$remote_api->set_output('json');
$json_list = $remote_api->xmlapi_query('listaccts', array( 'api.version'=> 1));
$list = json_decode($json_list, true);
if ( ! is_array($list)
|| ! array_key_exists('data', $list)
|| ! is_array($list['data'])
|| ! array_key_exists('acct', $list['data'])
) {
die("Invalid response!");
}
$email_list = array();
foreach ($list['data']['acct'] as $acct) {
$username = $acct['user'];
$json_emails = $remote_api->api2_query($username, 'Email', 'listpopswithdisk', array());
$acct_emails = json_decode($json_emails, true);
if ( is_array($acct_emails)
&& array_key_exists('cpanelresult', $acct_emails)
&& is_array($acct_emails['cpanelresult'])
&& array_key_exists('data', $acct_emails['cpanelresult'])
&& is_array($acct_emails['cpanelresult']['data'])
) {
foreach ($acct_emails['cpanelresult']['data'] as $an_email) {
array_push(
$email_list,
array(
'cpanel_account' => $username,
'domain' => $an_email['domain'],
'email' => $an_email['user'],
'full_email' => $an_email['email'],
'diskused' => $an_email['diskused'],
)
);
echo $an_email['email'] . ' - ';
echo $an_email['diskused'] . ' - ';
echo $an_email['user'] . '<br>';
}
}
}
I used this code to connect to my cpanel and list all the email on the server.
At first I just use var_dump to see if it works. And tried to use foreach loop but not success.
How can I do a loop to echo all the data, so I can put the data in a table or something like that.
Please help me with this, thank you.
You need to parse the array containing the email accounts and print into a <table>
Try to adapt this code:
$email_list = [
['email' => 'first#email.com', 'diskused' => 'the diskused', 'user' => 'the first user'],
['email' => 'second#email.com', 'diskused' => 'the diskused', 'user' => 'the second user'],
['email' => 'third#email.com', 'diskused' => 'the diskused', 'user' => 'the third user'],
];
if (count($email_list)) {
print '<table>';
/* Set the header fields to print and Titles */
$headers = [
'email' => 'Email',
'diskused' => 'Diskused',
'user' => 'User'
];
/* Print table headers */
print '<tr>';
foreach ($headers as $headerTitle) {
print '<th>'.$headerTitle.'</th>';
}
print '</tr>';
/* Parse all rows */
foreach ($email_list as $row) {
print '<tr>';
/* Print all values */
foreach (array_keys($headers) as $headerKey) {
print '<td>'.$row[$headerKey].'</td>';
}
print '</tr>';
}
print '</table>';
}

How to auto increment in sub foreach loop?

Here is the example code:
<?php
$arr = array(
array(
'company' => array(
'code' => 'ccd1',
'name' => 'cnm1'
) ,
'products' => array(
array(
'code' => 'pcd1',
'name' => 'pnm1'
) ,
array(
'code' => 'pcd2',
'name' => 'pnm2'
)
)
) ,
array(
'company' => array(
'code' => 'ccd2',
'name' => 'cnm2'
) ,
'products' => array(
array(
'code' => 'pcd1',
'name' => 'pnm1'
) ,
array(
'code' => 'pcd2',
'name' => 'pnm2'
) ,
array(
'code' => 'pcd3',
'name' => 'pnm3'
)
)
)
);
echo "<pre>"; print_r($arr); echo "</pre>";
$AI = 1;
foreach($arr as $value){
$total_products = count($value['products']);
echo $AI++.".{$value['company']['name']} ({$total_products})<br />";
foreach($value['products'] as $value2){
echo " ".$value2['name']."<br />";
}
}
I don't know how to explain it, but what I want is to add auto increment in sub foreach loop, like this:
1.cnm1 (2)
1.pnm1
2.pnm2
2.cnm2 (3)
1.pnm1
2.pnm2
3.pnm3
Usually you can just use the foreach keys plus one. As another alternative, if this is just for presentation, just use ordered lists:
echo '<ol>';
foreach($arr as $ar1) {
echo '<li>' . $ar1['company']['name'] . ' (' . count($ar1['products']) . ')</li>';
echo '<ol>';
foreach($ar1['products'] as $ar2) {
echo "<li>{$ar2['name']}</li>";
}
echo '</ol>';
}
It'll number those items accordingly. No need for addition. Plus you can use CSS to style the list,
You can access the key/index of an foreach :
foreach($arr as $i => $value){
$total_products = count($value['products']);
echo ($i+1).'.'.$value['company']['name'].' ('.$total_products .')<br />';
foreach($value['products'] as $j => $value2){
echo ' '.($j+1).'.'.$value2['name'].'<br />';
}
}
Here you go.
$arr = array(array('company'=>array('code'=>'ccd1', 'name'=>'cnm1'), 'products'=>array(array('code'=>'pcd1', 'name'=>'pnm1'), array('code'=>'pcd2', 'name'=>'pnm2'))), array('company'=>array('code'=>'ccd2', 'name'=>'cnm2'), 'products'=>array(array('code'=>'pcd1', 'name'=>'pnm1'), array('code'=>'pcd2', 'name'=>'pnm2'), array('code'=>'pcd3', 'name'=>'pnm3'))));
echo "<pre>";
print_r($arr);
echo "</pre>";
$AI = 1;
foreach($arr as $value)
{
$total_products = count($value['products']);
echo $AI++.".{$value['company']['name']} ({$total_products})<br />";
$k = 0;
foreach($value['products'] as $value2)
{
echo " ".$k++.". ".$value2['name']."<br />";
}
}
This also worked for me:
foreach($value['products'] as $key2=>$value2){
$AI2 = $key2+1;
echo " ".$AI2.".{$value2['name']}<br />";
}
You can try this:
foreach($arr as $value){
$total_products = count($value['products']);
echo $AI++.".{$value['company']['name']} ({$total_products})<br />";
$sub=1;
foreach($value['products'] as $value2){
echo " ".$sub++.'.'.$value2['name']."<br />";
}
}

Yii dependant dropDownList

I need to create to dependant dropDownLists with yii. Thus, I create a view and an action in controller like this :
The view
<?php
echo "<div class='row'>";
echo $form->labelEx($model, 'id_structure');
echo $form->dropDownList($model, 'id_structure', GxHtml::listDataEx(StructureInformation::model()->findAllAttributes()),
array('ajax' => array('type' => 'POST',
'url' => CController::createUrl('InfoComplementAAjouter/fillTypeList'),
'update' => '#typeDonnee',
'data' => array('id_structure' => 'js:this.value'))
));
echo $form->error($model, 'id_structure');
echo '</div>';
echo "<div class='row'>";
echo $form->labelEx($model, 'type');
echo $form->dropDownList($model, 'type', array(), array('prompt' => 'Sélectionner', 'id' => 'typeDonnee'));
echo $form->error($model, 'type');
echo '<div>';
?>
Now the action in the Controller InfoComplementAAjouterController :
public function actionFillTypeList(){
$id_struct = $_POST['InfoComplementAAjouter']['id_structure']; //I get the selected value
$taille = StructureInformation::model()->find('id = ' . (int)$id_struct)->taille; //and then pass it to the StructureInformation model to obtain the attribute taille
//I create two arrays which content will be the options of my dropDownList.
$un = array('text'=> 'Texte', 'radio' => 'Bouton radio', 'dropDownList' => 'Liste déroulante');
$plusieurs = array( 'checkboxlist' => 'Choix multiple',);
//If $taille == 1, I display the contents of $un; if $taille > 1, the content of $plusieurs will be displayed.
if($taille == 1){
foreach ($un AS $value => $name){
$opt = array();
$opt['value'] = $value;
echo CHtml::tag('option', $opt, CHtml::encode($name), true);
}
}
if($taille > 1){
foreach ($plusieurs AS $value => $name){
$opt = array();
$opt['value'] = $value;
echo CHtml::tag('option', $opt, CHtml::encode($name), true);
}
}
die;
}
I remark that the action is not executed. I can not understand why.
Can somebody help me to fix the problem ?
I just saw a typo:
'upedate' => '#typeDonnee',
should be 'update'
try with fixing it first
Edit:
you have more small errors/typos
echo $form->dropDownList($model, 'id_structure', GxHtml::listDataEx(StructureInformation::model()->findAllAttributes(),
array('ajax' => array('type' => 'POST',
'url' => CController::createUrl('InfoComplementAAjouter/fillTypeList'),
'update' => '#typeDonnee',
'data' => array('id_structure' => 'js:this.value'))
));
here you need to close parentheses
=====================================================
StructureInformation::model()->findAllAttributes()
there is no findAllAttributes() method in CActiveRecord
maybe you wanted to use findAllByAttributes(), but then you would want to pass some attributes. If this is your custom method then you should explain how it works, or paste a code
Edit 2:
$_POST['InfoComplementAAjouter']['id_structure']
Try to access post like this:
$_POST['id_structure']
I think you send post in that structure
It's not that easy for me to duplicate your situation, so I'm kinda shooting in the dark

Coding information from an array in php into a document

I am trying to display the details for the inner array below but don't know how. I manage to get the right answer but with a warning saying array to string conversion how do I avoid the warning along with the answer:
$user = array(
'name' => 'Joe Blogs',
'age' => 25,
'validate' => 'true',
'children' => array(
'Jack',
'Jill',
'Mark'
)
);
foreach($user as $v){
echo $v . ' ' ."<br />";
}
foreach($user['children'] as $h){
echo "$h ";
}
Your first loop will still try to echo the value of $user['children'], but arrays can't simply be echoed.... prevent the original loop from trying to display the child array, and leave that to your second loop
foreach($user as $v){
if (!is_array($v) {
echo $v . ' ' ."<br />";
}
}
or
foreach($user as $key => $v){
if ($key !== 'children') {
echo $v . ' ' ."<br />";
}
}

Categories