Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create empty array, getting array from session vars

Status
Not open for further replies.

excession

Programmer
Sep 22, 2004
111
0
0
GB
A few simple questions regarding arrays.

1) how do i create an empty array?

2) I have a seeion variable, $piclist, this could be one value or an array of values. When i pick up the list in the do_process script I have a line:

$piclist = $_SESSION['piclist']

When the session piclist is an array all is fine, when it contains only one element I get an error telling me the array operator cannot be applied to strings.

What is going wrong? I've tried everything I can think of but I still can't get it to work.

 
1. Try:
Code:
$foo = array();
2. I feel we don't have enough information to give you a meaningful answer. From what you gave us, you are only re-assinging the session var to a local var. Be it a single string value or an array, this should cause no problems. Your error must lie elsewhere.
 
Here about the second question:
Code:
$piclist = "I am a string";

# You must be trying something like this:
$piclist[] = "Something else";

It will fail because you cannot add elements to a variable that is not an array. You should check that out before you try adding. Add a construct that checks if $piclist is an array, and if not, convert it:
Code:
if (!is_array($_SESSION['piclist'])){
   $piclist[] = $_SESSION['piclist'];
} else {
   $piclist = $_SESSION['piclist'];
}

This will get rid of the error.
The faulty thinking is that if there is a string value in the session variable you are dealing with an array that has one element. That is not true. The string actually itself is sort of an array as you would be able to access the individual chars by offset. The conversion shown above will make sure that the single string is put into $piclist as an array element.
 
sorry there wasn't much - the session variable piclist gets built up through several scripts and I didn't want to cut and paste half a dozen snippets.

I think I've solved it now... it may be an imperfect solution...

it all starts in a form where the user browses for a picture file. This ends up in the $_FILES var and is extracted like thus :



Code:
$piclist[] = (""); // *** i tried = array(0 etc here but
                   // *** didn't work. This was the only
                   // solution that did

if ($_FILES["file"]["name"] == ""){
  // no image
  } else {
    // new pic 1 file
    
    $target = "c:/public/xxx/pages/products/
                      productimages/".$pnos.".jpg";
    
    move_uploaded_file($_FILES["file"]
                           ["tmp_name"], "$target");
    // now need to ftp this to the server
    // will do this when doing the ftp, so make a note
    
    // *** the simple $piclist[] = $pnos.".jpg" 
    // *** didn't work here so was changed to this 
    array_push($piclist, $pnos.".jpg");
  }

then $piclist is stored in the session var like thus:

Code:
if (!empty($piclist)){
    if (!empty($_SESSION['piclist'])){
      $piclist = array_unique(array_merge(unserialize($_SESSION['piclist']), $piclist));

    // *** if piclist has only a single item in this
    // would throw a warning about param 2 being 
    // a string. By defineing piclist as an array
    // with an empty item in it this solved that 
    // problem.
      $_SESSION['piclist'] = serialize($piclist);
    } else {
      $_SESSION['piclist'] = serialize($piclist);
    }
  }

this loop can be repeated many times until the user decides to ftp the data ...


Code:
if  ((isset($_SESSION['piclist'])) && (!empty($_SESSION['piclist']))) {
  foreach (unserialize($_SESSION['piclist']) as $p){
    if ($p != ""){
      echo "uploading file : $p<br>";
      if($live==1)  do_upload($conn_id, $p, $p, $ftp_server);
    }


Hope this all makes sense.

the problem seems to be the was the piclist was passed via the session var. If it had 2 or more items in it, it wolud be passed as an array, if it had only on the it would be passed as a string, which was causing the problems.

Anyhow, like I said, I have made this work now. If there is a better way of doing it I would be interested.

Thanks

 
There is no need to serialize anything in the session variables. The PHP interpreter itself serializes the data before writing it to the session store.
I would recommend to take the serialization out and just have $_SESSION['piclist'] as an array. That will allow for the test when you assign your local variable.
 
DRJ478, I think you've got it there. As soon as I read your post the penny dropped. Thanks for that, I'll make the changes and tidy it up.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top