Jump to content


Photo

Replace array value with previous array value (key-1)


  • Please log in to reply
6 replies to this topic

#1 s1mpl3

s1mpl3
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 August 2006 - 03:28 AM

Please help me i have stucked here, i want to repalce session array value with previous session array value. Like this:
for($i=1;$i<=count($_SESSION['counter']);$i++){
  $_SESSION['job_code'][$i]=$_SESSION['job_code'][$i-1];
}

but i got the value that i dont want to.
how to fix this problem
Thanks before...

(sorry for my english) ;D

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 03:33 AM

$stop = count($_SESSION['jobcode']);
for($i=1;$i<=$stop;$i++){
  $_SESSION['job_code'][$i]=$_SESSION['job_code'][$i-1];
}

or you could simply use array_shift($_SESSION['job_code']) - this will remove teh first element of the array and of course make the array one element smaller.
follow me on twitter @PHPsycho

#3 s1mpl3

s1mpl3
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 August 2006 - 03:47 AM

Still not working  :'(. Ok my problem is actualy how to insert an empty row between another row to table and another row altered automatic and i have an algorithm like this :
if(isset($_GET['add']) && isset($_GET['row'])):
    $_SESSION['new_count']++;
    $row = $_GET['row'];
    for($i=1;$i<=$_SESSION['new_count];$i++){
        if($i<=$row){
	//do nothing just print value who have key = $i
		
	} else if($i==$row+1){
    	//Insert blank row

        }else{
	//Altred table, and value get from key-1
	
	}
      }
  endif;

How to implement that algorithm to PHP code?

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 04:00 AM

Sorry my friend but I'm not quite getting that.

I have never done it but I am not sure whether inserting a NULL element into an array will work - it probably will.

BUT can't really see from that code what you're trying to insert and why you need to.

follow me on twitter @PHPsycho

#5 s1mpl3

s1mpl3
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 August 2006 - 04:17 AM

Oh sorry for my english so you not understand what i mean, i try use word to explain that algorithm.

eg. there are 6 row, if user want to add a new row under row 3, so a new value in row 4 is blank and value in row 5 is value  from row 4 before its has new value (blank).

Thanks for your attention ToonMariner

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 04:39 AM

OK to mimic that using an array....

$arr = array(1,3,4,7,9);
$newrow = 4;
$size = count($arr);
$arr[$size] = NULL;

for($i=$size;$i=$newrow;$i--)
{
$arr[$i] = $arr[$i-1];
}
$arr[$newrow - 1] = NULL;

I think that could do it.
follow me on twitter @PHPsycho

#7 s1mpl3

s1mpl3
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 August 2006 - 04:55 AM

Ok thanks, its work...!! ;D :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users