declare an empty array of size 100 in codeigniter php - php

I am developing an application in php using codeigniter for a workflow process.
Now i need to declare an empty array of size 100 or 200 with null value default..whenever workflow executed anyways values get stored in to that array dynamically..
I am having a array in my view like
if(isset($value)){
foreach ($value as $row) {
$wer[] = $row;
}
}
Now whenever workflow runs automatically values get stored in view using form_dropdown('perfpmself_1',$rate,$wer[0],'class="self"');
from the above format i use to load all values from $wer[0] to $wer[50]. All values will displayed properly...but if there is no values in database to load it to an array..it is throwing an error like
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: wer
Filename: views/performancepm.php
Can anyone suggests a solution for this to overcome this error on my view screen??
Please help..thanks in advance

You didn't initialize $wer before trying to assign a value to it:
if(isset($value)){
$wer = array();
foreach ($value as $row) {
$wer[] = $row;
}
}

Try:
$options = array_fill_keys(range(0,99),null);
echo form_dropdown('perfpmself_1', $options);

Just define wer globally. Also I'd use array_push, but thats just me. Keep it simple:
$wer = array();
if(isset($value)){
foreach ($value as $row) {
array_push($wer, $row);
}
}
if(empty($wer)){
//wer has values
}else{
// wer is not empty
}
In regards to your comments, as far as I know there is only one way to enforce the required field specifically in the codeIgniter framework:
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
From the CodeIgniter Guide
Otherwise youll have to enforce it using JavaScript or in your own PHP code to see if is an empty field:
if (isset($_GET['username'])){
//username isnt blank
}else{
//username is blank
}

Related

Foreach loop that loops through $_POST data instead of static isset($_POST["name"]......)

