Jump to content

Recommended Posts

Hello! New here.

 

I'm refactoring an old app from PHP4 into PHP5 and because I am a n00b I hadn't really future-proofed it at the time :)

 

I'm trying to pass an array through an HTML form, using the following code which works on PHP4 but is broken in PHP5:

 

<!--the form select itself (the locationselect function is just grabbing some entries from the DB, that works fine):-->
<select name="strLocations[]" multiple="yes" size="6" style="width:160px;"><?php echo (locationselect()); ?></select>

<!--the print on the following page:-->
print "locations:". $_POST['strLocations'] ."!";

 

The print is just blank - it is, of course, a syntax error, but I don't know what the right syntax actually is. ;) A quick pointer would be much appreciated.

 

Cheers,

4eB

$_POST['strLocations'][0]

 

 

Thanks for the quick response - works like a charm!

 

Quick question; what's the [0] actually for? Does it indicate the first item in the array? So if I wanted to, for whatever reason (although I can't imagine why I would want to hardcode this), only print the third item in the array, I would change that to [2]?

The text/number between the square brackets is what is called the array key (or index). You use the array key to retrieve an item (value) from the array. Arrays can be multi-dimensional.

 

So $_POST['strLocations'] holds an array within the $_POST array. To see the arrays structor use:

echo '<pre>' . print_r($your_array_var, true) . '</pre>';

Sorry to drag up this thread again, but I'm having a similar difficulty with another app - I'm getting three warnings that "the argument should be an array on line 77" of this piece of code:

 

$count = count($votes);
  for($i=0; $i < $count; $i++)
       {
         $total_votes = array_sum($votes[$i]); 
       } 

 

Line 77 is the one with the array_sum in it. The contents of $votes comes from unserializing another variable if it helps:

 

$poll = array(
     'question' => 'query?',
     'options' => array('No', 'Yes', 'Perhaps!'),
     'votes' => array(0,0,0),
     ); 

 

I think that PHP doesn't like the serialized $poll array since it warns me three times, once for each value in 'votes'. I've tried changing it to array(0=>"0",1=>"0",2=>"0"), but it hasn't made a difference. What am I missing?

Its more to with what you're trying to do on line 77.

$total_votes = array_sum($votes[$i]); 

Where is the $votes variable assigned? does $votes contain an array?

 

Perhaps you mean to do:

$total_votes = array_sum($votes);

 

Its more to with what you're trying to do on line 77.

$total_votes = array_sum($votes[$i]); 

Where is the $votes variable assigned? does $votes contain an array?

 

Perhaps you mean to do:

$total_votes = array_sum($votes);

 

 

I was using array_sum($votes); originally, but had the same "argument should be an array" warning (although only one of them instead of three). Now I've just typed out a long explanation of where $votes comes from and I thought I'd check first to make sure exactly what the error was before I used $votes[$i] - and now it works :/ I must have just not tested properly when I changed those array values to use index numbers.  ::)

 

Thanks for making me doublecheck! Seems I've got to learn how to debug properly just as much as I need to learn the language, heh.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.