bothwell Posted June 27, 2008 Share Posted June 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/ Share on other sites More sharing options...
.josh Posted June 27, 2008 Share Posted June 27, 2008 $_POST['strLocations'][0] Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/#findComment-576307 Share on other sites More sharing options...
bothwell Posted June 27, 2008 Author Share Posted June 27, 2008 $_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]? Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/#findComment-576344 Share on other sites More sharing options...
wildteen88 Posted June 27, 2008 Share Posted June 27, 2008 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/#findComment-576345 Share on other sites More sharing options...
bothwell Posted July 5, 2008 Author Share Posted July 5, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/#findComment-582426 Share on other sites More sharing options...
wildteen88 Posted July 5, 2008 Share Posted July 5, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/#findComment-582436 Share on other sites More sharing options...
bothwell Posted July 5, 2008 Author Share Posted July 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/112249-solved-n00b-php4-ph5-form-array-compatibility-problem/#findComment-582455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.