So here I am again trying to find better ways of doing things. 90% of tutorials do things the normal way below:
if (isset($_POST['name']) && isset($_POST['password'])) {
// Does some stuff...
}
It is fine but it does seem too static since I prefer something far more dynamic. For example lets say looping through all $_POST arrays within a contact form. This way I can change the name or the fields to whatever I want or add more...my code will always handle the rest.
I know a foreach loop would come in handy but, as someone new to the world of programming and php I thought you could show me how something like this is done. So how do I replace the above with a for loop? I am not sure where to start.
try this:
$check=true;
if(isset($_POST)){
foreach($_POST as $key=>$value){
if(!isset($_POST[$key]){
$check = false;
break;
}
}
}
based on $check you can verify if it was properly sent or not.
Another approach is to have a sort of verification because it is possible you might not get the key in $_POST
$keys =array("input1","input2");
$check=true;
if(isset($_POST)){
foreach($keys as $input){
if(!array_key_exists($input,$_POST)){
$check = false;
break;
}
}
}
Well you could try something like this :-
<?php
$inputNames = array("input1","input2");
foreach($inputNames as $input)
{
if (isset($_POST["$input"]) && isset($_POST["$input"])) {
// Does some stuff...
}
}
?>
Make an array with the names of all your input tags, and then simply do a foreach between them. In this way, you always only need to edit the names array.
You can always use foreach loop like that:
foreach($_POST as $key => $value){ echo '$_POST["'.$key.'"] = "'.$value.'"'}
But remember, that anyone, can modify your form, prepare some post statement and send data that can create little mess with your code. So it`s good way to validate all fields.
Dynamic validation is of course possible, but you need to do it right!

PHP - Foreach look giving a warning

I made a login page and while navigating from one page to another I'd like to destroy the unnecessary sessions
$keepSessions = array('vendor_id','email_login','user_password','passport_id');
foreach($_SESSION as $sessionKey){
if (!in_array($sessionKey,$keepSessions)) {
unset($_SESSION[$sessionKey]);
}
}
Unfortunately I get the following error PHP Warning: Illegal offset type in unset
I cannot seem to find a solution that uses the foreach and $_SESSION to elimate the above error
$keepSessions = array('vendor_id','email_login','user_password','passport_id');
foreach($_SESSION as $sessionKey => $sessionValue){
if (!in_array($sessionKey,$keepSessions)) {
unset($_SESSION[$sessionKey]);
}
}
Seems you were using the values inside the $_SESSION variable instead of the keys. Hope it helps!

Yii - Manipulating a sesssion variable

I am a still a newbie when it comes to using YII, but I been working with session variables for the past few days, and I can't seem to grasp to the concept behind my error. Any advice will be appreciated.
My add function works perfectly so far, for my current purpose of keeping track of the last 3 variables added to my session variable nutrition.
public function addSessionFavourite($pageId)
{
$page = Page::model()->findByPk($pageId);
$categoryName = $page->getCategoryNames();
if($categoryName[0] == 'Nutrition')
{
if(!isset(Yii::app()->session['nutrition']))
{
Yii::app()->session['nutrition'] = array();
}
$nutrition = Yii::app()->session['nutrition'];
array_unshift($nutrition, $pageId);
array_splice($nutrition, 3);
Yii::app()->session['nutrition'] = $nutrition;
}
My remove function doesn't seem to work at all, no matter what I try to do with it. The reason why I am transfering the session array to a temp array was to try to get around the "If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called." But it was a total failure.
public function removeSessionFavourite($pageId)
{
$page = Page::model()->findByPk($pageId);
$categoryName = $page->getCategoryNames();
if($categoryName[0] == 'Nutrition')
{
if(!isset(Yii::app()->session['nutrition']))
{
return true;
}
$nutritionArray = Yii::app()->session['nutrition'];
unset($nutritionArray[$pageId]);
Yii::app()->session['nutrition'] = $nutritionArray;
}
Any advice or push toward to the correct direction will be appreciated.
I personally I have never used Yii::app()->session I normally use the Yii user and I have never had any issues with it:
Yii::app()->user->setState('test', array('a'=>1,'b'=>2));
print_r(Yii::app()->user->getState('test')); //see whole array
$test = Yii::app()->user->getState('test');
unset($test['b']);
Yii::app()->user->setState('test',$test);
print_r(Yii::app()->user->getState('test')); //only 'a'=>1 remains
Yii::app()->user->setState('test', null);
print_r(Yii::app()->user->getState('test')); //now a null value
As I put in a comment above there seems to be issues with multidimensional arrays with the session variable: https://code.google.com/p/yii/issues/detail?id=1681

Session not been set

public function action_adicionar_item()
{
$lista_item_pedido = array();
$x = 0;
if(Session::has('lista_item_pedido'))
{
foreach(Session::get('lista_item_pedido') as $item)
{
$lista_item_pedido[$x] = $item;
$x++;
}
}
$lista_item_pedido[$x] = Input::all();
Session::put('lista_item_pedido', $lista_item_pedido);
}
The first time I ran this method, the session is not created so the if is ignored and it sets the array value and should define the session with name a value but it doesn't.
The second time I call it, the session is created but with no values, what is weird.
Any ideas why on my first run the session is created with the empty array?
Input::all() is returning the correct values.
I have checked the file storage/sessions/ the file is created and the value is set correctly:
s:17:"lista_item_pedido";a:1:{i:0;a:7:{s:2:"id";s:3:"162";s:10:"referencia";s:12:"112233445566";s:9:"descricao";s:6:"Sapato";s:5:"grade";s:14:"Grade 41 ao 46";s:8:"grade_id";s:1:"4";s:5:"valor";s:5:"50.00";s:10:"fornecedor";s:2:"30";}}}s:13:"last_activity";i:1340395110;}
This is created the first time I run the method, so it is created but I can't access it, only when I add two values and in this case, the first is ignored.
Try using $_SESSION instead...

php array's making the Undefined index: 0 notice go away

Right so i have E_NOTICES on and my code works its just i keep getting "Severity: Notice Message: Undefined index: 0" everytime i try to insert my data into the array with the set key. Its really annoying when ur trying to debug.
What am i doing wrong that will make the notices go away without turning off E_NOTICES?
foreach ($bracketmatches->result() as $row)
{
if(!isset($bracketdata[$row->position]))
{
$bracketdata[$row->position] = array();
}
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
Is $teams[$row->home_id] definitely defined?
edit: Quick and dirty test for you:
foreach ($bracketmatches->result() as $row)
{
if(!isset($teams[$row->home_id]))
{
die('GOTCHA!!!');
}
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
Impossible to say for certain without more information, but I would check that $row->position is set and that $row->home_id is set if there is any possibility they may be undefined.
You must initialize the base array before pushing values to it. The isset here doesn't really do anything. Just throw it away. If you still get the error make sure $teams[$row->home_id]['team_name'] is always set.
$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
if (isset($row->position) && !empty($row->position) && isset($teams[$row->home_id]['team_name']))
$bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}

Categories