Messages - RedPillow [ switch to compact view ]

Pages: prev1 2 3 4 [5] 6 7 8 9 10 ... 28next
21
Developer's Corner / [PHP] Variables in array to int ?
« on: December 09, 2010, 12:19 AM »
I got this IF - FOREACH - THING here (I know that's a bit stupidly done ... adding "," to $num and removing it ...
My excuse is, that Im just a poor student with goal to someday become good at this kind of stuff and currently I don't care if there's something done stupidly ... cause if I ever try to fix that, it will not work):

Code: PHP [Select]
  1. if (isset($_GET["num"])) { foreach
  2. ($_GET["num"] as $arvo) $num = $num . $arvo . ",";
  3. $numchk[] = $num;}
  4. $numchktostring = $numchk[0];
  5. $num2 = explode(",", $numchktostring);
  6.  
  7. unset($num2[7]);
  8. $validchk = count($num2);

Now, it somewhy gives me output like this (By var_dumping $num2):
Code: PHP [Select]
  1. array(7) { [0]=>  string(1) "1" [1]=>  string(1) "2" [2]=>  string(1) "3" [3]=>  string(1) "4" [4]=>  string(1) "5" [5]=>  string(1) "6" [6]=>  string(1) "7" }

As you can see, every single key is a string.
My guestion is plain simple; how to make those strings int?

I have this example-snippet here, but how do I use it to array? (loop preferred):
Code: PHP [Select]
  1. $str = "10";
  2.       $num = (int)$str;

-- EDIT --

This seems to turn the variables to int, but it loses the avlue given to them before:

Code: PHP [Select]
  1. $i = 0;
  2. $a = 0;
  3. While ($i < 7) {
  4. $num2[$a] = (int)$num2[$a];
  5. $i++;
  6. $a++;
  7. }

var_dump($num2) outputs now:
Code: PHP [Select]
  1. array(7) { [0]=>  int(1) [1]=>  int(2) [2]=>  int(3) [3]=>  int(4) [4]=>  int(5) [5]=>  int(6) [6]=>  int(7) }

22
Developer's Corner / Re: What is wrong with this php-snippet?
« on: December 08, 2010, 03:28 AM »
_______
SOLVED

My html-side was messed-up really bad ...
I didn't even have a value for each option.

-- But hey, thanks a lot for your time and help !!

23
Developer's Corner / Re: What is wrong with this php-snippet?
« on: December 08, 2010, 03:17 AM »
Writing to file didn't work so well ... it only wrote the last option selected in the listbox to the file.

But here's a code from some php-site they used for multiple selections:

Code: HTML [Select]
  1. <form method=post action=''>
  2. <select name='color[]' size=4 multiple>
  3. <option value='blue'>Blue</option>
  4. <option value='green'>Green</option>
  5. <option value='red'>Red</option>
  6. <option value='yellow'>Yelllow</option>
  7. <option value='' selected>Select a Color </option>
  8. <option value='white'>White</option>
  9. <input type=submit></form>

///// collecting form data /////

Code: PHP [Select]
  1. @$color= $_POST['color'];
  2. if( is_array($color)){
  3. while (list ($key, $val) = each ($color)) {
  4. echo "$val <br>";
  5. }
  6. }//else{echo "not array";}


I fail to see what this snippet does differently ... maybe you can?

24
Developer's Corner / Re: What is wrong with this php-snippet?
« on: December 08, 2010, 02:49 AM »
Thanks, your explode-function works!
Though there is still a tiny problem..
With this, it only prints the last selected option in the listbox ie.

Option 1 -selected-
Option 2
Option 3 -selected-

output --> Only option 3

Btw, the value $_POST gets is not an array ... and it should be I guess since you can choose multiple options from the listbox.

Code: PHP [Select]
  1. $vehicles="";
  2. if (isset($_POST["vehicle"]))
  3. {$vehicles = "You chose:<br />";
  4. $arrex = explode(" ", $_POST["vehicle"]);
  5. foreach($arrex as $value)
  6. {$vehicles = $vehicles . $value . "<br />";}
  7. print $vehicles;}

25
Developer's Corner / What is wrong with this php-snippet?
« on: December 08, 2010, 01:51 AM »
Here, it comes up with this error "Invalid argument supplied for foreach() in J:\Xampp\xampp\htdocs\forms-1\next.php  on line 28
I read somewhere that the variable it's checking isn't array ... it gets the $_POST vehicle from listbox with multiple selections.

Code: PHP [Select]
  1. $vehicles=""; //line25
  2. if (isset($_POST["vehicle"])) //line26
  3. {$vehicles = "You chose: <br />"; //line27
  4. foreach ($_POST["vehicle"] as $value) //line28
  5. {$vehicles = $vehicles. $value . "<br />";} //line29
  6. print $vehicles;} //line230

Pages: prev1 2 3 4 [5] 6 7 8 9 10 ... 28next
Go to full version